update in mysql_query sometime return null

后端 未结 2 542
挽巷
挽巷 2021-01-26 16:19

I use this code for update a record in mysql
This is my code:



        
相关标签:
2条回答
  • 2021-01-26 16:57

    $result in this case is just a resource, it does not contain the reason.

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

    Source: PHP Manual

    So you can only get TRUE / FALSE from it in your update query. It won't tell you the reason. But as you said it happens when your server is overcrowded so reason probably is "too many connections"

    0 讨论(0)
  • 2021-01-26 16:58

    If mysql_query returned NULL, then that would be a bug on PHP. How do you know that it is actually returning NULL?

    For update statements mysql_query should only return TRUE or FALSE. So your error checking code is fine. As to finding out what went wrong, you will have to call other function - mysql_error() would give you a blurb about what went wrong. So print the value of mysql_error() inside your false block. Like this:

     echo 'failed. SQL Err: '. mysql_error()
    

    Do that and you will probably get a clue to as to how 'record got updated, but return value is false'. It should not have happened.

    0 讨论(0)
提交回复
热议问题