0

Hello Im getting this error

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\wamp\www\Dynamic [PHP WEBSITE]\includes\main_content.php on line 15

Anyone fix this code..

Here is my code

<?php 
include("includes/connect.php");

$select_posts = "select * from prodcut order by rand() LIMIT 0,4";

$run_posts = mysql_query($select_posts);

while($row=mysql_fetch_array($run_posts)){ /* This is LINE no. 15*/
$post_id = $row['pid']; 
$post_name = $row['name'];
$post_image = $row['image'];
$post_details = substr($row['details'],0,200);
?>

Here is screenshot of code..

http://i.imgur.com/Ca1cCWS.png

Screenshot of database table http://img546.imageshack.us/img546/699/8ler.png

Remember: im only want to show ID, name, image and details of every post on my website.

2 Answers2

4

Try error checking with:

$run_posts = mysql_query($select_posts) or die(mysql_error());

There should be an error, that's why it fails. Read the error mysql_error() prints out and solve the problem.

EDIT: And read the comment under your post by MLeFevre. This may be the error.

Marcel Balzer
  • 2,591
  • 2
  • 15
  • 31
-1

the reason why the parameter is missing is because you don't have it in db so you need to condition the while loop. If nothing then No data!

if($run_posts){

while($row=mysql_fetch_array($run_posts)){ /* This is LINE no. 15*/
$post_id = $row['pid']; 
$post_name = $row['name'];
$post_image = $row['image'];
$post_details = substr($row['details'],0,200);
}
else {echo "No data!";}  
SamotnyPocitac
  • 299
  • 4
  • 16
  • That's not correct. If $run_posts == false then there is an error. If there is no data, just nothing happens. – Marcel Balzer Dec 11 '13 at 15:05
  • don't you understand? he is giving you an error, so why are you asking for checking the error? Thanks for voting down my correct answer. – SamotnyPocitac Dec 11 '13 at 15:10
  • Your answer is NOT correct. Read the manual of mysql_query(). It returns false (what you are checking with if($run_posts)) when an ERROR occurs and NOT if no data is returned. Then there are just 0 loops from the while loop. – Marcel Balzer Dec 11 '13 at 15:12