mysql_fetch_assoc() expects parameter 1

Alles, was PHP betrifft, kann hier besprochen werden.

mysql_fetch_assoc() expects parameter 1

Postby apachelearner » 09. June 2013 06:02

Hi,

When I place my php file in the browser, I encounter the following error :

mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\products.php on line 8

products.php :

Code: Select all
<?PHP
$link=mysql_connect("localhost","","");
mysql_select_db("brimelow_store");
$query='SELECT * FROM products';
$results=mysql_query($query);
echo "<?xml version=\"1.0\"?>\n";
echo "<products>\n";
while ($line=mysql_fetch_assoc($results)){
    echo "<item>".$line["pruduct"]."</item\n";
}
echo "</products>\n";
mysql_close($link);
?>


Which part that causes error, please point out.
apachelearner
 
Posts: 6
Joined: 03. June 2013 06:02
Operating System: win xp

Re: mysql_fetch_assoc() expects parameter 1

Postby Altrea » 09. June 2013 11:08

Hi apachelerner,

apachelearner wrote:Which part that causes error, please point out.

output errors with the help of mysql_errno() and mysql_error() to find it out yourself.

best wishes,
Altrea
We don't provide any support via personal channels like PM, email, Skype, TeamViewer!

It's like porn for programmers 8)
User avatar
Altrea
AF Moderator
 
Posts: 11935
Joined: 17. August 2009 13:05
XAMPP version: several
Operating System: Windows 11 Pro x64

Re: mysql_fetch_assoc() expects parameter 1

Postby apachelearner » 10. June 2013 08:08

Although try many ways but still can't solve.
apachelearner
 
Posts: 6
Joined: 03. June 2013 06:02
Operating System: win xp

Re: mysql_fetch_assoc() expects parameter 1

Postby Altrea » 10. June 2013 08:48

apachelearner wrote:Although try many ways but still can't solve.

Sorry but i don't believe that.

The error message says, that mysql_fetch_assoc() expects parameter 1 ($result) to be a resource (to be as specific as possible: a MySQL result set).
It contains a boolean value instead, which is most common the value false.
You have to find out why $result contains false.

The way to find that out is to implement mysql error functions on every place where a mysql command can fail (connection, db selection, query).
Show us how you have tried to implement the error functions and we can continue to give you specific help.
But we will not do the debugging work for you.

best wishes,
Altrea
We don't provide any support via personal channels like PM, email, Skype, TeamViewer!

It's like porn for programmers 8)
User avatar
Altrea
AF Moderator
 
Posts: 11935
Joined: 17. August 2009 13:05
XAMPP version: several
Operating System: Windows 11 Pro x64

Re: mysql_fetch_assoc() expects parameter 1

Postby apachelearner » 10. June 2013 14:29

Hi, I use the following in the browser and got this : 0: 0:

<?php
$link = mysql_connect("localhost", "root", "");

mysql_select_db("brimelow_store", $link);
echo mysql_errno($link) . ": " . mysql_error

($link). "\n";

mysql_select_db("brimelow_store", $link);
mysql_query("SELECT * FROM products", $link);
echo mysql_errno($link) . ": " . mysql_error

($link) . "\n";
?>

What does it mean ?
apachelearner
 
Posts: 6
Joined: 03. June 2013 06:02
Operating System: win xp

Re: mysql_fetch_assoc() expects parameter 1

Postby Altrea » 10. June 2013 15:15

Try this:
Code: Select all
<?php
$link = mysql_connect('localhost', 'root', '');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}

$db_selected = mysql_select_db('brimelow_store', $link);
if (!$db_selected) {
    die ('Can\'t use brimelow_store: ' . mysql_error());
}

$result = mysql_query("SELECT * FROM products", $link);
if (!$result) {
    die('Invalid query: ' . mysql_error());
}
?>
We don't provide any support via personal channels like PM, email, Skype, TeamViewer!

It's like porn for programmers 8)
User avatar
Altrea
AF Moderator
 
Posts: 11935
Joined: 17. August 2009 13:05
XAMPP version: several
Operating System: Windows 11 Pro x64

Re: mysql_fetch_assoc() expects parameter 1

Postby apachelearner » 10. June 2013 16:32

Hi,
I add some lines to your code, no errors, and it works.
Your guide and hints very helpful to me.
Thanks a lot,

<?php
$link = mysql_connect('localhost', 'root', '');
if (!$link) {
die('Could not connect: ' . mysql_error());
}

$db_selected = mysql_select_db('brimelow_store', $link);
if (!$db_selected) {
die ('Can\'t use brimelow_store: ' . mysql_error());
}
$sql = 'SELECT product FROM products';
$result = mysql_query($sql, $link);
if (!$result) {
die('Invalid query: ' . mysql_error());
}

while ($row = mysql_fetch_assoc($result)) {
echo $row['product'];
}

mysql_free_result($result)
?>
apachelearner
 
Posts: 6
Joined: 03. June 2013 06:02
Operating System: win xp


Return to PHP

Who is online

Users browsing this forum: No registered users and 5 guests