2016-12-26 118 views
0

在我的網站中,我在很多不同的設置中顯示不同的產品,以確保結果數量正確我想用PHP回顯它們。到目前爲止,我嘗試以我知道的方式迴應他們,但這種方式沒有奏效。迴應SELECT COUNT(*)查詢的結果

我想要回顯的頁面上的代碼包含對頂部的函數文件的require。我曾嘗試:

<?php 
//Call the count_stock() function 
$result_count = count_stock(); 
?> 
<p>Showing 1-9 of <?php echo $result_count['COUNT(*)']; ?> results.</p> 
<p>Showing 1-9 of <?php echo $result_count[0]; ?> results.</p> 
<p>Showing 1-9 of <?php print_r($result_count[0]); ?> results.</p> 

我執行查詢功能:

//create a function to count get_stock 
function count_stock() { 
    global $conn; 
    //query the database to count the data entries in the stock table 
    $sql = 'SELECT COUNT(*) FROM stock'; 
    //use a prepared statement to enhance security 
    $statement = $conn->prepare($sql); 
    $statement->execute(); 
    $result_count = $statement->fetchAll(); 
    $statement->closeCursor(); 
    return $result_count; 
} 
+0

什麼var_dump($ result_count)返回? –

回答

0

你的函數變化:

function count_stock() { 
     global $conn; 
     //query the database to count the data entries in the stock table 
     $sql = 'SELECT COUNT(*) as tot FROM stock'; 
     //use a prepared statement to enhance security 
     $statement = $conn->prepare($sql); 
     $statement->execute(); 
     $result_count = $statement->fetch(); 
     $statement->closeCursor(); 
     return $result_count['tot']; 
    } 
0

變化從

//create a function to count get_stock 
function count_stock() { 
    global $conn; 
    //query the database to count the data entries in the stock table 
    $sql = 'SELECT COUNT(*) FROM stock'; 
    //use a prepared statement to enhance security 
    $statement = $conn->prepare($sql); 
    $statement->execute(); 
    $result_count = $statement->fetchAll(); 
    $statement->closeCursor(); 
    return $result_count; 
} 

//create a function to count get_stock 
function count_stock() { 
    global $conn; 
    //query the database to count the data entries in the stock table 
    $sql = 'SELECT COUNT(*) as total_result FROM stock'; 
    //use a prepared statement to enhance security 
    $statement = $conn->prepare($sql); 
    $statement->execute(); 
    $result = $statement->get_result(); 
    $row = $result->fetch_assoc(); 
    $statement->closeCursor(); 
    return $row['total_result']; 
} 

而且還從

<?php 
//Call the count_stock() function 
$result_count = count_stock(); 
?> 
<p>Showing 1-9 of <?php echo $result_count['COUNT(*)']; ?> results.</p> 
<p>Showing 1-9 of <?php echo $result_count[0]; ?> results.</p> 
<p>Showing 1-9 of <?php print_r($result_count[0]); ?> results.</p> 

改變

<?php 
//Call the count_stock() function 
$result_count = count_stock(); 
?> 
<p>Showing 1-9 of <?php echo $result_count; ?> results.</p> 
0

試試這個

$sql = "SELECT count(*) FROM `stock"; 
$result = $con->prepare($sql); 
$result->execute(); 
$number_of_rows = $result->fetchColumn(); 

上面的代碼示例使用一份聲明中,這是在許多情況下,這可能是不必要的計算行E,所以:

$nRows = $pdo->query('select count(*) from blah')->fetchColumn(); 
echo $nRows; 

參考:http://php.net/manual/en/pdostatement.rowcount.php

注: PDO有PDOStatement對象:: rowCount時(),這顯然不MySQL的。多麼痛苦