Warning: mysql_fetch_assoc() expects parameter 1?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
defroster
Forum Commoner
Posts: 49
Joined: Wed Mar 24, 2010 12:05 pm

Warning: mysql_fetch_assoc() expects parameter 1?

Post by defroster »

Hello,

When I write this:

Code: Select all

<?php
require("incl/header.php");
require("incl/menu.php");
?>
<!------------------------------ HEADER ------------------------------------->
<?php
$sql = "Select photos.*, categories.cat FROM photos, categories
	WHERE photos.cat_id = categories.id
	ORDER BY dateposted DESC
	LIMIT 1;";
$result = mysql_query($sql);
$row = mysql_fetch_assoc($result);
echo "<h2><a href='viewentry.php?id=" . $row['id'] . "'>" .$row['title'] . "</a></h2><br>";

?>


<!------------------------------ FOOTER ------------------------------------->
<?php
require("incl/footer.php");
?>
I get this:
Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in /Applications/XAMPP/xamppfiles/htdocs/photo.php on line 12.

Line 12 is:

Code: Select all

$row = mysql_fetch_assoc($result);
Anyone have any ideas what I am doing wrong. Thanks!!

Thanks,
df
User avatar
liljester
Forum Contributor
Posts: 400
Joined: Tue May 20, 2003 4:49 pm

Re: Warning: mysql_fetch_assoc() expects parameter 1?

Post by liljester »

your query has an error, fix your sql and it should return a valid resource.
User avatar
Alex-V
Forum Newbie
Posts: 17
Joined: Mon Jul 19, 2010 3:53 pm

Re: Warning: mysql_fetch_assoc() expects parameter 1?

Post by Alex-V »

This can help you track down the problem.

Code: Select all

// change this line
$result = mysql_query($sql);

// to look like this
$result = mysql_query($sql)or die(mysql_error());
defroster
Forum Commoner
Posts: 49
Joined: Wed Mar 24, 2010 12:05 pm

Re: Warning: mysql_fetch_assoc() expects parameter 1?

Post by defroster »

Thanks for the help. When I enter code:

Code: Select all

$result = mysql_query($sql)or die(mysql_error());
It tells me "No database selected" which I find very strange. Any ideas?

/df
User avatar
Alex-V
Forum Newbie
Posts: 17
Joined: Mon Jul 19, 2010 3:53 pm

Re: Warning: mysql_fetch_assoc() expects parameter 1?

Post by Alex-V »

Code: Select all

// add this after mysql_connect()
mysql_select_db('database_name');
Post Reply