如何使用bind_result与get_result的示例

Ari*_*osh 76 php mysql mysqli prepared-statement

我想看一个如何调用使用bind_resultvs 的示例,以及get_result使用one over另一个的目的是什么.

也是使用每个的利弊.

使用任何一个的限制是什么,是否存在差异.

Ari*_*osh 176

对我而言,决定性因素是我是否使用调用我的查询列*.

使用bind_result()会更好:

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

使用get_result()会更好:

// Use get_result() with fetch_assoc() 
$query2 = 'SELECT * FROM table WHERE id = ?';
Run Code Online (Sandbox Code Playgroud)

实施例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();
Run Code Online (Sandbox Code Playgroud)

实施例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();
Run Code Online (Sandbox Code Playgroud)

正如你所看到的,你不能使用bind_result带*.但是,get_result适用于两者,但bind_result更简单,并拿出一些混乱$row['name'].


bind_result()

优点:

  • 更简单
  • 没必要乱用 $row['name']
  • 用途 fetch()

缺点:

  • 不适用于使用的SQL查询 *

get_result()

优点:

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

缺点:

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

  • 天哪,我希望在将所有内容绑定到`$ row []之前,我找到了这个问答.谢谢你的详细解释!一个警告; 根据手册get_result()是**仅适用于mysqlnd.** (8认同)
  • 对于第二个例子,get_result()对我来说也不起作用 - 保持返回False和errno = 0(没有错误).删除store_result()调用修复它. (3认同)
  • @SableFoste正确!这意味着......你必须弄乱配置...所以...使用bind_result. (2认同)
  • 对于所有 get_result() 方法不起作用的人:http://stackoverflow.com/questions/8321096/call-to-undefined-method-mysqli-stmtget-result (2认同)
  • `bind_result()` 似乎与使用 `*` 的 SQL 查询配合得很好,您只需要知道查询返回哪些列。您在这里看到什么问题? (2认同)