2016-05-10 49 views
0

警告接收:解決庫MySQLi空查詢錯誤

警告:mysqli的::查詢():空查詢在C:\瓦帕\ WWW \ OTP-任務\的welcome.php在線路62上

我的數據庫連接是在這裏:

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(); 
} 
+0

執行查詢兩次產生該錯誤!註釋這一行'\\ if($ conn-> query($ res)=== TRUE)'並使用'if($ res)' – Saty

回答

0

以下兩個只是同一事物的不同格式:

$conn->query(...) 

mysqli_query($conn, ...) 

一旦執行其中的一個,使用它的返回值,而不是重新執行查詢:

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

也請閱讀準備的語句:http://php.net/manual/en/mysqli-stmt.prepare.php

+0

謝謝@fejese和saty現在這個問題運行良好 – roxor