从mysql_magic获取COUNT(*)

时间:2014-08-08 19:29:49

标签: php sql

以下代码从查询中检索结果。通常,我要求SELECT ...我得到一个二维数组,但这次我需要从表中计算。

if(isset($_GET['query']))
{
    $results = mysql_magic($_GET['query']);
    $response = array();
    //ERROR : Invalid argument supplied for foreach()   
    foreach($results as &$row)
    {
        $rowArray = array();
        foreach($row as &$column)
        {
            $rowArray[] = $column;
        }
        $response[] = $rowArray;
    }

    $jsonData = json_encode($results);
}

这是mysql_magic函数的一部分。在大多数情况下,它返回mysql_fetch_all($ req_result)。在这种情况下,它返回一行。

else if (startsWith($req_sql, 'select count(*)'))
{
    $line = mysql_fetch_row($req_result);
    return $line[0];
}

为什么我会收到错误"为foreach()提供的参数无效"既然我的结果,一个计数,包含一行一列?

1 个答案:

答案 0 :(得分:0)

我认为您使用的是mysql_magic($ _ GET ['query'])应该是mysql_query($ _ GET ['query'])。我在code中看不到$ _GET案例的引用。因为php错误导致$ results未定义?

if(isset($_GET['query']))
{
    $results = mysql_query($_GET['query']);
    $response = array();
    //ERROR : Invalid argument supplied for foreach()   
    foreach($results as &$row)
    {
        $rowArray = array();
        foreach($row as &$column)
        {
            $rowArray[] = $column;
        }
        $response[] = $rowArray;
    }

    $jsonData = json_encode($results);
}
相关问题