我的 PHP 从数据库中的 table 搜索名字或姓氏的代码没有在我的网页上打印任何内容
My PHP code to search for a first name or last name from a table in a database is not printing anything to my webpage
用户假设在网页的搜索栏中输入名字或姓氏,并假设将 table 中的属性列出到网页中。无论我在搜索栏中输入什么,都不会输出任何内容。我观看了 this 有关如何在 php 中搜索的视频。我试着看了一个多小时,但我找不到任何错误。我的网页中没有收到任何错误消息。
<?php
$serverName = 'localhost';
$userName = 'root';
$password = '';
$databaseName = 'project3';
$connection = mysqli_connect($serverName, $userName, $password,
$databaseName);
if (!$connection) {
die("Connection Failed: " . mysqli_connect_error());
}
echo "Connected Successfully!! <br>";
$output = '';
if (isset($_Post['search'])) {
$searchq = $_Post['search'];
$searchq = preg_replace("#[^0-9a-z]#i", "", $searchq);
$query = mysqli_query("SELECT * from employee WHERE fname LIKE
'%$searchq%' OR"
. "lname LIKE '%$searchq%") or die("failed");
$count = mysqli_num_rows($query);
if ($count == 0) {
$output = 'No search results';
} else {
while ($row = mysqli_fetch_array($query)) {
$firstname = $row['fname'];
$lastname = $row['lname'];
$id = $row['id'];
$output .= '<div>' . $firstname . '' . $lname . '</div>';
echo "hi";
}
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Database Webpage</title>
<font color ="white">
<h1 style="background-color:black; text-align: center">Datebase Website</h1>
<font color ="black">
</head>
<body>
<form action = "index.php" method = "POST">
<input type = "text" name ="search" placeholder="Search"/>
<input type= "submit" value = ">>"/>
</form>
<?php print("$output"); ?>
</body>
</html>
mysqli_query
确实需要 2 个参数,因为 the PHP api 显示了您拥有的程序形式。
因此,对于那部分,调用应如下所示:
$query = mysqli_query($connection, "SELECT * from employee WHERE fname LIKE
'%$searchq%' OR"
. "lname LIKE '%$searchq%") or die("failed");
但是查询会有问题。请注意查询的这一部分:
'%$searchq%' OR" <--- No space after the OR
. "lname LIKE '%$searchq%")
^--- No space before lname
这两个区域中的任何一个都需要space。
查询字符串的最后一部分最终将如下所示:
'%$searchq%' ORlname LIKE '%$searchq%
^--- trouble (no space) ^---and trouble here (missing a closing single quote)
有时单独设置查询很有用,这样您就可以回显它以检查它在关键字、列、值、逗号用法(需要时)、正确引用等方面的句法是否正确,等等
考虑这个区别:
$query = "SELECT * from employee WHERE fname LIKE '%$searchq%' OR "
. "lname LIKE '%$searchq%'";
// query check (delete after validation, or comment-out)
echo $query;
$result = mysqli_query($connection, $query);
// I like to use $result, as the query call will return a result set with SELECT
// or false with failure. (though it can return true for other query types, see the api link)
将错误作为死消息的一部分输出也很有帮助:
if (!$result) { // Doh! something wrong...
die('failed: ' . mysqli_error($connection));
} else {
$count = mysqli_num_rows($result); // check the result count
if ($count == 0) {
$output = 'No search results';
} else {
while ($row = mysqli_fetch_array($result)) { // fetch a row
$firstname = $row['fname'];
$lastname = $row['lname'];
$id = $row['id'];
$output .= '<div>' . $firstname . '' . $lname . '</div>';
echo "hi";
}
}
}
HTH
用户假设在网页的搜索栏中输入名字或姓氏,并假设将 table 中的属性列出到网页中。无论我在搜索栏中输入什么,都不会输出任何内容。我观看了 this 有关如何在 php 中搜索的视频。我试着看了一个多小时,但我找不到任何错误。我的网页中没有收到任何错误消息。
<?php
$serverName = 'localhost';
$userName = 'root';
$password = '';
$databaseName = 'project3';
$connection = mysqli_connect($serverName, $userName, $password,
$databaseName);
if (!$connection) {
die("Connection Failed: " . mysqli_connect_error());
}
echo "Connected Successfully!! <br>";
$output = '';
if (isset($_Post['search'])) {
$searchq = $_Post['search'];
$searchq = preg_replace("#[^0-9a-z]#i", "", $searchq);
$query = mysqli_query("SELECT * from employee WHERE fname LIKE
'%$searchq%' OR"
. "lname LIKE '%$searchq%") or die("failed");
$count = mysqli_num_rows($query);
if ($count == 0) {
$output = 'No search results';
} else {
while ($row = mysqli_fetch_array($query)) {
$firstname = $row['fname'];
$lastname = $row['lname'];
$id = $row['id'];
$output .= '<div>' . $firstname . '' . $lname . '</div>';
echo "hi";
}
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Database Webpage</title>
<font color ="white">
<h1 style="background-color:black; text-align: center">Datebase Website</h1>
<font color ="black">
</head>
<body>
<form action = "index.php" method = "POST">
<input type = "text" name ="search" placeholder="Search"/>
<input type= "submit" value = ">>"/>
</form>
<?php print("$output"); ?>
</body>
</html>
mysqli_query
确实需要 2 个参数,因为 the PHP api 显示了您拥有的程序形式。
因此,对于那部分,调用应如下所示:
$query = mysqli_query($connection, "SELECT * from employee WHERE fname LIKE
'%$searchq%' OR"
. "lname LIKE '%$searchq%") or die("failed");
但是查询会有问题。请注意查询的这一部分:
'%$searchq%' OR" <--- No space after the OR
. "lname LIKE '%$searchq%")
^--- No space before lname
这两个区域中的任何一个都需要space。
查询字符串的最后一部分最终将如下所示:
'%$searchq%' ORlname LIKE '%$searchq%
^--- trouble (no space) ^---and trouble here (missing a closing single quote)
有时单独设置查询很有用,这样您就可以回显它以检查它在关键字、列、值、逗号用法(需要时)、正确引用等方面的句法是否正确,等等
考虑这个区别:
$query = "SELECT * from employee WHERE fname LIKE '%$searchq%' OR "
. "lname LIKE '%$searchq%'";
// query check (delete after validation, or comment-out)
echo $query;
$result = mysqli_query($connection, $query);
// I like to use $result, as the query call will return a result set with SELECT
// or false with failure. (though it can return true for other query types, see the api link)
将错误作为死消息的一部分输出也很有帮助:
if (!$result) { // Doh! something wrong...
die('failed: ' . mysqli_error($connection));
} else {
$count = mysqli_num_rows($result); // check the result count
if ($count == 0) {
$output = 'No search results';
} else {
while ($row = mysqli_fetch_array($result)) { // fetch a row
$firstname = $row['fname'];
$lastname = $row['lname'];
$id = $row['id'];
$output .= '<div>' . $firstname . '' . $lname . '</div>';
echo "hi";
}
}
}
HTH