1

Somewhere you have patience for takes you somewhere, only now the last problem.

It works for one party, only for the persons who not have any recipies, the peron with recipies can't see it, it only see a blank ó page.

$usersi = $dbh->prepare('SELECT * FROM recettes WHERE id_user = :id');
$usersi->bindParam(':id', $_GET['id'], PDO::PARAM_INT);
$usersi->execute();
$usersis = $usersi->fetchAll(PDO::FETCH_ASSOC);
  • And what doesn’t work about that? Is there an error? Please include it if so, or the contents of your tables, the results you expect, and what you’re actually getting if not. – Ry- Apr 25 '15 at 02:16
  • What is the issue you are facing? Can you share that? – Pratik Apr 25 '15 at 02:22
  • What i said was 'show only recipes' select by id, and now everybody see each others recipes. – Joshua Hiwat Apr 25 '15 at 02:23
  • can you show your tables structures and an example of your expected output please? – Adrian Cid Almaguer Apr 25 '15 at 02:25
  • So for every user, currently your query should be displaying recepies linked to him/her. If you want for a particular user, put a where clause. I still might not be getting what you really want here – Pratik Apr 25 '15 at 02:27
  • 1
    Do you only want to select recipes for one user? Don’t use a join; it’s just `SELECT * FROM recettes WHERE id_user = :user`, then `->execute([':user' => $user_id])`. – Ry- Apr 25 '15 at 02:30
  • I pick up 2 id's from 2 different tables. I have a profile page where users can see their own recipies. now they see every recipe of everybody. – Joshua Hiwat Apr 25 '15 at 02:32
  • And you want the users to just see their own recepies right? Then add the where condition for id = id of the user viewing the page. – Pratik Apr 25 '15 at 02:34
  • Can't i need to have it on his own like 'INNER JOIN'. – Joshua Hiwat Apr 25 '15 at 02:41
  • Inner join is used to link two tables based on a key column(ideally). The result would be all the records in the two tables satisfying the join condition which in this case is that the userId in the recepies table should be present in user table. To filter on a particular user, you need to know the where clause. – Pratik Apr 25 '15 at 02:45
  • possible duplicate of [Difference between INNER and OUTER joins](http://stackoverflow.com/questions/38549/difference-between-inner-and-outer-joins) – Dhanuka Apr 25 '15 at 02:47
  • It need to be a inner join there are no obstacles – Joshua Hiwat Apr 25 '15 at 02:53

3 Answers3

2

For a user with id = 1:

<?php
$usersi_sql = $dbh->prepare('SELECT * FROM recettes WHERE id_user = 1');
$usersi_sql->execute();
$usersi = $usersi_sql->fetchAll(PDO::FETCH_ASSOC);
?> 
Adrian Cid Almaguer
  • 7,815
  • 13
  • 41
  • 63
1

The solution on my question is:

$usersi_sql = $dbh->prepare('SELECT * FROM recettes WHERE id_user = :id');
$usersi_sql->bindParam(':id', $ma['id'], PDO::PARAM_INT);
$usersi_sql->execute();
$usersi = $usersi_sql->fetchAll(PDO::FETCH_ASSOC);  
if(isset($usersi[0])) {
?>

Here your HTML code

<?php
}
 }else{
?>

Here your HTML else code

<?php } ?>

This in the top of your php file

$ma_sql = $dbh->prepare('SELECT * FROM users WHERE id = :id');
$ma_sql->bindParam(':id', $_GET['id'], PDO::PARAM_INT);
$ma_sql->execute();
$ma = $ma_sql->fetch();

I used this in my top because it also check if the user is logged in or not, but this above is a part of that code.

0

If you want all recepies linked to all users -

SELECT * FROM recettes INNER JOIN users ON recettes.id_user = users.id LIMIT 20

If you want distinct recepies linked to all users -

SELECT DISTINCT recettes.* FROM recettes INNER JOIN users ON recettes.id_user = users.id LIMIT 20

If you want just for one user -

SELECT * FROM recettes WHERE id_user = user LIMIT 20

Change PHP call to -

$usersi_sql->bindParam(':user', $user_id);
$usersi_sql->execute()
Pratik
  • 1,122
  • 8
  • 26
  • Did you pass the userId through PHP? – Pratik Apr 25 '15 at 02:40
  • Parse error: syntax error, unexpected '[', expecting ')' in /home/joshuox91/domains/recettes.nl/public_html/new/profiel.php on line 409 – Joshua Hiwat Apr 25 '15 at 02:44
  • `$usersi_sql = $dbh->prepare('SELECT * FROM recettes WHERE id_user = :user LIMIT 20'); $usersi_sql->bindParam(':user', $user_id); $usersi_sql->execute() $usersi_sql->fetchAll(PDO::FETCH_ASSOC);` – Joshua Hiwat Apr 25 '15 at 03:00
  • SELECT * FROM recettes WHERE id_user = :user LIMIT 20. Substitute the userId here and run in database and see. If you get all results, its fine. Else I am blank. – Pratik Apr 25 '15 at 03:01