Aido89 Posted July 15, 2013 Share Posted July 15, 2013 Hi Please see code below, when I query my db I am getting an unefined variable issue, any ideas? I am quiet new to PHP so please help. mysql_connect("localhost","root",""); mysql_select_db("db"); $id = intval($_GET['id']); $res = "SELECT * FROM table WHERE id=$id"; echo "<table>"; echo "<tr>"; echo "<td>"; echo "<h4>".$row['item']."</h4>"; echo "</td>"; echo "</tr>"; echo "<td>";?> <img src ="<?php echo $row['image']; ?>" height ="100" width ="100"> <?php echo "</td>"; echo "</tr>"; echo "<td>"; echo $row['description']; echo "</td>"; echo "</tr>"; echo "<td>"; echo $row['price']; echo "</td>"; echo "</tr>"; echo"</table>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/280159-undefined-variable-issue/ Share on other sites More sharing options...
Barand Posted July 15, 2013 Share Posted July 15, 2013 1. Use code tags (or <> button) when posting code. 2. post the actual error message Quote Link to comment https://forums.phpfreaks.com/topic/280159-undefined-variable-issue/#findComment-1440747 Share on other sites More sharing options...
White_Lily Posted July 15, 2013 Share Posted July 15, 2013 (edited) try writing a line between your query and the item output echos that says: $row = mysql_fetch_assoc($res); EDIT: Put all your echos on new lines too, this would make it slightly easier to read. Edited July 15, 2013 by White_Lily Quote Link to comment https://forums.phpfreaks.com/topic/280159-undefined-variable-issue/#findComment-1440748 Share on other sites More sharing options...
web_craftsman Posted July 15, 2013 Share Posted July 15, 2013 Aido89,you have forgotten mysql_query Quote Link to comment https://forums.phpfreaks.com/topic/280159-undefined-variable-issue/#findComment-1440750 Share on other sites More sharing options...
Aido89 Posted July 15, 2013 Author Share Posted July 15, 2013 Hi I have added in $row = mysql_fetch_assoc($res); as seen below but am now getting following error, Warning: mysql_fetch_assoc() expects parameter 1 to be resource, string given in Thanks again for fast replies guys, much appreciated <?php mysql_connect("localhost","root",""); mysql_select_db("db"); $id = intval($_GET['id']); $res = "SELECT * FROM table WHERE id=$id"; /* $res = "SELECT * FROM shoes WHERE id=".intval($_REQUEST['id']);*/ /*$res=mysql_query ("select * from shoes");*/ echo "<table>"; $row = mysql_fetch_assoc($res); { echo "<tr>"; echo "<td>"; echo "<h4>".$row['item']."</h4>"; echo "</td>"; echo "</tr>"; echo "<td>";?> <img src ="<?php echo $row['image']; ?>" height ="100" width ="100"> <?php echo "</td>"; echo "</tr>"; echo "<td>"; echo $row['description']; echo "</td>"; echo "</tr>"; echo "<td>"; echo $row['price']; echo "</td>"; echo "</tr>"; } echo"</table>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/280159-undefined-variable-issue/#findComment-1440751 Share on other sites More sharing options...
Barand Posted July 15, 2013 Share Posted July 15, 2013 See web_craftsman's reply $sql = "SELECT * FROM table WHERE id=$id"; $res = mysql_query($sql); Quote Link to comment https://forums.phpfreaks.com/topic/280159-undefined-variable-issue/#findComment-1440752 Share on other sites More sharing options...
Aido89 Posted July 15, 2013 Author Share Posted July 15, 2013 Hi I have tried this but the same error keeps appearing I'm afraid Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given inC:\xampp\htdocs\readmore.php on line 68 <?php mysql_connect("localhost","root",""); mysql_select_db("db"); $id = intval($_GET['id']); $sql = "SELECT * FROM table WHERE id=$id"; $res = mysql_query($sql); /* $res = "SELECT * FROM table WHERE id=".intval($_REQUEST['id']);*/ /*$res=mysql_query ("select * from table");*/ echo "<table>"; $row = mysql_fetch_assoc($res); { echo "<tr>"; echo "<td>"; echo "<h4>".$row['item']."</h4>"; echo "</td>"; echo "</tr>"; echo "<td>";?> <img src ="<?php echo $row['image']; ?>" height ="100" width ="100"> <?php echo "</td>"; echo "</tr>"; echo "<td>"; echo $row['description']; echo "</td>"; echo "</tr>"; echo "<td>"; echo $row['price']; echo "</td>"; echo "</tr>"; } echo"</table>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/280159-undefined-variable-issue/#findComment-1440755 Share on other sites More sharing options...
Solution Barand Posted July 15, 2013 Solution Share Posted July 15, 2013 check for errors $sql = "SELECT * FROM table WHERE id=$id"; $res = mysql_query($sql); if (!$res) die(mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/280159-undefined-variable-issue/#findComment-1440757 Share on other sites More sharing options...
Aido89 Posted July 15, 2013 Author Share Posted July 15, 2013 Yes it comes back with the below You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'table WHERE id=1' at line 1 Quote Link to comment https://forums.phpfreaks.com/topic/280159-undefined-variable-issue/#findComment-1440758 Share on other sites More sharing options...
Aido89 Posted July 15, 2013 Author Share Posted July 15, 2013 Hi i have it sorted, error in the table name, im such an idiot Quote Link to comment https://forums.phpfreaks.com/topic/280159-undefined-variable-issue/#findComment-1440759 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.