dongzhi4239 2012-08-15 22:46
浏览 56

PHP MySQLi准备语句无法返回行

I have been converting a lot of my old MySQL stuff to MySQLi in PHP and am getting a problem on the following code:

### FETCH INCLUDES ###
$player=$_POST['player'];
$password=md5($_POST['password']);
#### DB CONNECTION ####
if(!$mysqli=new mysqli(DBHOST,DBUSER,DBPWD,DBNAME)) {$err=$mysqli->error; print($err); }
$sql="SELECT * FROM accounts WHERE name='?' AND passkey='?'";
if($stmt=$mysqli->prepare($sql)) {
    //$stmt->bind_param('ss',$player,$password);
    $stmt->execute();
    $stmt->store_result();
    if($stmt->num_rows==1) {
        $account=$stmt->fetch_assoc();
        // purely for debugging
        print_r($account);
        if($_SESSION['account']=$account) $account=true;
    } else {
        echo "Failed. Row count: ";
        print($stmt->num_rows);
        echo "<br />";
        $query=str_replace('?','%s',$sql);
        printf($query,$player,$password);
        $account=false;
    }
    $stmt->close();
} else {
    $err=$mysqli->error;
    print($err);
}

I have narrowed down the fault to the query itself. I am getting 0 rows returned, no errors, so I thought I would output the query (the str_replace thing I have going there) and I can use the query to return a row from the database using the same query from PHPMyAdmin

Where am I going wrong?

EDIT

I tried changing the query to a basic one without binding params - "SELECT * FROM table" still get no rows returned. So it is isn't the query itself, it would be something in my order/format of the prepare,execute situation

second edit: I have added the $stmt->store_result() to the code and still returns 0 row count.

Third Edit: I investigated the connection and user settings which seem fine. I can connect via console to the database using the same user and password, and the database name is the same. I am really stumped on this :(

  • 写回答

3条回答 默认 最新

  • douzhong2954 2012-08-15 23:06
    关注

    Add a $stmt->store_result(); after $stmt->execute();, as it seem's it must be called once before $stmt->num_rows... At least they do this in the examples (see http://php.net/manual/en/mysqli-stmt.store-result.php). And they meantion a dependency in the documentation of "num_rows".

    Other ideas: You check for if($stmt->num_rows==1) {, are you sure num_rows is 0? I don't know your database structure for the table "accounts". Is "name" the primary key (or at least a unique index)? If not, there could be multiple columns that match. That's just a quick idea what could be wrong, and cause you looking hours for the problem in your source code. While the problem is somewhere else.

    评论

报告相同问题?

悬赏问题

  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错