0

It seems to be duplicate question, but I tried all the answers, which has been already posted.

this is my code

include"db.php";
//$poster_id=1;
$sql = "SELECT pi.full_name,pi.poster_img,pi.poster_tag,
         p.post_title,p.post_description,p.post_tag,
         p.post_snap
 FROM POSTS p JOIN POSTER_INFO pi ON p.poster_id=pi.auth_id";
   $result = mysql_query($sql) or die(mysql_error());
echo mysql_error();
$msg=" ";
while($row = mysql_fetch_array($result)) {

$msg_footer="<div class='post_footer'>";
$msg_footer=$msg_footer."<div class='post_img'>";
$msg_footer=$msg_footer."<img  src='". 'data:image/jpeg;base64,' . base64_encode( $row[6] ) ."'  width='330' height='130' /></div>";
$msg_footer=$msg_footer."<div class='post_author'><div  class='author_img'><img  src='". 'data:image/jpeg;base64,' . base64_encode( $row[1] ) ."'  width='80' height='80' /></div>";
$msg_footer=$msg_footer."<div class='author_info'><b>". $row[0] ."</b><br/><span style='font-size:11px'>". $row[2] ."</span><br/><span style='font-size:10px'>1  Post</span></div></div></div>";

$msg .= " <li><br/><span class='post_title'>". $row[3] ."</span><br/><br/><span class='post_description'>" . $row[4] . "</span></li>";
$msg .=$msg_footer;  //WARNING POINTING TO THIS LINE
}

I'm getting warning, which pointing last line of while loop;

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\labs\load_data.php on line 40

My sql query is correct, because, I tried to execute it in sql editor and in php page. like this

<?php
$sql = "SELECT pi.full_name,pi.poster_img,pi.poster_tag,
         p.post_title,p.post_description,p.post_tag,
         p.post_snap
 FROM POSTS p JOIN POSTER_INFO pi ON p.poster_id=pi.auth_id";
   $result = mysql_query($sql) or die(mysql_error());
echo mysql_error();

   while($row = mysql_fetch_array($result)) {
?>
<tr>
  <td><?php echo $row[0]; ?></td>
  <td><?php echo $row[2]; ?></td>
  <td><?php echo $row[3]; ?></td> 
</tr>
<?php }
?>

In this case, I'm not getting any warning.

11
  • 1
    obligatory comment saying to stop using the old, and now deprecated, mysql functions: php.net/manual/en/changelog.mysql.php Commented Jun 22, 2013 at 13:31
  • 1
    echo mysql_error() will not be even reached if there is an error. so its pointless code. Commented Jun 22, 2013 at 13:33
  • @HorusKol that doesn't mean it stopped working, there will be a point in time where you won't be able to upgrade with it. Commented Jun 22, 2013 at 13:33
  • 1
    @DevZer0 There will always be userland shims should the dated functions ever be removed. An actual reason to switch, is that PDO et al are significantly less cumbersome to use. Commented Jun 22, 2013 at 13:35
  • 1
    Yes, it will still work - but there's a number of reasons why it is being deprecated (albeit, at stupendously glacial rates), the topmost of which being it isn't as safe to use as the successor mysqli or PDO - both of which have been around for many years now. Commented Jun 22, 2013 at 13:36

2 Answers 2

1

I'm pretty sure that the error is not triggered by the code that you have posted.


Check the return value of mysql_query() at all positions in the code, like this:

if(mysql_query(...) === FALSE) {
    die(mysql_error());
}

I see you are already doing it in the code you have shown, but the warning will be triggered at another point in the code I assume.


Another explanation might be, that you overwrite $result somewhere in the code - but this is not shown as well.


General advice: Don't use mysql_* functions for new code. They are deprecated. Use PDO or mysqli_* instead

Sign up to request clarification or add additional context in comments.

5 Comments

@mario You are right (I've realized, thx).. But I think the problem is somewhere in code but not in the shown part
True, probably then just 'too localized' and wrong excerpt posted. But at least some research has been done on OPs part.
when you said... $result may be overwritten somewhere in code. Since, I have already checked it, but didn't get anywhere. Then, I saw, function (that wasn't created by me) has been called below my code, inside that function a wrong table was queried. When I change that table name to correct one. Then after, it solve my problem. Thanksss!!
@jWeavers I had once the same problem, it took me hours to figure that out! :) Consider to follow my advice and use PDO in favour of the old mysql_* functions
yes... i was also stuck for last 2-3 hours, lastly, I found error was somewhere else.
0

I think your query is failing, so producing a boolean FALSE instead of a query resource. But it's very strange that the second one runs fine..

1 Comment

you probably don't have the ability to comment yet, when you do use the comments for this type of input :) welcome to SO.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.