How can I know that the query result is 0 Records? I want to check whether this record exists in the database before inserting the record, in fact, my database table does not have a record that meets certificatetype = 'hdfj '$ SQL = & quot; select * fromcustomerswherecertific. how can I know that the query result is 0 records?
I want to check whether this record exists in the database before inserting the record. In fact, the record in my database table does not meet certificatetype = 'hdfj '.
$ SQL = "select * from MERS where certificatetype = 'hdfj '";
$ Result = mysql_query ($ SQL );
If (! $ Result)
Die mysql_error ();
Else
{
// What should I do to know if there is a record in the database?
// Why do the two statements below output 1?
Echo count (mysql_fetch_row ($ result ));
Echo count ($ result );
}
------ Solution --------------------
Mysql_num_rows () gets the number of rows in the result set.
1 is returned because of the special count. If the database does not have this record, mysql_fetch_row ($ result) returns false. While
$ Result = count (false );
// $ Result = 1
------ Solution --------------------
Echo mysql_num_rows ($ result) // returns the number of queried rows.
------ Solution --------------------
Discussion
Mysql_num_rows () gets the number of rows in the result set.
1 is returned because of the special count. If the database does not have this record, mysql_fetch_row ($ result) returns false. While
$ Result = count (false );
// $ Result = 1
------ Solution --------------------
PHP code
// What should I do to know if there is a record in the database? Echo mysql_affected_rows (); // or echo mysql_num_rows ($ result );
------ Solution --------------------
The first parameter in the count () function should be array or object. "If var is not an array or..., 1 will be returned." refer to the PHP manual:
Http://cn2.php.net/manual/en/function.count.php
------ Solution --------------------
Mysql_num_rows is the count
Mysql_fetch_array he mysql_fetch_rows returns an array