如何使用bind_result与get_result的示例

问题描述 投票:70回答:5

我想看一个如何使用bind_resultget_result进行调用的示例,以及使用one over另一个的目的是什么。

也是使用每个的利弊。

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

php mysql mysqli prepared-statement
5个回答
165
投票

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

使用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 = ?';

使用$query1bind_result()的示例1

$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();

使用$query2get_result()的示例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']


bind_result()

优点:

  • 更简单
  • 无需惹恼$row['name']
  • 使用fetch()

缺点:

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

get_result()

优点:

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

缺点:

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

2
投票

您可以在相应的手册页上找到示例。

虽然赞成和缺点很简单:

  • get_result是处理结果的唯一理智方式
  • 但它并不总是可用,你的代码必须使用丑陋的bind_result进行后备。

无论如何,如果你的想法是在应用程序代码中使用任何一个函数 - 这个想法是错误的。然而,只要你将它们封装在一些方法中以从查询中返回数据,那么使用哪一个并不重要,除了你需要十倍的代码来实现bind_result这一事实。


1
投票

我注意到的主要区别是bind_result()给你错误2014,当你试图在其他$ stmt中编译嵌套的$ stmt时,正在获取(没有mysqli::store_result()):

准备失败:(2014)命令不同步;你现在不能运行这个命令

例:

  • 主代码中使用的函数。 function GetUserName($id) { global $conn; $sql = "SELECT name FROM users WHERE id = ?"; if ($stmt = $conn->prepare($sql)) { $stmt->bind_param('i', $id); $stmt->execute(); $stmt->bind_result($name); while ($stmt->fetch()) { return $name; } $stmt->close(); } else { echo "Prepare failed: (" . $conn->errno . ") " . $conn->error; } }
  • 主要代码。 $sql = "SELECT from_id, to_id, content FROM `direct_message` WHERE `to_id` = ?"; if ($stmt = $conn->prepare($sql)) { $stmt->bind_param('i', $myID); /* execute statement */ $stmt->execute(); /* bind result variables */ $stmt->bind_result($from, $to, $text); /* fetch values */ while ($stmt->fetch()) { echo "<li>"; echo "<p>Message from: ".GetUserName($from)."</p>"; echo "<p>Message content: ".$text."</p>"; echo "</li>"; } /* close statement */ $stmt->close(); } else { echo "Prepare failed: (" . $conn->errno . ") " . $conn->error; }

0
投票

我认为示例2只会像这样工作,因为store_result和get_result都从表中获取信息。

所以删除

/* Store the result (to get properties) */
$stmt->store_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();

0
投票

get_result()现在只能通过安装MySQL本机驱动程序(mysqlnd)在PHP中使用。在某些环境中,可能无法或不希望安装mysqlnd。

尽管如此,您仍然可以使用mysqli来执行'select *'查询,并使用字段名称获取结果 - 尽管它比使用get_result()稍微复杂一些,并涉及使用php的call_user_func_array()函数。请参阅How to use bind_result() instead of get_result() in php上的示例,该示例执行简单的“select *”查询,并将结果(带有列名称)输出到HTML表。

© www.soinside.com 2019 - 2024. All rights reserved.