2

我在 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'];

我怎样才能简化这个?我究竟做错了什么?我正在为此绞尽脑汁,但我无法让它发挥作用。

4

3 回答 3

1

试试这个

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

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

谢谢。

于 2013-10-14T06:05:55.473 回答
0

尝试这个:

<?php

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

echo $numRows;

?>
于 2013-10-14T04:57:02.693 回答
0

尝试这个:

$sql = "SELECT COUNT( * ) AS total FROM tableA";
$sth = $DC->prepare($sql);
$sth->execute();
$result = $sth->fetch(PDO::FETCH_ASSOC);                
echo $result['total'];
于 2013-10-14T03:49:30.253 回答