gpt4 book ai didi

php - 当我运行数据库搜索时,如何让 mysqli_fetch_row 返回多行

转载 作者:行者123 更新时间:2023-11-29 21:48:56 25 4
gpt4 key购买 nike

我正在使用表单运行简单的数据库搜索。我只对数据库中的一个表运行搜索,但只运行一行。如果搜索出现两种或多种可能性,则不会显示其中任何一种。我怎样才能让它在应该显示的时候显示多行?

<?php

$servername = "localhost";
$username = "xxx";
$password = "xxx";
$dbname = "oldga740_SeniorProject";


// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}


// If there is a search variable try to search database
if(isset($_POST['search'])) {
$searchq = $_POST['search'];
$searchq = preg_replace("#[^0-9a-z]#i","",$searchq);
$sql = "SELECT * FROM Customers WHERE Client LIKE '%$searchq%'";

if ($result = $conn->query($sql)) {

/* fetch object array */
while ($row = $result->fetch_row()) {
printf ("%s (%s)\n", $row[0], $row[1]);
}

/* free result set */
$result->close();
}





}
?>

<html>

<head>

</head>

<body>

<form action="Index.php" method="post">
<input type="text" name="search" placeholder="Search...." />
<input type="submit" value=">>" />
</form>



</body>

</html>

最佳答案

您可以使用$result->fetch_all(MYSQLI_ASSOC)来获取行数组(关联数组的数组)。

示例:

// This will display all the rows returned by your SQL query.
$elements = $result->fetch_all(MYSQLI_ASSOC);
foreach($elements as $row) {
var_dump($row);
echo "<br/>";
}

关于php - 当我运行数据库搜索时,如何让 mysqli_fetch_row 返回多行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33848712/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com