无效的对象或资源mysqli_stmt错误

时间:2019-03-16 16:30:10

标签: php mysql sql

我试图获取每台计算机的ip地址并插入我网站上的数据库,但是由于某种原因出现以下错误:

Warning: mysqli_stmt_bind_param(): invalid object or resource mysqli_stmt...

Warning: mysqli_stmt_execute(): invalid object or resource mysqli_stmt...

Warning: mysqli_stmt_store_result(): invalid object or resource mysqli_stmt...

Warning: mysqli_stmt_num_rows(): invalid object or resource mysqli_stmt...

Warning: mysqli_stmt_execute(): invalid object or resource mysqli_stmt in..

警告:mysqli_stmt_execute():无效的对象或资源mysqli_stmt位于...

这是我的代码:

<?php
$deviceip=$_SERVER['REMOTE_ADDR'];
//check if ip is already in database
if(isset($deviceip)){
    $sql = "SELECT * FROM users WHERE deviceip=?";
    $resstmt = mysqli_stmt_init($db);
    mysqli_stmt_bind_param($resstmt,"s",$deviceip);
    mysqli_stmt_execute($resstmt);
    //did we get a match?
    mysqli_stmt_store_result($resstmt);
    $resultCheck = mysqli_stmt_num_rows($resstmt);
    if($resultCheck ==0){
        $sql = "INSERT INTO deviceips(deviceip) VALUES (?)";
        $resstmt = mysqli_stmt_init($db);
        mysqli_stmt_prepare($resstmt,$sql);
        mysqli_stmt_bind_param($resstmt,"s",$deviceip);;
        mysqli_stmt_execute($resstmt);
    }
}?>

1 个答案:

答案 0 :(得分:0)

由于您收到许多错误,我想没有与数据库的连接。如果您已发布所有代码,则可能会错过建立连接的机会。 在查询之前尝试以下行:

$db = mysqli_connect(SERVER_DNS_OR_IP, DATABASE_USER, USER_PASSWORD, DATABASE_NAME); if(mysqli_connect_errno()) { ... do some error handling... }

将大写的数据替换为您自己的数据库数据。