riwas Posted October 31, 2015 Share Posted October 31, 2015 Hello,I'm trying to display records from a MySQL Database with PHP. I'm kind of new when it comes to PHP. But I made a code for display the record..now this is my first time doing this. but instead of it working I received the below warning message...Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in /home/vuyaezcu/public_html/page1.php on line 34And this is my PHP code that I just made:<?php//make connectionmysql_connect('localhost', 'xxxxxxx','xxxxxxxxx');//select dbmysql_select_db('kunder');$sql="SELECT * FROM APARTMENTS";$records=mysql_query($sql);?><html><head><title>Lediga Lägenheter</title></head><body><table width="600" border="1" cellpadding="1" cellpadding="1"><tr><th>Objekt</th><th>Yta</th><th>ID</th><th>Kommun</th><tr><?phpwhile($apartments=mysql_fetch_assoc($records)){echo "<tr>";echo "<td>".$apartments['objekt']."</td>";echo "<td>".$apartments['yta']."</td>";echo "<td>".$apartments['id']."</td>";echo "<td>".$apartments['kommun']."</td>";echo "</tr>";}//end while?></table></body></html>Does anyone know why this is happening?I have been trying to find out for my self, but don't seem to find anything helpful..Thank you Girls and Guys;)!/J Quote Link to comment https://forums.phpfreaks.com/topic/298970-phpmysql-fetch_assoc/ Share on other sites More sharing options...
Barand Posted October 31, 2015 Share Posted October 31, 2015 Then your query failed and mysql_query is returning "false" echo mysql_error() after calling mysql_query() to see why Quote Link to comment https://forums.phpfreaks.com/topic/298970-phpmysql-fetch_assoc/#findComment-1525026 Share on other sites More sharing options...
riwas Posted October 31, 2015 Author Share Posted October 31, 2015 Then your query failed and mysql_query is returning "false" echo mysql_error() after calling mysql_query() to see why Hello Barand, Thank you for you reply! I don't quite understand what you mean. Did you spot a fault in my code? Quote Link to comment https://forums.phpfreaks.com/topic/298970-phpmysql-fetch_assoc/#findComment-1525028 Share on other sites More sharing options...
Barand Posted October 31, 2015 Share Posted October 31, 2015 $sql="SELECT * FROM APARTMENTS"; $records=mysql_query($sql); if ($records==false) echo mysql_error(); See what the error message tells you. Quote Link to comment https://forums.phpfreaks.com/topic/298970-phpmysql-fetch_assoc/#findComment-1525029 Share on other sites More sharing options...
riwas Posted October 31, 2015 Author Share Posted October 31, 2015 $sql="SELECT * FROM APARTMENTS"; $records=mysql_query($sql); if ($records==false) echo mysql_error(); See what the error message tells you. Now this is guite exiting to learn like this well, let's see... First of all, I notised that I'm missing the "if ($records==false) echo mysql_error();" ? I'm sorry, but I still don't get it I usually work with patients and not on computers, but I do want to learn!!! Could you please give me some more hints? Thanks!!! Quote Link to comment https://forums.phpfreaks.com/topic/298970-phpmysql-fetch_assoc/#findComment-1525031 Share on other sites More sharing options...
ginerjm Posted October 31, 2015 Share Posted October 31, 2015 Barand gave you the exact code to use. Insert it into your existing code where it should be and see what it tells you. Quote Link to comment https://forums.phpfreaks.com/topic/298970-phpmysql-fetch_assoc/#findComment-1525032 Share on other sites More sharing options...
Barand Posted October 31, 2015 Share Posted October 31, 2015 did you add this line and rerun the code? if ($records==false) echo mysql_error(); Quote Link to comment https://forums.phpfreaks.com/topic/298970-phpmysql-fetch_assoc/#findComment-1525033 Share on other sites More sharing options...
riwas Posted November 1, 2015 Author Share Posted November 1, 2015 did you add this line and rerun the code? if ($records==false) echo mysql_error(); Yes I did add if ($records==false) echo mysql_error(); right after $records=mysql_query($sql); <?php //make connection mysql_connect('localhost', 'xxxxx','xxxxxxxx'); //select db mysql_select_db('kunder'); $sql="SELECT * FROM apartments"; $records=mysql_query($sql); if ($records==false) echo mysql_error(); ?> <html> <head> <title>Lediga Lägenheter</title> </head> <body> <table width="600" border="1" cellpadding="1" cellpadding="1"> <tr> <th>Objekt</th> <th>Yta</th> <th>ID</th> <th>Kommun</th> <tr> <?php while($apartments=mysql_fetch_assoc($records)){ echo "<tr>"; echo "<td>".$apartments['objekt']."</td>"; echo "<td>".$apartments['yta']."</td>"; echo "<td>".$apartments['id']."</td>"; echo "<td>".$apartments['kommun']."</td>"; echo "</tr>"; }//end while ?> </table> </body> </html> But this time I got this message: No database selected Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in /home/vuyaezcu/public_html/page1.php on line 34 Quote Link to comment https://forums.phpfreaks.com/topic/298970-phpmysql-fetch_assoc/#findComment-1525038 Share on other sites More sharing options...
riwas Posted November 1, 2015 Author Share Posted November 1, 2015 Yes I did add if ($records==false) echo mysql_error(); right after $records=mysql_query($sql); <?php //make connection mysql_connect('localhost', 'xxxxx','xxxxxxxx'); //select db mysql_select_db('kunder'); $sql="SELECT * FROM apartments"; $records=mysql_query($sql); if ($records==false) echo mysql_error(); ?> <html> <head> <title>Lediga Lägenheter</title> </head> <body> <table width="600" border="1" cellpadding="1" cellpadding="1"> <tr> <th>Objekt</th> <th>Yta</th> <th>ID</th> <th>Kommun</th> <tr> <?php while($apartments=mysql_fetch_assoc($records)){ echo "<tr>"; echo "<td>".$apartments['objekt']."</td>"; echo "<td>".$apartments['yta']."</td>"; echo "<td>".$apartments['id']."</td>"; echo "<td>".$apartments['kommun']."</td>"; echo "</tr>"; }//end while ?> </table> </body> </html> But this time I got this message: No database selected Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in /home/vuyaezcu/public_html/page1.php on line 34 NEVER mind: I got it working now WOOW Thank you GUYS for you help!!! Happy Halloween!!! Quote Link to comment https://forums.phpfreaks.com/topic/298970-phpmysql-fetch_assoc/#findComment-1525039 Share on other sites More sharing options...
riwas Posted November 1, 2015 Author Share Posted November 1, 2015 There is one question do I still need some help with...the data that I want to show used swedish Ä.Ö.Å signs... and I know that in MySQL i need to change it to the UTF-8 right, for me to be alble to see the right letters? Quote Link to comment https://forums.phpfreaks.com/topic/298970-phpmysql-fetch_assoc/#findComment-1525040 Share on other sites More sharing options...
riwas Posted November 1, 2015 Author Share Posted November 1, 2015 (edited) There is one question do I still need some help with...the data that I want to show used swedish Ä.Ö.Å signs... and I know that in MySQL i need to change it to the UTF-8 right, for me to be alble to see the right letters? the collation is set to: latin1_swedish_ci Edited November 1, 2015 by Barand remove image Quote Link to comment https://forums.phpfreaks.com/topic/298970-phpmysql-fetch_assoc/#findComment-1525041 Share on other sites More sharing options...
benanamen Posted November 1, 2015 Share Posted November 1, 2015 (edited) I am surprised you fellow experts have not said a word about the OP using obsolete code. Your not helping anyone by getting that code working. At least get him on Mysqli and then help him. Shame, Shame! OP, since no one has told you I will have to. You are using obsolete code that will not work at all in the latest version of Php. You need to be using PDO or Mysqli. You are wasting your time learning obsolete code. It has been obsolete starting well over 11 Years ago. Edited November 1, 2015 by benanamen Quote Link to comment https://forums.phpfreaks.com/topic/298970-phpmysql-fetch_assoc/#findComment-1525052 Share on other sites More sharing options...
Barand Posted November 1, 2015 Share Posted November 1, 2015 I am surprised you fellow experts have not said a word about the OP using obsolete code. Every post I make says it in my sig Quote Link to comment https://forums.phpfreaks.com/topic/298970-phpmysql-fetch_assoc/#findComment-1525066 Share on other sites More sharing options...
benanamen Posted November 1, 2015 Share Posted November 1, 2015 Did you know that in the mobile version of this site there are no sigs? I am on the full version of the site most of the time and never notice anybody sigs. I'm sure the OP's don't either. Even so, that still doesn't account for helping to get obsolete code working. I personally have taken a stance with many other programmers to not help people get obsolete code working. It would be much better for everyone and the Inernet to guide them over to MySQLi or PDO and then have them come back with their updated code if they still have problems. If we were in the rehab world it would be known as enabling. Quote Link to comment https://forums.phpfreaks.com/topic/298970-phpmysql-fetch_assoc/#findComment-1525089 Share on other sites More sharing options...
ginerjm Posted November 2, 2015 Share Posted November 2, 2015 What would be really nice is for the community/authors to FINALLY remove MySQL instead of having this long denouement. Of course it has come to fruition, but many, many users do not yet run the most current version. My hoster is currently only on 5.5 Quote Link to comment https://forums.phpfreaks.com/topic/298970-phpmysql-fetch_assoc/#findComment-1525216 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.