mysql_query ($sql) return Boolen value or resource ID in the end? How to solve

Source: Internet
Author: User
Tags mysql query
mysql_query ($sql) whether the Boolen value or the resource ID is returned???
PHP Code
  
   
  
   

If SQL is used, the output is OK, the user existresource ID #4
If using SQL2, the output is the user not exist

Declare that the data of id = 222 does not exist in the database, why is there such a result?
With SQL query, return the resource ID, with SQL2 query, return is a Boolean value, why?



------Solution--------------------
The resource ID is returned when the query succeeds, and it doesn't matter how much the result is.
Gets the number of rows for the query result with mysql_num_rows
------Solution--------------------
$result =mysql_query ($sql);

if (! $mysql _num_rows ($result)) {
Echo ("The user not exist");
}else{
Echo ("OK, login Sucsuss);
}
------Solution--------------------
PHP Code
Mysql_query― sends a MySQL Query description resource mysql_query (string $query [, Resource $link _identifier]) mysql_query () to the specified connection identifier Sends a query to the currently active database in the associated server. If Link_identifier is not specified, the previous open connection is used. If there is no open connection, this function attempts to call the mysql_connect () function without arguments to establish a connection and use it. The query results are cached. Note: The query string should not end with a semicolon. mysql_query () returns a resource identifier only for Select,show,explain or DESCRIBE statements, or FALSE if the query is executed incorrectly. For other types of SQL statements, mysql_query () returns TRUE on successful execution and returns FALSE on error. A return value other than FALSE means that the query is legitimate and can be executed by the server. This does not indicate any information about the number of rows affected or returned. It is possible that a query executed successfully but did not affect or return any rows. The following query has syntax errors, so mysql_query () fails and returns False:example #1 mysql_query () example
  
   
     If you do not have permission to access the table referenced in the query statement, mysql_query () also returns FALSE. Assuming the query succeeds, you can call Mysql_num_rows () to see how many rows are returned for the SELECT statement, or call Mysql_affected_rows () to view the corresponding delete,insert,replace or UPDATE How many rows are affected by the statement. Only the Select,show,describe or EXPLAIN statement mysql_query () returns a new result identifier that can be passed to mysql_fetch_array () and other functions that process the result table. After the result set has been processed, the associated resource can be freed by calling Mysql_free_result (), although the memory is freed automatically when the script finishes executing. See Mysql_num_rows (), Mysql_affected_rows (), Mysql_unbuffered_query (), Mysql_free_result (), mysql_fetch_array (), 
 Mysql_fetch_row (), Mysql_fetch_assoc (), mysql_result (), mysql_select_db () and mysql_connect (). 
    
  • 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.