Lệnh MySQL tốt nhất để đếm tổng số hàng trong bảng mà không có điều kiện nào được áp dụng cho nó? Tôi đang làm điều này thông qua PHP, vì vậy có lẽ có một chức năng PHP làm điều này cho tôi? Tôi không biết. Đây là một ví dụ về PHP của tôi:
<?php $con = mysql_connect("server.com","user","pswd"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("db", $con); $result = mysql_query("some command"); $row = mysql_fetch_array($result); mysql_close($con); ?>Hỏi ngày 11 tháng 7 năm 2011 lúc 19:47Jul 11, 2011 at 19:47
1
<?php $con = mysql_connect("server.com","user","pswd"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("db", $con); $result = mysql_query("select count(1) FROM table"); $row = mysql_fetch_array($result); $total = $row[0]; echo "Total rows: " . $total; mysql_close($con); ?>
Jim
18.4K5 Huy hiệu vàng48 Huy hiệu bạc65 Huy hiệu Đồng5 gold badges48 silver badges65 bronze badges
Đã trả lời ngày 11 tháng 7 năm 2011 lúc 19:50Jul 11, 2011 at 19:50
fdainesfdainesfdaines
1.1969 huy hiệu bạc12 Huy hiệu đồng9 silver badges12 bronze badges
3
Sử dụng đếm trong truy vấn MySQL của bạn hoặc thực hiện chọn * từ bảng và làm:
$result = mysql_query("SELECT * FROM table"); $rows = mysql_num_rows($result); echo "There are " . $rows . " rows in my table.";Đã trả lời ngày 11 tháng 7 năm 2011 lúc 19:50Jul 11, 2011 at 19:50
fdainesfdainesJules
1.1969 huy hiệu bạc12 Huy hiệu đồng6 gold badges24 silver badges49 bronze badges
3
Sử dụng đếm trong truy vấn MySQL của bạn hoặc thực hiện chọn * từ bảng và làm:
e.g
<?php $con=mysqli_connect("localhost","my_user","my_password","my_db"); // Check connection if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } $sql="SELECT Lastname,Age FROM Persons ORDER BY Lastname"; if ($result=mysqli_query($con,$sql)) { // Return the number of rows in result set $rowcount=mysqli_num_rows($result); printf("Result set has %d rows.\n",$rowcount); // Free result set mysqli_free_result($result); } mysqli_close($con); ?>
Julesjules
7.0676 Huy hiệu vàng24 Huy hiệu bạc49 Huy hiệu đồng11 gold badges95 silver badges103 bronze badges
<?php $con = mysql_connect("server.com","user","pswd"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("db", $con); $result = mysql_query("select count(1) FROM table"); $row = mysql_fetch_array($result); $total = $row[0]; echo "Total rows: " . $total; mysql_close($con); ?> 1 được sử dụng trong PHP 5 trở lên.Aug 13, 2019 at 14:19
Suraj Raochobela
29.2k11 Huy hiệu vàng95 Huy hiệu bạc103 Huy hiệu Đồng1 silver badge4 bronze badges
Đã trả lời ngày 13 tháng 8 năm 2019 lúc 14:19
$result = mysql_query('SELECT COUNT(1) FROM table'); $num_rows = mysql_result($result, 0, 0);ChobelachobelaJul 11, 2011 at 19:48
1851 Huy hiệu bạc4 Huy hiệu đồngJon Gauthier
Sử dụng <?php $con = mysql_connect("server.com","user","pswd"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("db", $con); $result = mysql_query("select count(1) FROM table"); $row = mysql_fetch_array($result); $total = $row[0]; echo "Total rows: " . $total; mysql_close($con); ?> 2 trong truy vấn <?php $con = mysql_connect("server.com","user","pswd"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("db", $con); $result = mysql_query("select count(1) FROM table"); $row = mysql_fetch_array($result); $total = $row[0]; echo "Total rows: " . $total; mysql_close($con); ?> 3.5 gold badges63 silver badges69 bronze badges
1
Đã trả lời ngày 11 tháng 7 năm 2011 lúc 19:48
$cnt = mysqli_num_rows(mysql_query("SELECT COUNT(1) FROM TABLE")); echo $cnt;Jon Gauthierjon GauthierNov 14, 2013 at 19:48
24.8K5 Huy hiệu vàng63 Huy hiệu bạc69 Huy hiệu ĐồngShridhar
Bạn chỉ có thể làm điều đó trong một dòng như dưới đây:2 silver badges15 bronze badges
6
Đã trả lời ngày 14 tháng 11 năm 2013 lúc 19:48
$result = $connect->query("select * from table where id='$iid'"); $count=$result->num_rows; echo "$count";ShridharshridharMar 11, 2019 at 17:09
3392 Huy hiệu bạc15 Huy hiệu ĐồngToki
Sử dụng Num_Rows để có được số lượng chính xác cho các truy vấn với các điều kiện3 silver badges7 bronze badges
1
Đã trả lời ngày 11 tháng 3 năm 2019 lúc 17:09
TOKITOKI3073 Huy hiệu bạc7 Huy hiệu ĐồngMar 3, 2017 at 10:12
Đối với PHP 5.3 bằng cách sử dụng PDOAsesha George
<?php $staff=$dbh->prepare("SELECT count(*) FROM staff_login"); $staff->execute(); $staffrow = $staff->fetch(PDO::FETCH_NUM); $staffcount = $staffrow[0]; echo $staffcount; ?> 2 gold badges30 silver badges65 bronze badges
1
<?php $con=mysqli_connect("localhost","my_user","my_password","my_db"); // Check connection if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } $sql="SELECT Lastname,Age FROM Persons ORDER BY Lastname"; if ($result=mysqli_query($con,$sql)) { // Return the number of rows in result set $rowcount=mysqli_num_rows($result); echo "number of rows: ",$rowcount; // Free result set mysqli_free_result($result); } mysqli_close($con); ?>
Đã trả lời ngày 3 tháng 3 năm 2017 lúc 10:12
Asesha Georgeasesha GeorgeFeb 24, 2018 at 15:05
1
<?php $conn=mysqli_connect("127.0.0.1:3306","root","","admin"); // Check connection if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } $sql="select count('user_id') from login_user"; $result=mysqli_query($conn,$sql); $row=mysqli_fetch_array($result); echo "$row[0]"; mysqli_close($conn); ?>
2.1682 Huy hiệu vàng30 Huy hiệu bạc65 Huy hiệu Đồng
Đó là cách tốt nhất (tôi nghĩ) để có được số lượng hàng đặc biệt trong MySQL với PHP.Mar 22, 2017 at 10:37
<?php $con = mysql_connect("server.com","user","pswd"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("db", $con); $result = mysql_query("select count(1) FROM table"); $row = mysql_fetch_array($result); $total = $row[0]; echo "Total rows: " . $total; mysql_close($con); ?> 0
Đã trả lời ngày 24 tháng 2 năm 2018 lúc 15:05Jul 11, 2011 at 19:49
Vẫn gặp sự cố, hãy truy cập hướng dẫn của tôi //www.studentstutorial.com/php/php-count-rows.phpTrevor
Đã trả lời ngày 22 tháng 3 năm 2017 lúc 10:372 gold badges32 silver badges40 bronze badges
0