program tip

PHP의 mysql 테이블에서 count (*)를 선택하십시오.

radiobox 2020. 9. 13. 10:18
반응형

PHP의 mysql 테이블에서 count (*)를 선택하십시오.


mysql 쿼리 결과의 값과 행을 모두 얻을 수 있습니다.

그러나 나는 쿼리의 단일 출력을 얻기 위해 고군분투하고 있습니다. 예 :

$result = mysql_query("SELECT COUNT(*) FROM Students;");

표시 할 결과가 필요합니다. 그러나 나는 결과를 얻지 못하고 있습니다.

다음 방법으로 시도했습니다.

  1. mysql_fetch_assoc()
  2. mysql_free_result()
  3. mysql_fetch_row()

그러나 나는 실제 값을 표시하는 데 성공하지 못했습니다.


집계 as를 호출 하려면 키워드를 사용하여 집계의 별칭을 지정해야합니다.mysql_fetch_assoc

$result=mysql_query("SELECT count(*) as total from Students");
$data=mysql_fetch_assoc($result);
echo $data['total'];

값만 필요한 경우 :

$result = mysql_query("SELECT count(*) from Students;");
echo mysql_result($result, 0);

$result = mysql_query("SELECT COUNT(*) AS `count` FROM `Students`");
$row = mysql_fetch_assoc($result);
$count = $row['count'];

이 코드를 사용해보십시오.


PDO 사용을 시작하십시오.

mysql_ *는 PHP 5.5.0부터 사용되지 않으며 7에서 완전히 제거 될 예정입니다. 지금 업그레이드하고 사용하기 쉽게 시작하겠습니다.

$dbh = new \PDO($dsn, $user, $password);
$sth = $dbh->prepare('SELECT count(*) as total from Students');
$sth->execute();
print_r($sth->fetchAll());

$num_result = mysql_query("SELECT count(*) as total_count from Students ") or exit(mysql_error());
$row = mysql_fetch_object($num_result);
echo $row->total_count;

mysqli 사용자의 경우 코드는 다음과 같습니다.

$mysqli = new mysqli($db_host, $db_user, $db_pass, $db_name);

$result = $mysqli->query("SELECT COUNT(*) AS Students_count FROM Students")->fetch_array();
var_dump($result['Students_count']);

또는:

$mysqli = new mysqli($db_host, $db_user, $db_pass, $db_name);

$result = $mysqli->query("SELECT COUNT(*) FROM Students")->fetch_array();
var_dump($result[0]);

 $howmanyuser_query=$conn->query('SELECT COUNT(uno)  FROM userentry;');
 $howmanyuser=$howmanyuser_query->fetch_array(MYSQLI_NUM); 
 echo $howmanyuser[0];

너무 많은 시간이 지난 후 :)


$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'];
  }     
 }

이 계산에서 question1 열의 발생 횟수, 그 작업은 완전히


you can as well use this and upgrade to mysqli_ (stop using mysql_* extension...)

$result = mysqli_query($conn, "SELECT COUNT(*) AS `count` FROM `Students`");
$row = mysqli_fetch_array($result);
$count = $row['count'];
echo'$count';

With mysql v5.7.20, here is how I was able to get the row count from a table using PHP v7.0.22:

$query = "select count(*) from bigtable";
$qresult = mysqli_query($this->conn, $query);
$row = mysqli_fetch_assoc($qresult);
$count = $row["count(*)"];
echo $count;

The third line will return a structure that looks like this:

array(1) {
   ["count(*)"]=>string(4) "1570"
}

In which case the ending echo statement will return:

1570

I think there is a typo in your code and you should remove the second to last semi-colon in:

$result = mysql_query("SELECT COUNT(*) FROM Students;");

You need to alias the aggregate using the as keyword in order to call it from mysqli_fetch_assoc

$result=mysqli_query($conn,"SELECT count(*) as total from Students");
$data=mysqli_fetch_assoc($result);
echo $data['total'];

here is the code for showing no of rows in the table with PHP

$sql="select count(*) as total from student_table";
$result=mysqli_query($con,$sql);
$data=mysqli_fetch_assoc($result);
echo $data['total'];

I think it's better answer.

$query = "SELECT count(*) AS total FROM table_name"; 
mysql_select_db('database_name');
$result = mysql_query($query); 
$values = mysql_fetch_assoc($result); 
$num_rows = $values['total']; 
echo $num_rows;

$db  = new PDO('mysql:host=localhost;dbname=java_db', 'root', '') or die(mysql_errno());
$Sql = "SELECT count(*) as 'total' FROM users";
$stmt = $db->query($Sql);
$stmt->execute();
$data = array();
$total = $stmt->fetch(PDO::FETCH_ASSOC);
print '<pre>';
print_r($total);
print '</pre>';

Result:

enter image description here


$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. welcome all


If you only want the count value you could do shorthand:

$cnt = mysql_num_rows(mysql_query('select * from students'));

참고URL : https://stackoverflow.com/questions/6907751/select-count-from-table-of-mysql-in-php

반응형