0

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

Moncho Chavez
  • 674
  • 2
  • 10
  • 30

2 Answers2

2

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.

Joe Frambach
  • 26,300
  • 10
  • 69
  • 98
  • I notice that if i delete the trigger error stuff in the code, it works great, but i thougth the error was still there, silly me thanks for this solutions , totally got it, thanks dude! – Moncho Chavez Sep 09 '13 at 00:52
0

If $cat is an array, then do this:

$query = "SELECT COUNT(*) as num FROM $tableName WHERE cat_id IN (" . implode(',',$cat) . ")";
Joe Frambach
  • 26,300
  • 10
  • 69
  • 98
  • I would like to know why my answer was considered not useful. – Joe Frambach Sep 09 '13 at 00:20
  • i do not know, thanks for help i will vote up, i dont know why people downvote on helpers, even if it is a fool question its not your fault. I edited the question, this is not an array, thanks – Moncho Chavez Sep 09 '13 at 00:44