adamjones Posted April 15, 2009 Share Posted April 15, 2009 Ok. I'm working on a very simple guestbook, for user profiles. This is the code; <?php session_start(); ....DATABASE INFO.... mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); $user=$_GET['user']; $sql="SELECT * FROM $tbl_name WHERE to='$user'"; $result=mysql_query($sql); while($rows=mysql_fetch_array($result)){ ?> <p class="clearboth style1"> <p><div style="height:500px;width:350px;font:16px/26px Georgia, Garamond, Serif;overflow:scroll;"> <table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td><? echo $rows['from']; ?></td> <td><? echo $rows['message']; ?></td> <td> </td> </tr> </table> </div> </p> <div class="clearboth"></div> <div class="end"></div> <? } mysql_close(); ?> But I get this error; Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/domainey/public_html/habhub/guestbook.php on line 17 Any ideas? Adam. Quote Link to comment https://forums.phpfreaks.com/topic/154143-solved-why-isnt-this-working-s/ Share on other sites More sharing options...
MasterACE14 Posted April 15, 2009 Share Posted April 15, 2009 try... <?php session_start(); // ....DATABASE INFO.... $con = mysql_connect($host, $username, $password)or die("cannot connect"); $con = mysql_select_db($db_name)or die("cannot select DB"); $sql="SELECT * FROM $tbl_name WHERE to='".$_SESSION['user']."'"; $result = mysql_query($sql) or die(mysql_error()); while($rows = mysql_fetch_array($result)) { ?> <p class="clearboth style1"> <p><div style="height:500px;width:350px;font:16px/26px Georgia, Garamond, Serif;overflow:scroll;"> <table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td><?php echo $rows['from']; ?></td> <td><?php echo $rows['message']; ?></td> <td> </td> </tr> </table> </div> </p> <div class="clearboth"></div> <div class="end"></div> <?php } mysql_close($con); ?> see what error it gives you now. Quote Link to comment https://forums.phpfreaks.com/topic/154143-solved-why-isnt-this-working-s/#findComment-810276 Share on other sites More sharing options...
adamjones Posted April 15, 2009 Author Share Posted April 15, 2009 try... <?php session_start(); // ....DATABASE INFO.... $con = mysql_connect($host, $username, $password)or die("cannot connect"); $con = mysql_select_db($db_name)or die("cannot select DB"); $sql="SELECT * FROM $tbl_name WHERE to='".$_SESSION['user']."'"; $result = mysql_query($sql) or die(mysql_error()); while($rows = mysql_fetch_array($result)) { ?> <p class="clearboth style1"> <p><div style="height:500px;width:350px;font:16px/26px Georgia, Garamond, Serif;overflow:scroll;"> <table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td><?php echo $rows['from']; ?></td> <td><?php echo $rows['message']; ?></td> <td> </td> </tr> </table> </div> </p> <div class="clearboth"></div> <div class="end"></div> <?php } mysql_close($con); ?> see what error it gives you now. Thanks. I get this now; 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 'to='Adam'' at line 1 Quote Link to comment https://forums.phpfreaks.com/topic/154143-solved-why-isnt-this-working-s/#findComment-810280 Share on other sites More sharing options...
ToonMariner Posted April 15, 2009 Share Posted April 15, 2009 <?php session_start(); ....DATABASE INFO.... mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); $sql="SELECT * FROM $tbl_name WHERE to = '". mysql_real_escape_string($user) . "'"; $result=mysql_query($sql); if (mysql_num_rows($result) > 0) { while($rows=mysql_fetch_array($result)){ ?> <p class="clearboth style1"> <p><div style="height:500px;width:350px;font:16px/26px Georgia, Garamond, Serif;overflow:scroll;"> <table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td><? echo $rows['from']; ?></td> <td><? echo $rows['message']; ?></td> <td> </td> </tr> </table> </div> </p> <div class="clearboth"></div> <div class="end"></div> <? } } else { echo "<p>no recipient could be found</p>"; } mysql_close(); ?> is $tbl_name set? is to the correct field? Quote Link to comment https://forums.phpfreaks.com/topic/154143-solved-why-isnt-this-working-s/#findComment-810283 Share on other sites More sharing options...
adamjones Posted April 15, 2009 Author Share Posted April 15, 2009 <?php session_start(); ....DATABASE INFO.... mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); $sql="SELECT * FROM $tbl_name WHERE to = '". mysql_real_escape_string($user) . "'"; $result=mysql_query($sql); if (mysql_num_rows($result) > 0) { while($rows=mysql_fetch_array($result)){ ?> <p class="clearboth style1"> <p><div style="height:500px;width:350px;font:16px/26px Georgia, Garamond, Serif;overflow:scroll;"> <table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td><? echo $rows['from']; ?></td> <td><? echo $rows['message']; ?></td> <td> </td> </tr> </table> </div> </p> <div class="clearboth"></div> <div class="end"></div> <? } } else { echo "<p>no recipient could be found</p>"; } mysql_close(); ?> is $tbl_name set? is to the correct field? Yes, and yes Quote Link to comment https://forums.phpfreaks.com/topic/154143-solved-why-isnt-this-working-s/#findComment-810286 Share on other sites More sharing options...
Philip Posted April 15, 2009 Share Posted April 15, 2009 to is a reserved word. Either rename the column or change it in the query to `to` Quote Link to comment https://forums.phpfreaks.com/topic/154143-solved-why-isnt-this-working-s/#findComment-810288 Share on other sites More sharing options...
adamjones Posted April 15, 2009 Author Share Posted April 15, 2009 to is a reserved word. Either rename the column or change it in the query to `to` Ahh. Thank you. Never knew this; Was just about to scrap the whole thing! Quote Link to comment https://forums.phpfreaks.com/topic/154143-solved-why-isnt-this-working-s/#findComment-810290 Share on other sites More sharing options...
Philip Posted April 15, 2009 Share Posted April 15, 2009 This page will help you http://dev.mysql.com/doc/mysqld-version-reference/en/mysqld-version-reference-reservedwords.html Quote Link to comment https://forums.phpfreaks.com/topic/154143-solved-why-isnt-this-working-s/#findComment-810291 Share on other sites More sharing options...
adamjones Posted April 15, 2009 Author Share Posted April 15, 2009 This page will help you http://dev.mysql.com/doc/mysqld-version-reference/en/mysqld-version-reference-reservedwords.html Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/154143-solved-why-isnt-this-working-s/#findComment-810293 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.