在 PHP 中显示 SELECT COUNT(*) 的结果

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21301238/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:01:02  来源:igfitidea点击:

Display the results of a SELECT COUNT(*) in PHP

phpsqlcount

提问by tgcowell

Current trying to display the results of a SELECT COUNT(*)from SQL within my website. I'm 100% new to PHP and SQL so understand this must be the basics! If anyone could recommend a good book or website to learn that would also be great.

当前尝试SELECT COUNT(*)在我的网站中显示来自 SQL的结果。我是 PHP 和 SQL 的 100% 新手,所以理解这必须是基础知识!如果有人可以推荐一本好书或网站来学习,那也很棒。

Here is my current code:

这是我当前的代码:

<?php
include_once 'includes/db_connect.php';
include_once 'includes/functions.php';

sec_session_start();
$sql = ("SELECT COUNT(*) FROM project_directory");
$result = mysql_fetch_array($sql);
?>

<?php echo $result ; ?> 

The results are 28 and work if i run the following within the SQL box from phpMyAdmin

结果是 28,如果我在 phpMyAdmin 的 SQL 框中运行以下内容,则可以工作

SELECT COUNT(*) FROM project_directory

Appreciate anyone help or advice.

感谢任何人的帮助或建议。

采纳答案by Jason W

you did not execute the query using mysql_query() function.

您没有使用 mysql_query() 函数执行查询。

you need to do like this

你需要这样做

<?php
include_once 'includes/db_connect.php';
include_once 'includes/functions.php';

sec_session_start();

$sql = ("SELECT COUNT(*) FROM project_directory");
$rs = mysql_query($sql);
 //-----------^  need to run query here

 $result = mysql_fetch_array($rs);
 //here you can echo the result of query
 echo $result[0];

?>

<?php echo $result[0]; ?> 

Note: if you have started learning PHP/Mysql then try to use mysqli_* functions. mysql_ will be deprecated in future PHP versions.

注意:如果您已经开始学习 PHP/Mysql,请尝试使用 mysqli_* 函数。mysql_ 将在未来的 PHP 版本中被弃用。

回答by Soosh

$resultis an array, so you should use $result[0]or $result[0][0]

$result是一个数组,所以你应该使用$result[0]$result[0][0]

if you use print_r($result)you will see the structure of your array and which once you should use.

如果您使用,print_r($result)您将看到数组的结构以及您应该使用的结构。

also you did not use mysql_query($sql).
you should use it like:

你也没有使用mysql_query($sql).
你应该像这样使用它:

$result = mysql_query($sql);
$out = mysql_fetch_array($result);
print($out[0][0]); // or print($out[0]);

回答by Sandy Marwal

Hello , if you are fresher then you can read w3schools.com enjoy

你好,如果你是新人,那么你可以阅读w3schools.com享受

<?php
include_once 'includes/db_connect.php';
include_once 'includes/functions.php';

$sql = "SELECT COUNT(*) as count FROM project_directory";
$result = mysql_num_rows($sql);
$my=$result['count'];
echo $my;
?>

回答by Pushkar Pokharkar

$abc="SELECT count(*) as c FROM output WHERE question1=4";
$result=mysqli_query($conn,$abc);
if($result)
{
    while($row=mysqli_fetch_assoc($result))
    {
        echo $row['c'];
    }       
}

ITS works completely

ITS 完全有效

回答by Samithe Adhikari

$qry_appr = "SELECT COUNT(*) FROM comments WHERE admin_panel_id ='$id' AND status = 'ON'";
$qry_data = mysqli_query($con, $qry_appr);
$approve_count = mysqli_fetch_array($qry_data);
$toatalCount = array_shift($approve_count);
echo $toatalCount;

This will also fine but this is do what returning 0 index value by shifting fetch array.this can use without alias. welcome all

这也可以,但这是通过移动获取数组来返回 0 索引值。这可以在没有别名的情况下使用。欢迎大家

回答by Bhoomi

You have two options.

你有两个选择。

Get the result from the resource returned by the count query:

从count查询返回的资源中获取结果:

      $resource = mysql_query("SELECT COUNT(col) FROM table");
      $count = mysql_result($resource,0);

Or, get the number of rows from the resource returned by the query (without count).

或者,从查询返回的资源中获取行数(不计)。

      $resource = mysql_query("SELECT col FROM table WHERE col IS NOT NULL");
      $count = mysql_num_rows($resource);

I would recommend that you use the first, the reason being is that it is unnecessary to extract all the data from the table when you only need the count.

我建议您使用第一个,原因是当您只需要计数时,没有必要从表中提取所有数据。

回答by Agha Umair Ahmed

if you are new in php and mysql try to use mysqli not mysql

如果您是 php 和 mysql 的新手,请尝试使用 mysqli 而不是 mysql

<?php
include_once 'includes/db_connect.php';
include_once 'includes/functions.php';

$sql = "SELECT COUNT(*) FROM project_directory";
$query = mysql_query($sql);
$result = mysql_num_rows($query);
echo $result ;
?>