簡體   English   中英

在計算mysql表的總行數時是否可以避免使用“ AS”參數? (php)

[英]Is it possible to avoid the “AS” parameter when counting total rows of a mysql table? (php)

現在,我使用以下代碼來計算表的總行數:

$sql = "SELECT COUNT(*) AS Total FROM MyTable";
$result = mysqli->query($sql);
$numberOfRows = mysqli_fetch_object($result);
$numberOfRows = $numberOfRows->Total;

我嘗試轉儲當我在查詢中不使用“ AS”參數並在互聯網上進行搜索時得到的不同結果,但是盡管有很多示例,但我發現它們都沒有顯示代碼不使用“ AS”參數直接檢索結果。

...

從收到的答案和評論中,我嘗試了這兩個提供預期結果的代碼塊:

$sql = "SELECT COUNT(*) FROM MyTable";
$result = mysqli->query($sql);

然后:使用獲取數組:

$numberOfRows = $result->fetch_array($result);
$numberOfRows = $numberOfRows[0];

使用提取assoc:

$numberOfRows = $result->fetch_assoc($result);
$numberOfRows = $numberOfRows["COUNT(0)"];

在性能方面,我發現訪存數組的性能略好一些(嘗試不使用操作碼)。

如果不使用AS分配別名,則輸出中的列名稱將為COUNT(*) 然后,您應該可以使用以下方法檢索它:

$numberOfRows = $numberOfRows->{"COUNT(*)"}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM