Page 1 of 1

Warning: mysql_fetch_assoc() expects parameter 1?

Posted: Mon Jul 19, 2010 7:59 pm
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

Re: Warning: mysql_fetch_assoc() expects parameter 1?

Posted: Mon Jul 19, 2010 8:23 pm
by liljester
your query has an error, fix your sql and it should return a valid resource.

Re: Warning: mysql_fetch_assoc() expects parameter 1?

Posted: Mon Jul 19, 2010 8:57 pm
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());

Re: Warning: mysql_fetch_assoc() expects parameter 1?

Posted: Tue Jul 20, 2010 5:50 am
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

Re: Warning: mysql_fetch_assoc() expects parameter 1?

Posted: Tue Jul 20, 2010 5:54 am
by Alex-V

Code: Select all

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