How can I know that the query result is 0 records?

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.