0

Possible Duplicate:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result

I have the following mysql query which is generating the above error and i can not work out why, any help would be appreciated.

$tableName="livetrack";     
$targetpage = "visitors.php";   
$limit = "$pagination"; 

$query = "SELECT COUNT(*) as num FROM $tableName where member_id = '$site_id' and display = 'yes'";
$total_pages = mysql_fetch_array(mysql_query($query));
$total_pages = $total_pages[num];

$stages = 3;
$page = mysql_escape_string($_GET['page']);
if($page){
    $start = ($page - 1) * $limit; 
}else{
    $start = 0; 
    }   

// Get page data

$query1 = "SELECT * FROM $tableName  WHERE date >= ( NOW() - '$fdate' ) and ip ='$ip' and  member_id = '$site_id' and display = 'yes'  and category= '$categories' and type ='$vtype' order by id DESC LIMIT $start, $limit";
$result = mysql_query($query1);

// Initial page num setup
if ($page == 0){$page = 1;}
$prev = $page - 1;  
$next = $page + 1;                          
$lastpage = ceil($total_pages/$limit);      
$LastPagem1 = $lastpage - 1;                    


$paginate = '';
if($lastpage > 1)
{   

The code below is where the error message is generating from.

while($row = mysql_fetch_array($result))
    {
Community
  • 1
  • 1
  • have you tried to print out the SQL code? – Dion Apr 10 '12 at 19:26
  • Is any code missing from this example? – HarryBeasant Apr 10 '12 at 19:26
  • Your problem is quite hard to debug from your code. – Lion Apr 10 '12 at 19:28
  • Why don't you use this statement `$total_pages = mysql_fetch_array(mysql_query($query)); ` in a loop? and you're retrieving just a single result and hence you may just need to use `mysql_result()` instead of using `mysql_fetch_array()`. – Lion Apr 10 '12 at 19:31
  • Yes there is some code missing which is just basically the pagination links –  Apr 10 '12 at 19:34

1 Answers1

1

You should print out the raw query and then run it manually in mysql (either through mysql tools or something like phpMyAdmin).

Do you get an error? (probably)

If yes, fix the query.

If not, you should always do a check for mysql errors whenever you run a query and before you try to access the results.

For instance:

  1. $query = 'SELECT * FROM users;';
  2. $result = mysql_query($query);
  3. if(mysql_error() != '') { echo 'error on mysql query: #'.mysql_errno().' - '.mysql_error();
  4. else { $data = mysql_fetch_array($result); }

A second likely possibility is that you don't have a valid connection to a database, or you are connected to the wrong database.

Patrick
  • 3,142
  • 4
  • 31
  • 46
  • Hi Thanks, I have error reporting turned on and the only error displayed was the one in the title, all the connections to the database were correct. In the end i just wrote it all in a different way and it worked. –  Apr 11 '12 at 01:07