查询在更改后停止工作在哪里()[重复]

Hi i used a COunt(*) to paginate in my php code,

The Variable $cat , look like this sometimes is

$cat = "1,2,3,4,5,6,7,8,9,0"; 
$cat = "1";

This time i need multiple cats, thats why thanks sorry for missing that ,

I always use something like

$query = "SELECT COUNT(*) as num FROM $tableName WHERE cat_id=$cat";
    $total_pages = mysql_fetch_array(mysql_query($query)or trigger_error('Query failed: ' . mysql_error(), E_USER_ERROR));
    $total_pages = $total_pages['num'];

But now i nee to use WHERE id IN (1,2,3,4,5,etc) this is the actual query..

$query = "SELECT COUNT(*) as num FROM $tableName WHERE cat_id IN ($cat)";
    $total_pages = mysql_fetch_array(mysql_query($query)or trigger_error('Query failed: ' . mysql_error(), E_USER_ERROR));
    $total_pages = $total_pages['num'];

But i get this error using WHERE IN

   Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in /Applications/MAMP/htdocs/pcweb/admin/tiendas.php on line 173

So how do you di this count then.. thanks i think there must be an easy syntax error, but i cant find it cause i have never used Where IN, i look into documentation here. http://www.w3schools.com/sql/sql_in.asp but i think i do not know exactly what to google, so i ask..

Thanks

</div>

You are doing too many things on this line:

$total_pages = mysql_fetch_array(mysql_query($query)or trigger_error('Query failed: ' . mysql_error(), E_USER_ERROR));

Split it up:

$query = mysql_query($query);
if (!$query) {
    trigger_error('Query failed: ' . mysql_error(), E_USER_ERROR);
    // `exit;` or `return;` or do something else.
}
$total_pages = mysql_fetch_array($query);

The problem is that trigger_error doesn't actually stop control flow. You need to actually stop execution when you notice that something has gone wrong.

If $cat is an array, then do this:

$query = "SELECT COUNT(*) as num FROM $tableName WHERE cat_id IN (" . implode(',',$cat) . ")";