How do I ensure I caught all errors from MySQLi::multi_query?

前端 未结 3 940
清酒与你
清酒与你 2020-12-03 15:11

The docs for multi_query say:

Returns FALSE if the first statement failed. To retrieve subsequent errors from other statements you have to call mysqli

3条回答
  •  一整个雨季
    2020-12-03 15:27

    Bill Karwin's answer doesn't look very eloquent to me. It seems strange to write separate conditional breaks when the do while loop is already setup to handle breakpoints.

    I haven't tested the following snippet, but you should be able to access the necessary results and errors from it:

    $queries["CURRENT_USER"]="SELECT CURRENT_USER()";
    $queries["CITY_NAME"]="SELECT Name FROM City ORDER BY ID LIMIT 20, 5";
    
    if(mysqli_multi_query($mysqli,implode(';',$queries))){
        do{
            list($current_table,$current_query)=each($queries);
            if($current_table!="CURRENT_USER"){
                printf("-----------------\n");
            }
            if($result=mysqli_store_result($mysqli)){
                if(mysqli_num_rows($result)<1){
                    echo "

    Logic Error @ $current_table Query
    $current_query

    "; }else{ while($row=mysqli_fetch_row($result)){ printf("%s\n",$row[0]); } } mysqli_free_result($result); } } while(mysqli_more_results($mysqli) && mysqli_next_result($mysqli)); }else{ list($current_table,$current_query)=each($queries); } if($error=mysqli_error($mysqli)){ echo "

    Syntax Error @ $current_table Query
    $current_query
    Error: $error

    "; }

提交回复
热议问题