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>