I’m having a problem my code. The mysql_fetch_array output repeating same value. This is my code :
$sql= "SELECT client.resID AS resID, client.resName AS resName, menu.id AS mid, menu.name AS mname, facilities.name AS fname, facilities.id AS fid FROM client INNER JOIN menu ON client.resID = menu.resID INNER JOIN facilities ON client.resID = facilities.resID WHERE client.resID =".$_GET["resID"];
$rs = mysql_query($sql) or die($sql."<br/><br/>".mysql_error());
while ($f = mysql_fetch_array($rs)) { // loop as long as there are more results
$names[] = $f['mname']; // push to the array
print_r($names)
}
Let’s say a restaurant have rice and potato. The print_r($names) are returning :
Array ( [0] => rice [1] => potato [2] => rice [3] => potato [4] => rice [5] => potato [6] => rice [7] => potato [8] => rice [9] => potato )
How could I solve this problem? Really appreciate your help 😀
Your query
returns as many occurences of “rice” as many times as the other two tables match. Since you are joining on the column set by
WHERE, your query is effectively a cartesian product of the three joined tables (after filtering byresID).ex.:
Perhaps you wanted to make three separate queries, one for each table
Or maybe you wanted to join on some other column (say, one that is unique in all three tables)?
also note that including unescaped get parameters in the query string is dangerous, even if you expect numbers. Use mysqli prepared statements or (at least) mysql_real_escape_string.