如何使用bind_result与get_result的示例

如何使用bind_result与get_result的示例,第1张

如何使用bind_result与get_result的示例

对我来说,决定性的因素是我是否使用调用查询列

*

bind_result()
为此,使用会更好:
// Use bind_result() with fetch()$query1 = 'SELECt id, first_name, last_name, username FROM table WHERe id = ?';
get_result()
为此,使用会更好:
// Use get_result() with fetch_assoc() $query2 = 'SELECt * FROM table WHERe id = ?';

示例1
$query1
使用
bind_result()
$query1 = 'SELECt id, first_name, last_name, username FROM table WHERe id = ?';$id = 5;if($stmt = $mysqli->prepare($query)){      $stmt->bind_param('i',$id);      $stmt->execute();      $stmt->store_result();      $num_of_rows = $stmt->num_rows;      $stmt->bind_result($id, $first_name, $last_name, $username);   while ($stmt->fetch()) {        echo 'ID: '.$id.'<br>';        echo 'First Name: '.$first_name.'<br>';        echo 'Last Name: '.$last_name.'<br>';        echo 'Username: '.$username.'<br><br>';   }      $stmt->free_result();      $stmt->close();}$mysqli->close();
实施例2对于
$query2
使用
get_result()
$query2 = 'SELECt * FROM table WHERe id = ?'; $id = 5;if($stmt = $mysqli->prepare($query)){      $stmt->bind_param('i',$id);      $stmt->execute();      $result = $stmt->get_result();      $num_of_rows = $result->num_rows;   while ($row = $result->fetch_assoc()) {        echo 'ID: '.$row['id'].'<br>';        echo 'First Name: '.$row['first_name'].'<br>';        echo 'Last Name: '.$row['last_name'].'<br>';        echo 'Username: '.$row['username'].'<br><br>';   }      $stmt->free_result();      $stmt->close();}$mysqli->close();

正如你所看到的,你不能使用

bind_result
*
。但是,
get_result
两者均可使用,但
bind_result
更简单,并且消除了一些麻烦
$row['name']


bind_result()

优点:

  • 更简单
  • 不用惹
    $row['name']
  • 用途
    fetch()

缺点:

  • 不适用于使用
    *

get_result()

优点:

  • 适用于所有SQL语句
  • 用途
    fetch_assoc()

缺点:

  • 必须搞乱数组变量
    $row[]
  • 不那么整齐
  • 需要MySQL本机驱动程序(mysqlnd)


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5042487.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-15
下一篇 2022-11-15

发表评论

登录后才能评论

评论列表(0条)

保存