PHP / MySQL PDO的奇怪行数返回

时间:2012-04-30 22:09:58

标签: php mysql pdo

我在phpMyAdmin中检查了这个,并返回了1个结果:

    SELECT id, name
      FROM tbl_product_category
INNER JOIN tbl_product_cat_basket AS basket
        ON id = basket.cat_id
     WHERE basket.product_id = 2
     LIMIT 0,1

然后我在phpMyAdmin中检查了它,它返回COUNT = 1:

    SELECT COUNT(*)
      FROM tbl_product_category
INNER JOIN tbl_product_cat_basket AS basket
        ON id = basket.cat_id
     WHERE basket.product_id = 2
     LIMIT 0,1

现在我不明白为什么这会导致$rows返回空字符串或null:

function getProductCategory($product_id) {
    $sql = "SELECT id, name FROM tbl_product_category
        INNER JOIN tbl_product_cat_basket AS basket
                ON id = basket.cat_id
             WHERE basket.product_id = ?
             LIMIT 0,1;";
    $data = array((int)$product_id);
    $rows = dbRowsCount($sql, $data);
logErrors("getProductCategory(".$product_id.") returned rows: ".$rows);
    if ($rows == 1) {
        dbQuery($sql, $data);
        return dbFetch();
    } else {
        return null;
    }
}

logErrors方法为我提供了这个输出:

  

getProductCategory(1)返回行:
  getProductCategory(2)返回行:
  getProductCategory(3)返回行:
  getProductCategory(4)返回行:
  getProductCategory(5)返回行:

所以我从方法中返回null

我的dbRowsCount(...)方法将SQL转换为SELECT COUNT(*),然后dbQuery(...)执行真实查询。这很奇怪,因为到目前为止dbRowsCount(...)总是为我的其他查询返回正确的行。

无论如何这是:

function dbRowsCount($sql, $data) {
    global $db, $query;
    $regex = '/^SELECT\s+(?:ALL\s+|DISTINCT\s+)?(?:.*?)\s+FROM\s+(.*)$/i';
    if (preg_match($regex, $sql, $output) > 0) {
        $query = $db->prepare("SELECT COUNT(*) FROM {$output[1]}");
        $query->setFetchMode(PDO::FETCH_NUM);
        if ($data != null)
            $query->execute($data);
        else
            $query->execute();
        if (!$query) {
            echo "Oops! There was an error: PDOStatement returned false.";
            exit;
        }
        return $query->fetchColumn();
    }
    return false;
}

那出了什么问题?


修改

以下是我的程序的一些输出: [原始查询] [正则表达式后查询]

SELECT id, name FROM tbl_product_category INNER JOIN tbl_product_cat_basket AS basket ON id = basket.cat_id WHERE basket.product_id = ? LIMIT 0,1;
SELECT COUNT(*) FROM tbl_product_category

getProductCategory(1) returned rows: 7
SELECT id, name FROM tbl_product_category INNER JOIN tbl_product_cat_basket AS basket ON id = basket.cat_id WHERE basket.product_id = ? LIMIT 0,1;
SELECT COUNT(*) FROM tbl_product_category

getProductCategory(2) returned rows: 7
SELECT id, name FROM tbl_product_category INNER JOIN tbl_product_cat_basket AS basket ON id = basket.cat_id WHERE basket.product_id = ? LIMIT 0,1;
SELECT COUNT(*) FROM tbl_product_category

getProductCategory(3) returned rows: 7
SELECT id, name FROM tbl_product_category INNER JOIN tbl_product_cat_basket AS basket ON id = basket.cat_id WHERE basket.product_id = ? LIMIT 0,1;
SELECT COUNT(*) FROM tbl_product_category

getProductCategory(4) returned rows: 7
SELECT id, name FROM tbl_product_category INNER JOIN tbl_product_cat_basket AS basket ON id = basket.cat_id WHERE basket.product_id = ? LIMIT 0,1;
SELECT COUNT(*) FROM tbl_product_category

getProductCategory(5) returned rows: 7
SELECT id, name FROM tbl_product_category INNER JOIN tbl_product_cat_basket AS basket ON id = basket.cat_id WHERE basket.product_id = ? LIMIT 0,1;
SELECT COUNT(*) FROM tbl_product_category

getProductCategory(6) returned rows: 7
SELECT id, name FROM tbl_product_category INNER JOIN tbl_product_cat_basket AS basket ON id = basket.cat_id WHERE basket.product_id = ? LIMIT 0,1;
SELECT COUNT(*) FROM tbl_product_category

getProductCategory(7) returned rows: 7
SELECT id, name FROM tbl_product_category INNER JOIN tbl_product_cat_basket AS basket ON id = basket.cat_id WHERE basket.product_id = ? LIMIT 0,1;
SELECT COUNT(*) FROM tbl_product_category

getProductCategory(8) returned rows: 7
SELECT id, name FROM tbl_product_category INNER JOIN tbl_product_cat_basket AS basket ON id = basket.cat_id WHERE basket.product_id = ? LIMIT 0,1;
SELECT COUNT(*) FROM tbl_product_category

getProductCategory(9) returned rows: 7
SELECT * FROM tbl_store_admin WHERE php_sesskey=?;
SELECT COUNT(*) FROM tbl_store_admin WHERE php_sesskey=?;

SELECT * FROM tbl_menu_cat_basket WHERE menu_id=?;
SELECT COUNT(*) FROM tbl_menu_cat_basket WHERE menu_id=?;

SELECT * FROM tbl_menu_cat_basket WHERE menu_id=?;
SELECT COUNT(*) FROM tbl_menu_cat_basket WHERE menu_id=?;

SELECT * FROM tbl_menu_cat_basket WHERE menu_id=?;
SELECT COUNT(*) FROM tbl_menu_cat_basket WHERE menu_id=?;

SELECT * FROM tbl_menu_cat_basket WHERE menu_id=?;
SELECT COUNT(*) FROM tbl_menu_cat_basket WHERE menu_id=?;

SELECT * FROM tbl_menu_cat_basket WHERE menu_id=?;
SELECT COUNT(*) FROM tbl_menu_cat_basket WHERE menu_id=?;

SELECT * FROM tbl_menu_cat_basket WHERE menu_id=?;
SELECT COUNT(*) FROM tbl_menu_cat_basket WHERE menu_id=?;

SELECT * FROM tbl_menu_cat_basket WHERE menu_id=?;
SELECT COUNT(*) FROM tbl_menu_cat_basket WHERE menu_id=?;

SELECT * FROM tbl_product ORDER BY last_update ASC;
SELECT COUNT(*) FROM tbl_product ORDER BY last_update ASC;

2 个答案:

答案 0 :(得分:2)

你的RegeExp不匹配,它缺少dot-all标志(/总共)。

这会导致.也匹配字符串中的换行符。

答案 1 :(得分:1)

看来你的正则表达式没有返回正确的结果。由于您只是第一次出现FROM并在此之后返回所有内容,为什么不使用stristr

$output = stristr($sql, 'FROM');    // $output includes 'FROM'
if ($output !== false)
{
    $query = $db->prepare("SELECT COUNT(*) {$output}");
    // etc.