-9

On my Ajax Chat script I have got this warning:

Warning: mysql_fetch_array() expects parameter 1 to be resource,
boolean given in /home/mychat/public_html/c/listChats.php on line 7

Line 7:

while($row = mysql_fetch_array($sql)){

listChats.php:

<?php 
$chats = "";

include_once("scripts/connect_db.php");

$sql = mysql_query("SELECT * FROM (
    SELECT * FROM chatBox order by id DESC LIMIT 20
) TMP ORDER BY tmp.id ASC");

while($row = mysql_fetch_array($sql)){
    $chat = $row['chatBody'];
    $username = $row['username'];
    $chats = '<p><span id="un">' . $username . ':</span> ' . $chat . '</p>';
    echo "$chats";
}
?>
PhearOfRayne
  • 4,990
  • 3
  • 31
  • 44
  • @Charles My problem was to the point. – Agustin Leyva Dec 24 '12 at 06:37
  • 3
    There are [**three hundred fifteen potential duplicates**](http://stackoverflow.com/search?q=%22+mysql_fetch_array%28%29+expects+parameter+1+to+be+resource%2C+boolean+given%22) containing the error message you have provided. Every last one of them has the same core issue. The query failed and returned a bool. You need to add error checking to your code, and you need to fix the underlying SQL error. Hover over the downvote arrow. One of the reasons suggested there is "this question does not show any research effort," and it doesn't. – Charles Dec 24 '12 at 06:48
  • Now I know why people don't come on here your -point system is crazy – Agustin Leyva Dec 24 '12 at 06:49
  • @Charles Well if you say so and the reason I added this was because I was lost and needed help. – Agustin Leyva Dec 24 '12 at 06:51
  • So whats the deal with the points system - / + drop in points get ban – Agustin Leyva Dec 24 '12 at 06:52
  • Good questions get upvoted. Bad questions get downvoted. Don't take it personally. You might not have gotten the help you wanted, but you did get this help, at least: **always search for your error message first**. ***Always***. – Charles Dec 24 '12 at 06:54
  • @Charles I never take things personal site got rules, but thank's for the advice – Agustin Leyva Dec 24 '12 at 06:59
  • possible duplicate of [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) – Fenton Dec 24 '12 at 10:46
  • @SteveFenton sorry bro its not a possible duplicate of mysql_fetch_array() expects parameter 1 to be resource, boolean given in select. Because my code is different same problem different help problem. Plus problem was taken care of by the help of Harry. – Agustin Leyva Dec 24 '12 at 11:17
  • It is the same issue. Bad SQL syntax causes the issue. The answers to that question show you how to get the error message from MySQL to help diagnose the problem. – Fenton Dec 24 '12 at 11:18
  • I know that its kool bro Happy Holidays – Agustin Leyva Dec 24 '12 at 11:19

2 Answers2

1

Your query syntax is wrong

Try this

$sql = mysql_query("SELECT *,(

    SELECT * FROM chatBox order by id DESC LIMIT 20

) as data FROM TMP ORDER BY tmp.id ASC");

Updated query

select *,(select username from chatBox where id=".$_SESSION['userid'].") from chatBody where user_id=".$_SESSION['userid']
Hkachhia
  • 4,463
  • 6
  • 41
  • 76
0

I think you are using wrong table name.

Try this

$sql = mysql_query("SELECT *,(

    SELECT * FROM chatBox order by id DESC LIMIT 20

) as data FROM TMP ORDER BY TMP.id ASC");

If your table is TMP then use TMP.id if it is tmp then use tmp.id

Litisqe Kumar
  • 2,512
  • 4
  • 26
  • 40
fmask
  • 481
  • 7
  • 18