0

Is it possible to rewrite this script part it in different way? Because it works fine in my localhost ,but when i moved it in hosting it shows:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /***************************.php on line 67
<? 
$query = "SELECT COUNT(*) as num FROM $tableName"; 
//////Below error line
$total_pages = mysql_fetch_array(mysql_query($query)); 
/////////////
$total_pages = $total_pages['num'];
?>
Tomas
  • 9
  • 2
  • 1
    Check your database connection. Is MySQL running? Have you put the right database credentials for the connection? – Kemal Fadillah Oct 15 '11 at 15:00
  • 1
    Do some basic error checking. `mysql_query()` probably fails for some reason. `mysql_error()` can tell you why. – Pekka Oct 15 '11 at 15:00
  • 2
    Please please read at least a few dozen of the related links (on the right of this page). This error is reported again and again and again... – Mat Oct 15 '11 at 15:01
  • 1
    [Please look around to see if your question has been asked before](http://stackoverflow.com/search). – GG. Oct 15 '11 at 15:05

1 Answers1

-1

You can do just:

$query = "SELECT COUNT(*) FROM $tableName";

$total_pages = mysql_result(mysql_query($query), 0);

See PHP: mysql_result().

GG.
  • 21,083
  • 14
  • 84
  • 130
  • 1
    This is no better than the original code - when `mysql_query()` fails, it will return `false`. It is standard practice to catch that possibility, and if it occurs, to throw an error or exception containing `mysql_error()`'s output. The way you show will break exactly like the OP's original code, with the same error message. See also: [Reference: What is a perfect code sample using the mysql extension?](http://stackoverflow.com/q/6198104) – Pekka Oct 15 '11 at 15:13