如何使用bind_result VS get_result例如何使用bind_result VS g

2019-05-08 19:19发布

我想看看如何使用调用一个例子bind_resultget_result和什么是使用一个比其他的目的。

另外,亲和使用各的利弊。

什么是使用任何的限制,是有区别。

Answer 1:

对我来说,决定因素是,是否使用我打电话给我的查询栏*

使用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()

缺点:

  • 不使用SQL查询工作*

get_result()

优点:

  • 与所有的SQL语句工作
  • 使用fetch_assoc()

缺点:

  • 必须以数组变量勾搭$row[]
  • 还不如利落
  • 需要MySQL本地驱动程序( mysqlnd )


Answer 2:

例如,你可以找到相应的手册页。

虽然亲和缺点都相当简单:

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

无论如何,如果你的想法是为使用功能权限在应用程序代码 - 这种想法是错误的。 只要然而,正如你们有一些方法封装到从查询返回数据,它其实并不重要,要使用哪一个,保存的事实,你将需要十倍的代码来实现bind_result。



Answer 3:

我注意到的主要区别是, bind_result()为您提供了错误2014 ,当您尝试内的其他$语句代码嵌套$语句 ,也就是被取出 (不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; } 


Answer 4:

我想,例如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();


Answer 5:

get_result()现在通过安装MySQL的本地驱动程序(mysqlnd)仅在PHP可用。 在某些环境中,可能无法或需要安装mysqlnd。

尽管如此,你仍然可以使用的mysqli做“SELECT *”查询,并获得与字段名的结果 - 虽然它是略高于使用get_result(更复杂),以及涉及使用PHP的call_user_func_array()函数。 见例如在PHP中如何使用bind_result()代替get_result()该做一个简单的“SELECT *”查询,并输出结果(列名)HTML表格。



文章来源: Example of how to use bind_result vs get_result