gpt4 book ai didi

php - 在 PHP 中搜索名称

转载 作者:行者123 更新时间:2023-12-03 21:00:41 24 4
gpt4 key购买 nike

您如何搜索名称或关键字之类的内容并期望得到很多结果?

例如,当您搜索 Mark 时,
你期待这样的:
马克扎克伯格
马克哈米尔
马克菲施巴赫
...

但是当我进行查询时,它只为我显示一个。

我已经尝试使用许多不同的 SQL 子句和其他内容,但仍然给出了一个结果。顺便说一下,我使用 PDO 作为我的数据库处理程序。

这是我的代码:

<?php
require_once '../../assets/conn.db.php';
require_once '../../assets/init.php';
require_once '../../assets/main.func.php';

if($_POST){
$namesearch = $_POST['name'];

$sql = "SELECT * FROM `student_info` WHERE username LIKE ? OR username LIKE ?";
$checkstmt = $conn->prepare($sql);
$checkstmt->execute(array("%{$namesearch}%","%{$namesearch}%"));
$result = $checkstmt->fetch(PDO::FETCH_ASSOC);

if($result){
echo "
<table border='1' class='text-align: center'>
<tr>
<th>Name</th>
<th>Phone</th>
<th>Address</th>
<th>Username</th>
<th>Action</th>
</tr>
<tr>
<td>{$result['name']}</td>
<td>{$result['phone']}</td>
<td>{$result['address']}</td>
<td>{$result['username']}</td>
<td><a href='grades.php?user=".$result['StudentID']."'>+</a> Add Grades</td>
</tr>
</table> <br>
";
}
}

?>

<form method="post">
<input type="text" name="name" placeholder="Search for a student">
<input type="submit" value="Search!">
</form>

最佳答案

sql 查询是正确的,要输出结果的所有元素,您必须遍历 $result。

例子:

while($data = $result->fetch_array())
{
echo $result["name"];
}

关于php - 在 PHP 中搜索名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41872458/

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