MySQL返回PHP关联数组 - 但我无法访问数组数据

时间:2013-10-14 03:41:26

标签: php mysql arrays wordpress associative-array

我在MySQL表中有两条记录。我正在尝试使用SELECT COUNT(*) AS total FROM tableA,但我没有得到我期待的结果。

以下代码将回显Array ( [0] => Array ( [cnt] => 2 ) )

// Count the amount of records in the table
$total = $wpdb->get_results( "SELECT COUNT( * ) AS total FROM tableA", 'ARRAY_A' );

echo "Total Records:" . print_r( $total );

下面的代码没有回音:

// Count the amount of records in the table
$total = $wpdb->get_results( "SELECT COUNT( * ) AS total FROM tableA", 'ARRAY_A' );

echo "Total Records:" . $total[0]['total'];

我该如何简化?我究竟做错了什么?我正在绞尽脑汁,我无法让它发挥作用。

3 个答案:

答案 0 :(得分:1)

尝试这个

$total = $wpdb->get_results( "SELECT COUNT( * ) AS total FROM tableA", 'ARRAY_A' );

echo "Total Records:" . $total[0]['cnt'];

感谢。

答案 1 :(得分:0)

试试这个:

$sql = "SELECT COUNT( * ) AS total FROM tableA";
$sth = $DC->prepare($sql);
$sth->execute();
$result = $sth->fetch(PDO::FETCH_ASSOC);                
echo $result['total'];

答案 2 :(得分:0)

试试这个:

<?php

$numRows = $wpdb->get_var( "SELECT COUNT( * ) AS total FROM tableA");

echo $numRows;

?>