如何使用bind_result与get_result的示例 [英] Example of how to use bind_result vs get_result

查看:118
本文介绍了如何使用bind_result与get_result的示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想看一个示例,该示例说明如何使用bind_resultget_result进行调用,以及在一个与另一个之间使用的目的.

I would like to see an example of how to call using bind_result vs. get_result and what would be the purpose of using one over the other.

使用它们的优缺点.

使用这两种方法的局限性是什么?

What is the limitation of using either and is there a difference.

推荐答案

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

The deciding factor for me, is whether I call my query columns using *.

// Use bind_result() with fetch()
$query1 = 'SELECT id, first_name, last_name, username FROM table WHERE id = ?';

使用get_result()会更好:

Using get_result() would be better for this:

// Use get_result() with fetch_assoc() 
$query2 = 'SELECT * FROM table WHERE id = ?';


使用bind_result()

$query1的示例1


Example 1 for $query1 using bind_result()

$query1 = 'SELECT id, first_name, last_name, username FROM table WHERE id = ?';
$id = 5;

if($stmt = $mysqli->prepare($query)){
   /*
        Binds variables to prepared statement

        i    corresponding variable has type integer
        d    corresponding variable has type double
        s    corresponding variable has type string
        b    corresponding variable is a blob and will be sent in packets
   */
   $stmt->bind_param('i',$id);

   /* execute query */
   $stmt->execute();

   /* Store the result (to get properties) */
   $stmt->store_result();

   /* Get the number of rows */
   $num_of_rows = $stmt->num_rows;

   /* Bind the result to variables */
   $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>';
   }

   /* free results */
   $stmt->free_result();

   /* close statement */
   $stmt->close();
}

/* close connection */
$mysqli->close();

使用get_result()

$query2的示例2

$query2 = 'SELECT * FROM table WHERE id = ?'; 
$id = 5;

if($stmt = $mysqli->prepare($query)){
   /*
        Binds variables to prepared statement

        i    corresponding variable has type integer
        d    corresponding variable has type double
        s    corresponding variable has type string
        b    corresponding variable is a blob and will be sent in packets
   */
   $stmt->bind_param('i',$id);

   /* execute query */
   $stmt->execute();

   /* Get the result */
   $result = $stmt->get_result();

   /* Get the number of rows */
   $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>';
   }

   /* free results */
   $stmt->free_result();

   /* close statement */
   $stmt->close();
}

/* close connection */
$mysqli->close();


如您所见,您不能将bind_result*一起使用.但是,get_result可以同时使用,但是bind_result更简单,并且可以消除$row['name']带来的混乱.


As you can see you can't use bind_result with *. However, get_result works for both, but bind_result is simpler and takes out some of the mess with $row['name'].

优点:

  • 更简单
  • 无需惹恼$row['name']
  • 使用fetch()
  • Simpler
  • No need to mess with $row['name']
  • Uses fetch()

缺点:

  • 不适用于使用*
  • 的SQL查询
  • Doesn't work with SQL query that use *

优点:

  • 使用所有SQL语句
  • 使用fetch_assoc()

缺点:

  • 必须弄乱数组变量$row[]
  • 不那么整齐
  • 需要MySQL本机驱动程序( mysqlnd )
  • Must mess around with array variables $row[]
  • Not as neat
  • requires MySQL native driver (mysqlnd)

这篇关于如何使用bind_result与get_result的示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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