Hi, I am using this code to display some info from 2 mysql tables. <?php $result = mysql_query("SELECT 'giftlog'.'name' AS name, 'giftlog'.'image' AS image FROM 'giftlog' LEFT JOIN 'gifts' ON 'giftlog'.'giftid' = 'gifts'.'giftid' WHERE 'giftlog'.'recipientid' = " . $userid); echo $row['$name']; echo $row['$image']; ?> PHP: Problem is i get nothing !! No error just nothing !! So my question is, is the code wrong ? OR the sql tables ? Any1 got any help.
There probably is an error: tried adding this to the end of your query ? : or die(mysql_error()); PHP:
You're missing the part where you get a row from the result set. $row = mysql_fetch_array($result); It should go after your first line. Typically you'd do this in a while loop if your query can return multiple rows.
Did you add the bit to output the mysql error if there is one? Best guess is that it is working but your query is returning nothing. Make sure your $userid is set correctly and run the query manually in PhpMyAdmin or the like to make sure the data in the table is correct.
when you run mysql_connect($databasename, $username, $password) make sure that $username=proper name either from the config, or in your file
<?php $result = mysql_query("SELECT 'giftlog'.'name' AS name, 'giftlog'.'image' AS image FROM 'giftlog' LEFT JOIN 'gifts' ON 'giftlog'.'giftid' = 'gifts'.'giftid' WHERE 'giftlog'.'recipientid' = " . $userid); $row = mysql_fetch_assoc( $result ); echo $row['name']; // You don't use $ as it's not a variable. echo $row['image']; ?> PHP: That should work
Try this: $sql = 'SELECT ........'; $res = mysql_query($sql); echo $sql; // for debuging echo mysql_error(); // for debuging while ($arr = mysql_fetch_array($res)) { echo $arr['id']; // or whatever } PHP:
<?php $result = mysql_query("SELECT 'giftlog'.'name' AS name, 'giftlog'.'image' AS image FROM 'giftlog' LEFT JOIN 'gifts' ON 'giftlog'.'giftid' = 'gifts'.'giftid' WHERE 'giftlog'.'recipientid' = " . $userid) or die("MySQL Error with the query, error message: ".mysql_error()); $row = mysql_fetch_assoc( $result ) or die("MySQL Error with the 'fetch_assoc', error message: ".mysql_error()); echo $row['name']; // You don't use $ as it's not a variable. echo $row['image']; ?> PHP: Little revision on my code for better debugging. If you're only grabbing one row, then use the above code, otherwise use: <?php $result = mysql_query("SELECT 'giftlog'.'name' AS name, 'giftlog'.'image' AS image FROM 'giftlog' LEFT JOIN 'gifts' ON 'giftlog'.'giftid' = 'gifts'.'giftid' WHERE 'giftlog'.'recipientid' = " . $userid) or die("MySQL Error with the query, error message: ".mysql_error()); while($row = mysql_fetch_assoc( $result )) { echo $row['name']; // You don't use $ as it's not a variable. echo $row['image']; } ?> PHP: Also, I've used fetch_assoc() for faster queries, if the returned data isn't in an associative array, then switch fetch_assoc to fetch_array.