准备好的声明; fetch()还是fetch_all()? While语句的问题 [英] Prepared Statement; fetch() or fetch_all()? Issue with While Statement

查看:66
本文介绍了准备好的声明; fetch()还是fetch_all()? While语句的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试调试我准备好的语句代码,但是有一些问题.当使用以下脚本时,我在浏览器中看到测试一"和测试二"的回声,但没有看到测试三".我的-> fetch()语句似乎不起作用.没有错误.

I am trying to de-bug my prepared statement code but having some problems. When using the below script, I see 'test one' and 'test two' echoed in my browser but no 'test three'; my ->fetch() statement doesn't appear to be working. No errors.

if (empty($login_errors)) { // OK to proceed!

echo 'test one';

$pas = get_password_hash($p);
$loginQuery = $dbc->prepare("SELECT id, username, type FROM user WHERE (email=? AND pass=?)");
$loginQuery->bind_param('ss',$e,$pas);
$loginQuery->execute();
$loginQuery->bind_result($id,$username,$type);

echo 'test two';

while($loginQuery->fetch()){

echo 'test three';

$idRow = $id;
$usernameRow = $username;

$_SESSION['user_id'] = $idRow[0];
$_SESSION['username'] = $usernameRow[0];
} 
echo 'test four';
}

我首先想到的是,当仅选择一个字段(一个绑定结果)时使用->fetch().我尝试使用while($loginQuery->fetch_all()){是因为我有三个($loginQuery->bind_result($id,$username,$type);),但这会带回HTTP 500错误.这种想法正确吗? fetch()代表一个,fetch_all()代表多个?

My first thought was that ->fetch() is used when there is only one field being selected (one bind result). I tried using while($loginQuery->fetch_all()){ because i have three ( $loginQuery->bind_result($id,$username,$type);) but this brings back a HTTP 500 error.Is this thinking correct? fetch() for one, fetch_all() for many?

为什么两个提取都不起作用?您能看到我的代码有任何问题吗?

Why do neither fetches work? Can you see any issues with my code?

显示测试四".这将表明查询未返回任何数据.这是令人困惑的,因为在此准备好的语句之前,我使用了串联,并且以下内容可以正常工作:

'test four' is displayed. This would suggest that the query is returning no data. This is confusing because before this prepared statement, I used concatenation and the below worked without any issues:

$q = "SELECT id, username, type, IF(date_expires >= NOW(), true, false) FROM user WHERE (email='$e' AND pass='"  .  get_password_hash($p) .  "')";      
$r = mysqli_query ($dbc, $q);

推荐答案

这种想法正确吗? fetch()代表一个,fetch_all()代表多个?

Is this thinking correct? fetch() for one, fetch_all() for many?

实际上是两个不同对象的两种不同方法.

That's actually two different methods of two different objects.

  • fetch()属于mysqli语句对象,使用丑陋的方法将查询结果分配给全局变量. 可以循环运行.循环是一种您可以多次提取一行的东西.因此,使用fetch()获取许多记录并没有矛盾
  • fetch_all()属于mysqli结果对象,并且只是语法糖,可以为您循环运行fetch_assoc().
  • fetch() belongs to mysqli statement object and uses ugly method of assigning query result to global variables. Can be run in a loop. Loop is a thing where you can do fetch one single row... many times. So, nothing contradicting in using fetch() to get many records
  • fetch_all() belongs to mysqli result object and being just a syntax sugar, running fetch_assoc() for you in a loop.

这篇关于准备好的声明; fetch()还是fetch_all()? While语句的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆