当应该返回更高的数字时,mysql行id返回0

时间:2016-06-19 21:45:49

标签: php mysqli prepared-statement

感谢您花时间阅读本文,并且我提前道歉我的编码是如何混乱的,我试图自学。如果输入的名称,电子邮件或电话与数据库匹配,则在运行检查时获取行的id时遇到错误。我试图获取id,以便我可以将该ID绑定为" owner_id"当我存储它时的消息。

Fatal error: Call to undefined method mysqli_stmt::fetch_assoc() in /var/www/a-website.com/html/inc/db3.php on line 35

第35行是

while($row = $prepared_check->fetch_assoc()){

这是整个php文件。它在另一个php文件中的required_once,并使用

在该php文件中运行
checkRecords($name, $tel2, $email);

无论如何,进入php文件。

 $run_check = 1;

function newInsert($name, $email, $tel, $mysqli){
    $prepared_insert = $mysqli->prepare("INSERT INTO call_list (name, email, tel) VALUES (?, ?, ?)");
    $prepared_insert->bind_param('sss', $name, $email, $tel);
    $prepared_insert->execute();
    $run_check = 1;
}

function storeMessage($id, $message, $mysqli){
    $prepared_message = $mysqli->prepare("INSERT INTO messages (owner_id, message) VALUES (?, ?)");
    $prepared_message->bind_param('ss', $id, $message );
    $prepared_message->execute();
    $prepared_message->close();
    $run_check = 0;
}

function checkRecords($name, $email, $tel, $message){
    $servername = "localhost";
    $username = "a_valid_user";
    $password = "some_valid_password";
    $dbname = "call_list";
    $mysqli = new mysqli($servername, $username, $password, $dbname);
    if($mysqli->connect_errno){
            $_SESSION['result_sql'] = "<div class='alert alert-danger'>Failed to connect to MySQL:(".$mysqli->errno.")". $mysqli->error."</div>";
    }
    $prepared_check = $mysqli->prepare("SELECT * FROM call_list WHERE name = ? OR tel = ? OR email = ?");
    $prepared_check->bind_param('sss', $name, $tel, $email);
    $prepared_check->execute();
    $prepared_check->store_result();
    $nm_rows = $prepared_check->num_rows;
    $prepared_check->bind_result($id, $name, $tel, $email);
    while($row = $prepared_check->fetch_assoc()){
            echo "grabbed ". $nm_rows ." results<br><br>";

            if($nm_rows === 0){
                    echo "trying to insert<br><br>". $name ."<br><br>". $email ."<br><br>". $tel;
                    newInsert($name, $email, $tel2, $mysqli);
            }else{
                    echo "i'm trying to store a message with the owner id of ". $row['id'] ." who should be".$row['name'];
                    echo "<br><br> here's the message: <br><br>". $message ."<br><br><br>";
                    storeMessage($id, $message, $mysqli);
            }
    }

}

1 个答案:

答案 0 :(得分:1)

mysqli声明没有fetch_assoc()方法。相反,您可以使用:fetch()