【发布时间】:2019-07-19 00:35:12
【问题描述】:
请问各位,我的代码有什么问题,我尝试从我的数据库中获取搜索数据,如下所示
学生资料如下
注册号:全名:教师:程序:级别:组。
在 HTML 搜索页面上
<html>
<h2> Enter your matric number to connect to others studying your course </h2>
<form action="demo.php" method="post">
<b> ReG </b><input type="text" Name="find">
<input type="submit" value="Submit" />
</form>
</html>
PHP 端
<table border="1">
<tr>
<th>Student Full Name</th>
<th> Faculty</th>
<th> Program</th>
<th> Entry Year</th>
<th> Study Group</th>
<th> Group Members Contact</th>
<th> Group Leader Contacts</th>
</tr>
<?php
$conn=mysqli_connect("localhost", "root", "", "student");
// Check connection
if($conn=== false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
$q = $_POST['find'];
if ($q == "")
{
echo "<p>You forgot to enter a search term!!!";
exit;
}
$sql = "SELECT * FROM study_circle WHERE matric LIKE $q ";
$result = mysqli_query($conn, $sql);
if ($result)
{
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>";
echo $row['full_name'];
echo "</td>";
echo "<td>";
echo $row['faculty'];
echo "</td>";
echo "<td>";
echo $row['program'];
echo "</td>";
echo "<td>";
echo $row['entry_year'];
echo "</td>";
echo "<td>";
echo $row['study_group'];
echo "</td>";
echo "<td>";
echo $row['group_members'];
echo "</td>";
echo "<td>";
echo $row['group_leader'];
echo "</td>";
echo "</tr>";
echo "<br/>";
}
} else {
echo "0 results";
}
mysqli_close($conn);
?>
每次我尝试它都会带来 Empty 结果,即使我已经填充了数据库
现在它在我的本地主机系统上,请任何好心人帮忙,我只是编程新手。我很乐意解决这个问题
【问题讨论】:
-
也许,$q 是一个字符串。
-
var_dump($row)看看它显示了什么。如果它显示null或[](空数组),则您的SQL 查询有问题。
标签: php mysql database forms search