|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
|
|
#1
|
|||
|
|||
|
How can I find out the number of rows in the result set from a Select statement?
|
|
#2
|
|||
|
|||
|
$mysql_link=mysql_connect("hostname", "username", "password");
mysql_select_db("DB_NAME"); $query="select * from table"; $result=mysql_query($query, $mysql_link); $affected_rows=mysql_affected_rows($mysql_link); . . . // $affected_rows contains the number of rows returned from the query |
|
#3
|
|||
|
|||
|
Actually, affected_rows is only valid after an update.
Here you want: $num_rows=mysql_num_rows($result); |
|
#4
|
|||
|
|||
|
Uh, no....mysql_affected_rows() works after a select statement, too.
|
|
#5
|
|||
|
|||
|
From the PHP manual:
----------------------------------- mysql_affected_rows mysql_affected_rows -- Get number of affected rows in previous MySQL operation Description int mysql_affected_rows(int [link_identifier] ); mysql_affected_rows() returns the number of rows affected by the last INSERT, UPDATE or DELETE query on the server associated with the specified link identifier. If the link identifier isn't specified, the last opened link is assumed. If the last query was a DELETE query with no WHERE clause, all of the records will have been deleted from the table but this function will return zero. This command is not effective for SELECT statements, only on statements which modify records. To retrieve the number of rows returned from a SELECT, use mysql_num_rows(). --------------------------------- Note the terminology: "not effective for SELECT statements...". It WILL work in some instances depending on the query and the versions of PHP and MySQL involved, BUT is UNRELIABLE and should not be used. |
![]() |
| Viewing: Dev Shed Forums > Databases > MySQL Help > Counting resulting rows in Select statement |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|