dongzhi2332 2013-09-09 00:13
浏览 18
已采纳

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

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>
  • 写回答

2条回答 默认 最新

  • doumenshi1475 2013-09-09 00:48
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 luckysheet
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱