且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在php的mysql查询中获取有用的错误消息?

更新时间:2023-01-29 21:32:50

在else块中,您可以看到如何捕获MySQL错误并将其显示在警报中.

In the else-block you can see, how you can catch up MySQL-errors and display them in an alert.

if(isset($_POST['btnSubmit'])) {
    $name = mysqli_real_escape_string($conn,$_POST["name"]);
    $sql = "INSERT INTO isodetail(title) VALUES ('{$name}')";
    $run = mysqli_query($conn, $sql);

    if($run) {
        echo "<script>alert('Certi Added Successfully');window.open('isocerti.php','_self');</script>";
    } else {
        $error = addslashes(mysqli_error($conn));
        echo "<script>alert('An Error occur: {$error}');</script>";
    }
}

参考: MySQLi错误: http://php.net/manual/en/mysqli.error. php
转义特殊字符: http://php.net/manual/en/function.addslashes. php (在此示例中完成工作)

References: MySQLi-Error: http://php.net/manual/en/mysqli.error.php
Escape special characters: http://php.net/manual/en/function.addslashes.php (it does the job for this example)