无效的对象或资源mysqli_stmt

时间:2015-06-14 16:01:24

标签: php mysql mysqli prepared-statement

我想运行多个mysql查询(不兼容)。我正在使用准备好的声明这样做。这是我的代码的要点:

    <?php



if(isset($_GET['username'])&&isset($_GET['activationid'])){

require_once("../database/db_connect.php");

$stmt= $mysqli->stmt_init();
$stmt->prepare("Select username FROM users where username= ? AND activationid= ?");
$username=$_GET['username'];
$activationid=$_GET['activationid'];
$stmt->bind_param("ss",$username,$activationid);
$stmt->execute();
$row=$stmt->get_result()->fetch_array(MYSQLI_ASSOC);

if(!strcmp($row['username'],$username)){
    echo 'you are registered successfully';



$stmt->prepare("UPDATE users SET active=yes where username = ?");
$stmt->bind_param("s",$username);
$stmt->execute();







}
}

?>

和db_connect.php是:

<?php 





define('DBHOST','localhost');
define('DBUSER','root');
define('DBPASS','password');
define('DBNAME','Reminder');

    $mysqli= new mysqli(DBHOST,DBUSER,DBPASS,DBNAME) ;
    if($mysqli->connect_error) { 

    echo $mysqli->mysqli_connect_error();

    }
else {
echo "connected successfully";
}




?>

这给了我这个错误:

  

警告:mysqli_stmt :: bind_param():变量数不匹配   

中预准备陈述中的参数数量

有谁能告诉我我做错了什么?

2 个答案:

答案 0 :(得分:2)

请按照以下方式更改您的代码并检查: -

.run(['$urlRouter', '$timeout', '$state',
  function($urlRouter, $timeout, $state) {

    $http
      .get("modules.json")
      .success(function(data) {
        // here we can use some JSON to configure $stateProvider

    // in example we can use $timeout to simulate that 
    $timeout(function(){

      // here we turn all on again...
      $urlRouter.sync();
      $urlRouter.listen();

      // there could be some decision, driven by $http load
      // we just use some state as default target
      $state.go("parent.child");
    }, 1000)
    ...

第二个相同: -

//$stmt= $mysqli->stmt_init(); comment this line
$stmt = $mysqli->prepare("Select username FROM users where username= ? AND activationid= ?") or die( $mysqli->error);
$username=$_GET['username'];
$activationid=$_GET['activationid'];
$stmt->bind_param("ss",$userid,$activationid);
$stmt->execute();

注意: - 请注意正确定义和设置变量。感谢。

答案 1 :(得分:2)

准备之后和绑定之后(和执行之前),执行

echo $mysqli->error;

就像知道发生什么事一样