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.

Ravi
  • 30,829
  • 42
  • 119
  • 173
  • 1
    obligatory comment saying to stop using the old, and now deprecated, mysql functions: http://www.php.net/manual/en/changelog.mysql.php – HorusKol Jun 22 '13 at 13:31
  • 1
    `echo mysql_error()` will not be even reached if there is an error. so its pointless code. – DevZer0 Jun 22 '13 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. – DevZer0 Jun 22 '13 at 13:33
  • then... where i should put that.. – Ravi Jun 22 '13 at 13:34
  • 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. – mario Jun 22 '13 at 13:35
  • @mario i use PDO my self, but i was speaking in the context of this question. :) – DevZer0 Jun 22 '13 at 13:36
  • 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. – HorusKol Jun 22 '13 at 13:36
  • @jWeavers simply remove that line. if there is an error ` or die(mysql_error());` will print the error out and stop the program. – DevZer0 Jun 22 '13 at 13:37
  • @HorusKol i am not arguing with your cause :) – DevZer0 Jun 22 '13 at 13:39
  • @mario `There will always be userland shims ...` nice idea! It is so simple, why I never thought about this? :) – hek2mgl Jun 22 '13 at 13:45
  • Possible duplicate [mysql_fetch_array() expects parameter 1 to be resource, boolean given in select](http://stackoverflow.com/questions/2973202/mysql-fetch-array-expects-parameter-1-to-be-resource-boolean-given-in-select?answertab=votes#tab-top). – John Conde Jul 15 '13 at 17:30

2 Answers2

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

hek2mgl
  • 152,036
  • 28
  • 249
  • 266
  • @mario You are right (I've realized, thx).. But I think the problem is somewhere in code but not in the shown part – hek2mgl Jun 22 '13 at 13:34
  • 1
    True, probably then just 'too localized' and wrong excerpt posted. But at least some research has been done on OPs part. – mario Jun 22 '13 at 13:36
  • 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!! – Ravi Jun 22 '13 at 13:47
  • @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 – hek2mgl Jun 22 '13 at 13:49
  • yes... i was also stuck for last 2-3 hours, lastly, I found error was somewhere else. – Ravi Jun 22 '13 at 13:58
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..

Marc Delalonde
  • 335
  • 4
  • 17
  • you probably don't have the ability to comment yet, when you do use the comments for this type of input :) welcome to SO. – DevZer0 Jun 22 '13 at 13:38