Jump to content

PHP/MySQL fetch_assoc


riwas

Recommended Posts

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 34


And this is my PHP code that I just made:


<?php

//make connection
mysql_connect('localhost', 'xxxxxxx','xxxxxxxxx');

//select db
mysql_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>

<?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>



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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

$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!!!

Link to comment
Share on other sites

 

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

Link to comment
Share on other sites

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!!!

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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 by Barand
remove image
Link to comment
Share on other sites

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!  :shrug:

 

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 by benanamen
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.