forumnz Posted August 25, 2007 Share Posted August 25, 2007 Please help - I am realy having trouble with this. It comes up with error: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /var/www/html/show_mid.php on line 12 Code: <?php $con = mysql_connect("localhost","aaa","aaa"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("photomagik_co_nz_-_soho", $con); $result = mysql_query("SELECT * FROM show"); while($row = mysql_fetch_array($result)) { echo $row['title'] . " " . $row['link']; echo "<br />"; } mysql_close($con); ?> Thanks, Sam. Quote Link to comment https://forums.phpfreaks.com/topic/66610-mysql-select/ Share on other sites More sharing options...
trq Posted August 25, 2007 Share Posted August 25, 2007 Your not far wrong, but you should always do some error handling when dealing with a database. Try... <?php if (!$con = mysql_connect("localhost","aaa","aaa")) { die('Could not connect: ' . mysql_error()); } mysql_select_db("photomagik_co_nz_-_soho", $con); $sql = "SELECT * FROM show"; if ($result = mysql_query($sql)) { if (mysql_num_rows($result)) { while ($row = mysql_fetch_array($result)) { echo $row['title'] . " " . $row['link'] . "<br />"; } } else { echo "No records found<br />"; } } else { echo "Query failed<br />$sql<br />" . mysql_error(); } mysql_close($con); ?> What do you get now? Quote Link to comment https://forums.phpfreaks.com/topic/66610-mysql-select/#findComment-333732 Share on other sites More sharing options...
forumnz Posted August 25, 2007 Author Share Posted August 25, 2007 Thanks, Now I get Query failed SELECT * FROM show 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 'show' at line 1 What now? Sam. Quote Link to comment https://forums.phpfreaks.com/topic/66610-mysql-select/#findComment-333739 Share on other sites More sharing options...
Barand Posted August 25, 2007 Share Posted August 25, 2007 SHOW is an SQL reserved word. Change name of you table or use "SELECT * FROM `show` " Quote Link to comment https://forums.phpfreaks.com/topic/66610-mysql-select/#findComment-333744 Share on other sites More sharing options...
beboo002 Posted August 25, 2007 Share Posted August 25, 2007 in my machine ur code is working without any error check this <?php $con = mysql_connect("localhost","root",""); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("timesheet", $con); $result = mysql_query("SELECT * FROM test1"); while($row = mysql_fetch_array($result)) { echo $row['firstname'] . " " . $row['age']; echo "<br />"; } mysql_close($con); ?> result is tom 25 Quote Link to comment https://forums.phpfreaks.com/topic/66610-mysql-select/#findComment-333771 Share on other sites More sharing options...
Barand Posted August 25, 2007 Share Posted August 25, 2007 No, your code is working on your machine, not his code. Your table name "test1" isn't an SQL reserved word. Quote Link to comment https://forums.phpfreaks.com/topic/66610-mysql-select/#findComment-333773 Share on other sites More sharing options...
beboo002 Posted August 25, 2007 Share Posted August 25, 2007 "show" is mysql keyword is not working u hav to change tablename Quote Link to comment https://forums.phpfreaks.com/topic/66610-mysql-select/#findComment-333774 Share on other sites More sharing options...
Barand Posted August 25, 2007 Share Posted August 25, 2007 Well done, beeboo - did you come up with that all by yourself? And as you'll see in my earlier post, you can also use `show`. Quote Link to comment https://forums.phpfreaks.com/topic/66610-mysql-select/#findComment-333776 Share on other sites More sharing options...
micmania1 Posted August 25, 2007 Share Posted August 25, 2007 I dont know if this will work but you don't get anywhere in life if you don't try... Change... <?php while($row = mysql_fetch_array($result)) ?> to... <?php while($row = mysql_fetch_array($result, MYSQL_ASSOC)) ?> Quote Link to comment https://forums.phpfreaks.com/topic/66610-mysql-select/#findComment-333780 Share on other sites More sharing options...
beboo002 Posted August 25, 2007 Share Posted August 25, 2007 both code will producee same result wht are diffrance between them Quote Link to comment https://forums.phpfreaks.com/topic/66610-mysql-select/#findComment-333786 Share on other sites More sharing options...
wildteen88 Posted August 25, 2007 Share Posted August 25, 2007 Oh please... will you stop posting code suggestions. Its not do with the code. Its to do with the SQL query being used. All is explained in Barand's post above. Quote Link to comment https://forums.phpfreaks.com/topic/66610-mysql-select/#findComment-333789 Share on other sites More sharing options...
Barand Posted August 25, 2007 Share Posted August 25, 2007 beeboo, One will return an array indexed by both field name and field number, the second will return an array indexed on field name only. Quote Link to comment https://forums.phpfreaks.com/topic/66610-mysql-select/#findComment-333790 Share on other sites More sharing options...
Barand Posted August 25, 2007 Share Posted August 25, 2007 wildteen, I agree. I was expecting someone to suggest a change of background color in the stylesheet next Quote Link to comment https://forums.phpfreaks.com/topic/66610-mysql-select/#findComment-333792 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.