2013-10-22 2 views
0

저는 PHP가 좋지는 않지만 다른 유사한 언어를 알고 있습니다.MySQL - 테이블의 행 수를 반환합니다.

이제 테이블에있는 행 수를 얻으려고합니다. 여기 내 코드가있다.

$result = mysql_query("SELECT COUNT(*) FROM list"); 
$num_rows = mysql_num_rows($result); 
echo $num_rows; 

이제이 코드는 실제로 3 일 때 1을 반환합니다. 왜 그럴까요? 도움 주셔서 감사합니다.

답변

0

확인이를

$result = mysql_fetch_array(mysql_query("SELECT COUNT(*) as count FROM list")); 
echo $result['count']; 
0

다음과 같이하십시오 :

$result = mysql_query("SELECT COUNT(*) FROM list"); 
$num_rows = mysql_fetch_object($result); 
print_r($num_rows); 

참고 : mysql_로 * 기능이 사용되지 않습니다. 따라서 권장하지 않습니다.

0
<?php 
$con=mysqli_connect("example.com","peter","abc123","my_db"); 
// Check connection 
if (mysqli_connect_errno()) 
    { 
    echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
    } 

$result = mysqli_query($con,"SELECT COUNT(*) as total_row FROM list"); 

echo "<table border='1'> 
<tr> 
<th>total_row</th> 
</tr>"; 

while($row = mysqli_fetch_array($result)) 
    { 
    echo "<tr>"; 
    echo "<td>" . $row['total_row'] . "</td>"; 
    echo "</tr>"; 
    } 
echo "</table>"; 

mysqli_close($con); 
?> 
0
$result = mysql_query("SELECT COUNT(*) as total FROM list"); 
$num_rows = mysql_num_rows($result); 
echo $num_rows; // output 1; 

$rows = mysql_fetch_object($result); 
echo $rows->total; // output 3; 
0

당신은 다음과 같이 수행해야합니다

$result = mysql_query("SELECT * FROM list"); 
$num_rows = mysql_num_rows($result); 
echo $num_rows; 

당신이 COUNT을 통해 행의 총 수를 얻고 싶은 경우에, 당신은 다음과 같이 수행 할 수 있습니다

$result = mysql_query("select count(*) FROM list"); 
$row = mysql_fetch_array($result); 
echo $row[0]; 
0

그 후 만 1

를 참조 반환 된 행의 수는 하나의 열에서 계산 반환 : 만 조회 그것은 하나 개의 행을 반환

$result = mysql_query("SELECT * FROM list"); 
$num_rows = mysql_num_rows($result); 
echo $num_rows; 
0

을의 때문에 아래 쿼리는 하나의 값, 즉 행의 수를 반환합니다.

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

난이 도움이 될 것입니다 생각 :

$result = mysql_query("SELECT COUNT(*) FROM list"); 
$num_rows = $result->fetchColumn(); 
echo $num_rows; 
관련 문제