dotelauv682684 2014-09-01 10:39
浏览 34
已采纳

MySQLi Query准备和检索结果

I've gone through quite a number of stackoverflow threads and I simply can't get it right to retrieve the results after preparing a query. I've tried a number of different solutions and none seem to be able to fetch the associative array after I execute the query

    $mysqli = new MySQLi('localhost', 'root', '', 'prac2');
    $query = $mysqli-> prepare("SELECT * FROM `user` WHERE username=? and password=?");
    $query-> bind_param('ii', $username, $password);
    if($query-> execute())  {
        $query->store_result();
        if ($query -> num_rows > 0) {
            $result = $mysqli->query($query);
            $r = $result -> fetch_array(MYSQLI_ASSOC)['userid'];
            $_SESSION['userid'] = $r;
        }
    }

I've established that sometimes its a case of result containing a boolean for success but I'm still not certain what exactly I'm doing wrong.

UPDATED:

Okay the bind_param works now, but the fetch_assoc keeps giving me the error "Call to a member function fetch_assoc() on a non-object", I even test the result to ensure that it returns true.

$mysqli = new MySQLi('localhost', 'root', '', 'prac2');
$query = $mysqli->prepare("SELECT * FROM user WHERE username=? and password=?");
echo $mysqli->error;
$query-> bind_param('ss', $username, $password);
if($query->execute())  {
    $result = $query -> store_result();
    if($result) {
        while($row = $result -> fetch_assoc()){
            echo $row['userid'];
            $_SESSION['userid'] = $row['userid'];
        }
    }
}
  • 写回答

2条回答 默认 最新

  • douqian1296 2014-09-01 10:41
    关注

    Usernames and password are strings and it should be 's' denoting that corresponding variable has type string. I don't see how usernames and passwords are integers. Bind Param Types

       $query-> bind_param('ss', $username, $password);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Llama如何调用shell或者Python
  • ¥20 eclipse连接sap后代码跑出来空白
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案