解决MySQLi空查询错误

时间:2016-05-10 12:58:20

标签: php database mysqli

收到警告:

  

警告:mysqli :: query():第62行的C:\ wamp \ www \ otp-task \ welcome.php中的空查询

我的数据库连接在这里:

function insertDB($email, $OTP , $phonenumber )
{
    $servername = "localhost";
    $username = "root";
    $password = "";
    $db  = "dbotp";

    $conn = new mysqli($servername, $username, $password, $db);

    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }

    // insert to db and close the connection, default time attribute is 60 mins
    // the table name is onetime and will hold the otp and phonenum and email

    $res = mysqli_query($conn, "INSERT INTO onetime (otpnum,email,phoneNum,action) VALUES ('$OTP' , '$email' , '$phonenumber','success')");


    // $res = "INSERT INTO onetime (otpnum,email,phoneNum,action) VALUES ('$OTP' , '$email' , '$phonenumber', 'success')";


    if ($conn->query($res) === TRUE) {
        echo "New record created successfully";
    } else {
        echo "Error: " . $res . "<br>" . $conn->error;
    }

    $conn->close();
}

1 个答案:

答案 0 :(得分:0)

以下两个是同一事物的不同格式:

$conn->query(...)

mysqli_query($conn, ...)

执行其中一个后,使用它的返回值而不是重新执行查询:

$res =  $conn->query(...)
if ($res) {
    // ...
}

请阅读有关准备好的陈述: http://php.net/manual/en/mysqli-stmt.prepare.php