When mysql_query returns false

后端 未结 5 1089
南旧
南旧 2020-12-30 10:00

Aside from writing the wrong query and not having permissions to access a table, when mysql_query returns false? Are there any other cases?

相关标签:
5条回答
  • 2020-12-30 10:13

    See the reference guide:

    For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error.

    For other type of SQL statements, INSERT, UPDATE, DELETE, DROP, etc, mysql_query() returns TRUE on success or FALSE on error.

    The returned result resource should be passed to mysql_fetch_array(), and other functions for dealing with result tables, to access the returned data.

    Use mysql_num_rows() to find out how many rows were returned for a SELECT statement or mysql_affected_rows() to find out how many rows were affected by a DELETE, INSERT, REPLACE, or UPDATE statement.

    mysql_query() will also fail and return FALSE if the user does not have permission to access the table(s) referenced by the query.

    http://php.net/manual/en/function.mysql-query.php

    Edit: Clarification of what those errors actually are.

    So we have list of things that can return false:

    • When a MySQL statement which returns a resultset gets an error
    • When a MySQL statement which doesn't return anything gets an error
    • When a user does not have MySQL permission to access a table reference

    In my opinion the first 2 are the ones that are a bit diffuse. What are the possible errors? There are 59 different client errors you can get from MySQL. These are more system related errors which we can presume that php will handle and probably wrap into a smaller amount of abstract errors.

    Except for those client errors you have a set of more abstract errors which you can encounter during usage which is more related to using the actual API inside the application rather than the raw access to the MySQL server. Those are:

    • Access denied
    • Can't connect to [local] MySQL server
    • Lost connection to MySQL server
    • Client does not support authentication protocol
    • Password Fails When Entered Interactively
    • Host 'host_name' is blocked
    • Too many connections
    • Out of memory
    • MySQL server has gone away
    • Packet too large
    • Communication Errors and Aborted Connections
    • The table is full
    • Can't create/write to file
    • Commands out of sync
    • Ignoring user
    • Table 'tbl_name' doesn't exist
    • Can't initialize character set
    • Table corruption issues
    • Syntax related issues

    Here are the references of what I just said:

    • List of the client errors
    • List of the common errors dealing with the API
    • References about query related issues
    • Table related issues
    • Other issues related to known bugs
    0 讨论(0)
  • 2020-12-30 10:13

    Thank you for your comment.
    I am sure there is absolutely no need to dig into such details.

    A thing you really need is error message which you can read and thus get informed of the particular problem.

    So, it seems that mysql_error() function is really what you're looking for.

    $res = mysql_query($sql) or trigger_error(mysql_error()." in ".$sql);
    

    looks sufficient enough.

    0 讨论(0)
  • 2020-12-30 10:17

    Unfortunately, the reference guide is not very specific when it comes to what are possible ways to trigger errors. Besides syntactically invalid statements and not having permissions, exceeding the limit for creating new statements is a possible error that might pop up if you are experiencing high load.

    0 讨论(0)
  • 2020-12-30 10:21

    I had this error and mysql_error() returned "MySQL server has gone away". My query was a very simple query selecting * from a very small table. When I changed "Select *" to select the two columns I wanted it resolved the problem. "Select * " is lazy coding I know but I never dreamed it would cause the query not to run at all!

    0 讨论(0)
  • 2020-12-30 10:31

    UPDATE query will return false if the query is properly written but no rows affected, ex:

    UPDATE `table` SET `id`=1 WHERE `id`=1
    
    0 讨论(0)
提交回复
热议问题