简体   繁体   English

mysqli用while循环准备语句

[英]mysqli prepared statement with while loop

I am trying to do an extremely simple query using mysqli.我正在尝试使用 mysqli 进行非常简单的查询。 It is driving me mad!它快把我逼疯了!

I just want $data to be an array of the values from the sql query.我只希望$data是来自 sql 查询的值的数组。

This is my code...这是我的代码...

$req = $app->request();
$hashtag = $req->get('hashtag');

require_once 'Slim/lib/database.php';

$db = connect_db();

$statement = $db->prepare("SELECT `content` FROM `posts` WHERE `content` LIKE ?");
$newhashtag = '%#' . $hashtag . '%';
$statement -> bind_param("s", $newhashtag);

$statement -> execute();

$statement -> bind_result($result);

while ( $row = mysqli_fetch_array($statement) ) {
    $data[] = $row;
}

print_r($data);

$statement -> close();

I just get an error mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given and it doesn't make a difference using $result or $statement on the fetch_array我只是得到一个错误mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given并且在fetch_array上使用$result$statement没有区别

You can try this:你可以试试这个:

$statement = $db->prepare("SELECT `content` FROM `posts` WHERE `content` LIKE ?");
$newhashtag = "%#$hashtag%";
$statement->bind_param("s", $newhashtag);
$statement->execute();
$result = $statement->get_result();

while ($row = $result->fetch_assoc())
{
    $data[] = $row;
}

This uses the get_result() function , which is used to get the result from a prepared statement.这使用get_result()函数,该函数用于从准备好的语句中获取结果。

This is initialised outside the while loop and assigned to a new variable, in this instance $result .这在 while 循环之外初始化并分配给一个新变量,在本例中$result
Then $result->fetch_assoc() is assigned to $row and can be accessed within the loop.然后$result->fetch_assoc()被分配给$row并且可以在循环内访问。 Accessing each column as a key from the array such that $row["content"] would return the content from each row under that column将每一列作为数组中的键访问,这样$row["content"]将返回该列下每一行的内容

To get the result object from the prepared statement you can use get_result() .要从准备好的语句中获取结果对象,您可以使用get_result() This object can then be iterated with a foreach loop.然后可以使用foreach循环迭代该对象。

$statement->execute();

$result = $statement->get_result();

foreach ($result as $row) {
    print_r($row);
}

If you need to fetch all rows into an array, you can usefetch_all() .如果需要将所有行提取到数组中,可以使用fetch_all()

$statement->execute();

$result = $statement->get_result();

$data = $result->fetch_all(MYSQLI_ASSOC);
print_r($data);

You can also fetch the data by binding each column to a variable.您还可以通过将每列绑定到一个变量来获取数据。 First, you specify a variable you want to be populated usingbind_result() and then you call fetch() to populate that variable.首先,使用bind_result()指定要填充的变量,然后调用fetch()来填充该变量。

$statement->execute();

$statement->bind_result($content);

while ( $statement->fetch() ) {
    // Every time fetch is called a value from the next row will be inserted into $content
    $data[] = $content;
}

print_r($data);

replace these lines: 替换这些行:

while ( $row = mysqli_fetch_array($statement) ) {
   $data[] = $row;
}

with these: 用这些:

while ($row = $statement->fetch()) {
   $data[] = $row;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM