我在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'];
我该如何简化?我究竟做错了什么?我正在绞尽脑汁,我无法让它发挥作用。
答案 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;
?>