当前位置: 首页 > 面试题库 >

如何使用bind_result与get_result的示例

爱唯
2023-03-14
问题内容

我想看一个示例,该示例如何使用bind_resultvs. 进行调用,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)){
   /*
        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();

实施例2对于$query2使用get_result()

$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']

bind_result()

优点:

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

缺点:

  • 不适用于使用 *

get_result()

优点:

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

缺点:

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


 类似资料:
  • 问题内容: 我正在为uni开发一个项目,并且一直在基于的表中使用to上的以下代码: 这在我的测试服务器上运行良好,但是在迁移到大学项目服务器时返回此错误: 一些谷歌搜索建议使用代替,但我不知道如何在表中做到这一点。大多数示例仅显示返回 任何帮助将非常感激 问题答案: 假设您无法使用并且想要一系列设备,则可以执行以下操作: 这将创建一个临时数组并将其每一行中的数据存储在其中,然后将其推入主数组。据我

  • 问题内容: 有人可以举一个简单的例子来说明如何使用函数吗?我用谷歌搜索,但只发现了一些通用的提法,有人说他们成功使用了它,但是我找不到包含代码的单个示例。 例如,我有一个PHP对象。要将响应发送到浏览器,请生成HTML,然后通过将该HTML返回。然后回显结果。 像这样: 假设我想利用这种优化技术将结果发送到浏览器,然后完成一些潜在的漫长过程,例如连接到某些API,例如Facebook API。 我

  • 问题内容: 我刚刚将所有SQL查询更改为使用mysqli的准备好的语句。为了加快此过程,我创建了一个函数,将其替换为。它需要查询,绑定(如“ sdss”)和要传入的变量,然后执行所有准备好的语句。这意味着更改我所有的旧代码很容易。我的函数使用mysqli 返回一个对象。 这意味着我可以从以下位置更改旧代码: 至 这在localhost上可以正常工作,但是我的网络托管服务器没有可用的mysqlnd,

  • 问题内容: 这是我的代码: 我在最后一行收到以下错误: 调用未定义的方法mysqli_stmt :: get_result() 这是conn.php的代码: 如果我写这行: 打印 “未准备的声明” 。如果我直接在IDE中运行查询替换?用值标记,效果很好。请注意,$ conn对象在项目中的其他查询中可以正常工作。 请帮忙....... 问题答案: 请阅读此方法的用户说明: http://php.ne

  • 问题内容: 有没有人有一个使用ast.NodeVisitor的简单示例来遍历Python 2.6中的抽象语法树?对我来说,visit和generic_visit之间的区别尚不清楚,我无法使用Google Codesearch或纯Google找到任何示例。 问题答案: -当然,除非您在子类中覆盖了它- 当被调用以访问class的时,如果存在该方法,则调用,否则。后者再次在类本身的实现中,仅在每个子节

  • 本文向大家介绍Java中的BufferedInputStream与BufferedOutputStream使用示例,包括了Java中的BufferedInputStream与BufferedOutputStream使用示例的使用技巧和注意事项,需要的朋友参考一下 BufferedInputStream  BufferedInputStream 是缓冲输入流。它继承于FilterInputStrea