php - 如何使用 SQL 查询 COUNT 显示行数

标签 php html mysql

当我运行此查询时 SELECT COUNT(ID) FROM blog_posts在 PHPmyadmin 中,它返回我的数据库中的行数,这正是我想要的,但现在我希望这个数字显示在我的 success.php 上页面使用 count method下面。

这是我到目前为止的代码: Database.class.php

<?php 
include 'config/config.php'; // Inlude the config file, this is where the database login info is stored in an array

class database {
    private $dbc; // Define a variable for the database connection, it's visabiliatly is set to 'private' so only this class can access it

    function __construct($dbConnection){ 
    // Running the __construct() magic function, 
    // I am passing through a variable into the contruct method which when the object is created will hold the login details from the $dsn array in config.php

        $this->dbc = $dbConnection; // This is saying that the variable $dbc now has the same value as $dbConnection ($dsn array)
        $this->dbc = mysqli_connect($this->dbc['host'], $this->dbc['username'], $this->dbc['password'], $this->dbc['database']); 
        // ^ Running the mysqli_connect function to connect to the database.

        if(mysqli_connect_errno()){ // If there is a problem connecting it will throw and error
                die("Database connection failed" . mysqli_connect_error());
            } else {
                echo "allgood";
            }

    }

    function insert($insertSQL){ // Creating an insert function and passing the $insertSQL variable into it

        mysqli_query($this->dbc, $insertSQL); // Querying the database and running the query that is located in 'success.php'.

        if (mysqli_connect_errno($this->dbc)) { // Will throw an error if there is a problem
            die("Failed query: $insertSQL" . $this->dbc->error);
        }

    }

    function count($countSQL){ // This is the method used for counting
        mysqli_query($this->dbc, $countSQL);

        if (mysqli_connect_errno($this->dbc)) { // Will throw an error if there is a problem
            die("Failed query: $countSQL" . $this->dbc->error);
        }


    }



}



 ?>

Success.php

<?php
include 'classes/database.class.php';
include 'config/config.php';

echo '<h2>Success page</h2>';

$objdb = new database($dsn);

$insertSQL = "INSERT INTO blog_posts VALUES(NULL, 'Test', 'THis is a message')";

$objdb->insert($insertSQL);

$countSQL = "SELECT COUNT(ID) FROM blog_posts";

$objdb->count($countSQL); // Executes the query, now how do I display the result? I have tried 'echo'ing this but it doesn't seem to work



?>

最佳答案

实际上,在查询中添加别名会更好:

$countSQL = "SELECT COUNT(ID) as total FROM blog_posts";
$result = $objdb->count($countSQL);
echo $result['total'];

然后,关于你的方法:

function count($countSQL){ // This is the method used for counting
    $query = mysqli_query($this->dbc, $countSQL);

    if (mysqli_connect_errno($this->dbc)) { // Will throw an error if there is a problem
        die("Failed query: $countSQL" . $this->dbc->error);
    }

    $result = $query->fetch_assoc();
    return $result;
}

其他信息:

在其他方法上放置返回值可能也很好。这样您就会知道它工作正常。

示例:

function insert($insertSQL){ // Creating an insert function and passing the $insertSQL variable into it

    $query = mysqli_query($this->dbc, $insertSQL); // Querying the database and running the query that is located in 'success.php'.

    if (mysqli_connect_errno($this->dbc)) { // Will throw an error if there is a problem
        die("Failed query: $insertSQL" . $this->dbc->error);
    }

    return $this->dbc->affected_rows;

}

所以这里:

$insertSQL = "INSERT INTO blog_posts VALUES(NULL, 'Test', 'THis is a message')";
$insert = $objdb->insert($insertSQL); // so that its easy to test if it indeed inserted
if($insert > 0) {
    // hooray! inserted!
}

关于php - 如何使用 SQL 查询 COUNT 显示行数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26209928/

相关文章:

php - 当日期刚刚保存为 UNIX 时间戳时,针对特定日期的 MySQL 过滤器

php - 使用 MYSQL 和 PHP 的通知系统

mysql - (Laravel) 使用 "Eloquent Relationships"连接 3 个或更多表

javascript - 使用第一个输入填充 HTML 选择标签

mysql - 如何在不中断默认行为的情况下扩展默认的 docker 镜像命令

php - MySql 嵌套 select 与结果同一张表

php - simplexml_load_string - 子节点不显示

php - 数据发送后,MySQL出现空白页

javascript - 单击无需重新加载页面

html - 背景图像不会显示