我使用 000webhost 运行我的代码,并使用 phpmyadmin 运行我的数据库。我试图做到这一点,以便当我搜索一个名称时,它会在数据库中为我提供该名称下的名称和密码。
这是错误:
解析错误:语法错误,第 51 行/storage/h11/920/1783920/public_html/search.php 中出现意外的“>”
这是我的 html:
<!DOCTYPE html>
<html>
<head>
<title>Search</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="style.css"/>
</head>
<body>
<form action="search.php" method="GET">
<input type="text" name="query" />
<input type="submit" value="Search" />
</form>
</body>
</html>
这是我的 php:
<?php
mysql_connect("localhost", "id1783920_123456", "") or die("Error connecting to database: ".mysql_error());
/* The Third "" is the password spot and I don't want to put it. It's not the problem*/
// LINE 10
mysql_select_db("id1783920_mydb") or die(mysql_error());
?>
<!DOCTYPE html>
<html>
<head>
<title>Search results</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<?php
$query = $_GET['query'];
$min_length = 1;
// LINE 30
if(strlen($query) >= $min_length){ // if query length is more or equal minimum length then
$query = htmlspecialchars($query);
$query = mysql_real_escape_string($query);
// makes sure nobody uses SQL injection
$raw_results = mysql_query("SELECT * FROM Signup
WHERE (`username`='$query') OR (`password`='$query') or die(mysql_error());
// LINE 45
if(mysql_num_rows($raw_results) > 0){ // if one or more rows are returned do following
while($results = mysql_fetch_array($raw_results)){
echo "<p><h3>".$results['username']."</h3>".$results['password']."</p>";
}
}
else{ // if there is no matching rows do following
echo "No results";
}
}
else{ // if query length is less than minimum
echo "Minimum length is ".$min_length;
}
?>
</body>
</html>
任何帮助将不胜感激。谢谢
(出现另一个错误: fatal error :未捕获错误:调用/storage/h11/920/1783920/public_html/search.php:37 中未定义的函数 mysql_query() 堆栈跟踪:#0 {main} 抛出/storage/h11/920/1783920/public_html/search.php 第 37 行
最佳答案
你应该更换
$raw_results = mysql_query("SELECT * FROM Signup
WHERE (`username`='$query') OR (`password`='$query') or die(mysql_error());
与
$raw_results = mysql_query("SELECT * FROM Signup
WHERE (`username`='$query') OR (`password`='$query')") or die(mysql_error());
当您使用 PHP7 时,您应该删除 mysql_ 函数。它们自 v 5.5 起已被弃用,并在 PHP7 中被删除。使用mysqli相反。
关于php - 我在尝试搜索数据库时遇到此错误。有什么建议么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44249602/