使用fetch_array使用绑定参数查询Mysqli(Mysqli query with bind params using fetch_array)

我知道如何使用mysqli bind函数表示数据库中的行时使用fetch_array而不是printf()函数。

我怎么能使用$row->mysqli_fetch_array然后使用$row[0],$row[1]而不是每次我想从数据库打印东西时使用printf()函数?

I know how to use fetch_array instead of printf() function when expressing rows from database using mysqli bind function.

How can I use $row->mysqli_fetch_array and then use $row[0],$row[1] instead of using the printf() function every time I want to print something from database?

最满意答案

从预准备语句返回关联数组,您可以按如下所示跟进此过程。

<?php $category = $_POST['category']; $sql = "select id, name from items where category = ?"; $stmt = $connection->prepare($sql); $stmt->bind_param('s', $category); if($stmt->execute()) { $result = $stmt->get_result(); $a = $result->fetch_array(MYSQLI_ASSOC); // this does work :) } else { error_log ("Didn't work"); } ?>

您可以使用while循环从关联数组中打印出值。

while($a = $result->fetch_array(MYSQLI_ASSOC)) { echo 'Id: '. $a['id']; echo '<br>'; echo 'Name: '.$a['name']; }

输出:

Id: 1 Name: Example

Returning an associative array from a prepared statement you can follow up the procedure like this as follows.

<?php $category = $_POST['category']; $sql = "select id, name from items where category = ?"; $stmt = $connection->prepare($sql); $stmt->bind_param('s', $category); if($stmt->execute()) { $result = $stmt->get_result(); $a = $result->fetch_array(MYSQLI_ASSOC); // this does work :) } else { error_log ("Didn't work"); } ?>

You can use while loop for printing up the value over from the associative array.

while($a = $result->fetch_array(MYSQLI_ASSOC)) { echo 'Id: '. $a['id']; echo '<br>'; echo 'Name: '.$a['name']; }

Output:

Id: 1 Name: Example使用fetch_array使用绑定参数查询Mysqli(Mysqli query with bind params using fetch_array)

我知道如何使用mysqli bind函数表示数据库中的行时使用fetch_array而不是printf()函数。

我怎么能使用$row->mysqli_fetch_array然后使用$row[0],$row[1]而不是每次我想从数据库打印东西时使用printf()函数?

I know how to use fetch_array instead of printf() function when expressing rows from database using mysqli bind function.

How can I use $row->mysqli_fetch_array and then use $row[0],$row[1] instead of using the printf() function every time I want to print something from database?

最满意答案

从预准备语句返回关联数组,您可以按如下所示跟进此过程。

<?php $category = $_POST['category']; $sql = "select id, name from items where category = ?"; $stmt = $connection->prepare($sql); $stmt->bind_param('s', $category); if($stmt->execute()) { $result = $stmt->get_result(); $a = $result->fetch_array(MYSQLI_ASSOC); // this does work :) } else { error_log ("Didn't work"); } ?>

您可以使用while循环从关联数组中打印出值。

while($a = $result->fetch_array(MYSQLI_ASSOC)) { echo 'Id: '. $a['id']; echo '<br>'; echo 'Name: '.$a['name']; }

输出:

Id: 1 Name: Example

Returning an associative array from a prepared statement you can follow up the procedure like this as follows.

<?php $category = $_POST['category']; $sql = "select id, name from items where category = ?"; $stmt = $connection->prepare($sql); $stmt->bind_param('s', $category); if($stmt->execute()) { $result = $stmt->get_result(); $a = $result->fetch_array(MYSQLI_ASSOC); // this does work :) } else { error_log ("Didn't work"); } ?>

You can use while loop for printing up the value over from the associative array.

while($a = $result->fetch_array(MYSQLI_ASSOC)) { echo 'Id: '. $a['id']; echo '<br>'; echo 'Name: '.$a['name']; }

Output:

Id: 1 Name: Example