Page 1 of 1

mysql_fetch_assoc() expects parameter 1 to be resource...

Posted: Mon Dec 26, 2011 5:07 am
by alex123
I have one last small issue.. I've been trying to figure it out for hours now trying many different things. This exact same script for a different table works perfectly on other pages.

I'm getting this error message: Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean [...] on line 21
this is line 21: return mysql_fetch_assoc($result); It seems as if I can't retrieve anything from the function from outside the function.

relevant files:
hockey.php (part of a sort of listings site)
<?php
session_start();
$link = mysql_connect('localhost','root','1234') or die('Cannot connect to database');
mysql_select_db('youtube');
	
	function fetch_posts(){
        $result = mysql_query('SELECT `id`, `location`, `section`, `heading`, `postedby` FROM `posts`') or die(mysql_error());

        $posts = array();

        while (($row = mysql_fetch_assoc($result)) !== false){
                $posts[] = $row;
        } 
        return $posts;
}
?>
<html>
<head>
<title>Hockey Posts</title>
</head>
<body>
	<div>
		<?php
		foreach (fetch_posts() as $posts){
			?>
			<p>
				<a href="hockeypost.php?id=<?php echo $posts['id'];?>"><?php echo $posts['heading']; ?></a>
				<br>City: <?php echo $posts['location']; ?>
				<br>Posted by: <?php echo $posts['postedby']; ?>
			</p>
			
			<?php
		}
	
		?>
	</div>
</body>
</html>
hockeypost.php
<?php 
session_start();
$link = mysql_connect('localhost','root','1234') or die('Cannot connect to database');
mysql_select_db('youtube');

function fetch_user_info($id){
	$id = (int)$id;

	$sql = "SELECT `details`,
				`location`, 
				`section`,
				`date`, 
				`heading`,
				`postedby`,
				`contactinfo`,
			FROM `posts`
			WHERE `id` = {$id}";
			
	$result = mysql_query($sql);
	
	return mysql_fetch_assoc($result);  \\THIS LINE HERE
}
$user_info = fetch_user_info($_GET['id']);
?>

<html>
<head>
<title>Lol</title>
</head>
	<body>
		<div>
		
			<?php
			if ($user_info == false){
				echo 'This post no longer exists.';
			}else{
			?>	
			
				<h1><?php echo $user_info['details']; ?></h1>
				<p>Contact Details: <?php echo $user_info['contactinfo']; ?></p>
				<p>City: <?php echo $user_info['location']; ?></p>
				<p>Posted By: <?php echo $user_info['postedby']; ?></p>
			<?php
			}
			?>
		</div>
	</body>
</html>

Re: mysql_fetch_assoc() expects parameter 1 to be resource..

Posted: Mon Dec 26, 2011 2:54 pm
by jacek
A few things first, you should not be connecting to the database in every script like this instead you should have one file that connects to the database which should be included by all of the pages. Also you should be defining functions on the actual page, the whole point of using a function is to make the code reusable and to separate the logic back-end stuff form the page stuff. The functions should be defined in a separate file that will be included.

for the actual problem, add a
echo mysql_error();
after the query causing the error and it will tell you what is wrong.

Re: mysql_fetch_assoc() expects parameter 1 to be resource..

Posted: Mon Dec 26, 2011 9:41 pm
by alex123
thanks so much! that got it to work in a minute! as for having it all in a back-end file, I'll do that now I guess