0

I have table in mysql like follow:

| sq | date | title | description |
-----------------------------------
| 10 | 10-06| xxxx  |  blah blah  |
| 09 | 10-06| xxxx  |  blah blah  |
| 08 | 10-06| xxxx  |  blah blah  |
| 07 | 10-06| xxxx  |  blah blah  |

Now I want to display them in screen using php in descending order of sq(squence).

I used the following code but its showing an error:

$result = mysql_query("SELECT * FROM posts WHERE (SELECT MAX(sn) FROM posts)");

echo('<ul class="news">');
while($row=mysql_fetch_array($result)) {
    $date=$row["date"];
    $title=$row["title"];
    $description=$row["description"];
    echo "<li><strong>$date</strong><h4><a href='#'>$lastname</a></h4>$description</li>";
}
echo('</ul>');

The error message:

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\wamp\www\final\index.php on line 99
Call Stack

And three at a time with a link to show others.

Thanks in advance.

DontVoteMeDown
  • 21,122
  • 10
  • 69
  • 105
vidya
  • 1
  • 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) – John Conde Jul 02 '13 at 13:42
  • See [this answer](http://stackoverflow.com/a/11674313/250259) for how to troubleshoot this. – John Conde Jul 02 '13 at 13:42

1 Answers1

1

Try,

SELECT * FROM posts ORDER BY sq DESC

and if you want to limit the results to lets say 10 rows use this

SELECT * FROM posts ORDER BY sq DESC LIMIT 10

EDIT: RE: mysql_fetch_array() expects parameter 1 to be resource

This error means that the

$result = mysql_query("SELECT * FROM posts WHERE (SELECT MAX(sn) FROM posts)");

statement has failed i.e. the query is wrong

When MySQL fails to execute your query you get FALSE returned into $result instead of a resource handle.

As @vidya says, you should always code a test of $result after the mysql_query() statement

$result = mysql_query("SELECT * FROM posts WHERE (SELECT MAX(sn) FROM posts)");
if ( ! $result ) {
    die( mysql_errno() . ' ' . mysql_error() );
}
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • @vidya try this one, plus, as a best pratice, adopt a validation `if` of your queries to handle those errors like: `if ($result) { }`. Good luck. – DontVoteMeDown Jul 01 '13 at 11:58
  • guys am getting this error Warning: mysql_fetch_array() expects parameter 1 to be resource, boole – vidya Jul 01 '13 at 12:07