Php 如何使用SQL查询计数显示行数

Php 如何使用SQL查询计数显示行数,php,html,mysql,Php,Html,Mysql,当我在PHPmyadmin中从blog_posts运行这个查询时,它返回我数据库中的行数,这是我想要的,但是现在我想使用下面的COUNT方法将这个数字显示在我的success.php页面上 以下是我目前的代码: Database.class.php <?php include 'config/config.php'; // Inlude the config file, this is where the database login info is stored in an array

当我在PHPmyadmin中从blog_posts运行这个查询时,它返回我数据库中的行数,这是我想要的,但是现在我想使用下面的
COUNT方法将这个数字显示在我的
success.php
页面上

以下是我目前的代码:
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);
        }


    }



}



 ?>
<?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和MYSQL等都是新手,所以我看到您在$countSQL查询中添加了'as total'。这到底在干什么?感谢您的帮助:)@ifusion这基本上是
别名
所能做的,当您不提供时,结果集将为您提供此
计数(id)
,但是如果您将
别名
计数(id)一样作为总计
,您就可以使用
总计
。这是一个很好的读物:当我添加
返回$this->dbc->impacted_rows()时,在第31行的C:\xampp\htdocs\xampp\oop\3\classes\database.class.php中调用了一个未定义的方法mysqli::impacted_rows()
@ifusion天哪,我编辑了我的答案,它应该是
->受影响的行
属性,而不是方法
->受影响的行()