How to check if a MySQL query using the legacy API was successful?

前端 未结 6 1929
栀梦
栀梦 2020-12-01 13:48

How do I check if a MySQL query is successful other than using die()

I\'m trying to achieve...

mysql_query($query);

if(success){
//move         


        
相关标签:
6条回答
  • 2020-12-01 14:19

    This is the first example in the manual page for mysql_query:

    $result = mysql_query('SELECT * WHERE 1=1');
    if (!$result) {
        die('Invalid query: ' . mysql_error());
    }
    

    If you wish to use something other than die, then I'd suggest trigger_error.

    0 讨论(0)
  • 2020-12-01 14:24

    If your query failed, you'll receive a FALSE return value. Otherwise you'll receive a resource/TRUE.

    $result = mysql_query($query);
    
    if(!$result){
        /* check for error, die, etc */
    }
    

    Basically as long as it's not false, you're fine. Afterwards, you can continue your code.

    if(!$result)
    

    This part of the code actually runs your query.

    0 讨论(0)
  • 2020-12-01 14:29

    You can use mysql_errno() for this too.

    $result = mysql_query($query);
    
    if(mysql_errno()){
        echo "MySQL error ".mysql_errno().": "
             .mysql_error()."\n<br>When executing <br>\n$query\n<br>";
    }
    
    0 讨论(0)
  • 2020-12-01 14:31

    mysql_query function is used for executing mysql query in php. mysql_query returns false if query execution fails.Alternatively you can try using mysql_error() function For e.g

    $result=mysql_query($sql)
    

    or

    die(mysql_error());
    

    In above code snippet if query execution fails then it will terminate the execution and display mysql error while execution of sql query.

    0 讨论(0)
  • 2020-12-01 14:32

    put only :

    or die(mysqli_error());

    after your query

    and it will retern the error as echo

    example

    // "Your Query" means you can put "Select/Update/Delete/Set" queries here
    $qfetch = mysqli_fetch_assoc(mysqli_query("your query")) or die(mysqli_error()); 
    
    
    
        if (mysqli_errno()) {
            echo 'error' . mysqli_error();
            die();
        }
    
    0 讨论(0)
  • 2020-12-01 14:40

    if using MySQLi bind_param try to put this line above the query

    mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
    
    0 讨论(0)
提交回复
热议问题