-3

Possible Duplicate:
PHP: Warning: sort() expects parameter 1 to be array, resource given

I have the following PHP code but get this error. I tried different solutions but it does not seem to work. Any help would be greatly appreciated.

<?php
INCLUDE 'functions.php' ;

$host = 'localhost';    
$id = '***';    
$pwd = '***';    
$db = '****'; 

$myconnection = connect_db($host, $id, $pwd, $db);

$SortOn = $_POST["SortOn"];    
$SortIn = $_POST["SortIn"];


$sql = "SELECT ID, DateTime, FirstName, LastName, AdditionalInformation, Category1,    Category2, Category3, Category4, Category5, Pending, Approved, Disapproved, WebsiteName, WebsiteURL FROM 'websites' ORDER BY $SortOn $SortIn";
echo "<table border=\"1\"><tr><th>ID</th><th>Date & Time</th><th>First Name</th><th>Last Name</th><th>Additional Information</th><th>Category 1</th><th>Category 2</th><th>Category 3</th><th>Category 4</th><th>Category 5</th><th>Pending</th><th>Approved</th><th>Disapproved</th><th>Website Name</th><th>Website URL</th></tr>";

$result = mysql_query($sql);            



while ( $row = mysql_fetch_array($result))            
{                
    echo "<tr>";        
    for ( $column = 0;$column < count($row);$column++)        
    {            
        echo "<td>" . $row[$column] . "</td>";        
    }        
    echo "</tr>";    


}            
echo "</table>";

?>   
Community
  • 1
  • 1

3 Answers3

0

This variable $result gave you a boolean (probabily it will return a False or something like that, for tell you that query hasn't done a "good" execution) and not a resource as he expected.

So you have to use something like mysql_error() for retrive the cause and correct the above code.

Edit Take a look to your code, you miss a , in your order by fields

DonCallisto
  • 28,203
  • 8
  • 66
  • 94
  • Looks like this is on the right path but I can seem to figure out where to put this. I tried different places just for the sake of it but it keeps giving the same error. Its driving me crazy especially as I basically copied and pasted the code from a previous project and it all worked fine. Puzzling – user1410474 May 22 '12 at 15:13
  • @user1410474 Tried to remove the single quotas around websites and to add the "," between group by fields? [Group By Syntax](http://www.w3schools.com/sql/sql_groupby.asp) – DonCallisto May 22 '12 at 15:15
0

mysql_query returns a boolean false when an error occurs. So it looks like you have to fix your query. You can use mysql_error to figure out what the error is.

MrSoundless
  • 1,233
  • 2
  • 12
  • 34
  • Ok thanks guys. I did this and the error I get is: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''websites' ORDER BY ID asc' at line 1 The funny thing is that when I run this same query directly on the MySQL it works fine. – user1410474 May 22 '12 at 15:02
  • It shouldn't. I think you need FROM `websites` instead of FROM 'websites' – MrSoundless May 22 '12 at 15:06
  • If I do this, I get the following error: Notice: Undefined offset: 15 in – user1410474 May 22 '12 at 15:11
  • I stand corrected, It does produce the result now but also adds errors forever at the end of the table as in Notice: Undefined offset: 15 in C:\wamp\www\xxx\website\displaywebsiterequests.php on line 38 Call Stack # Time Memory Function Location 1 0.0017 374328 {main}( ) ..\displaywebsiterequests.php:0 – user1410474 May 22 '12 at 15:15
0

Make sure you have a valid database connection too. Try connect_db($host, $id, $pwd, $db) or die(mysql_error()). Also, use die in sql statement as suggested by others.

Akshat Goel
  • 540
  • 4
  • 17