我刚刚观看了有关如何创建自定义数据库的YouTube 视频。我试图在我的网站中实现这一点。但是,显然,它不起作用......
我收到错误“查询失败:”,以下代码仅此而已。
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect to database");
mysql_select_db("$db_name")or die("cannot select DB");
$con=mysqli_connect("$host","$username","$password","$db_name");
$terms = explode(" ", $search);
$query = "SELECT * FROM search WHERE ";
foreach ($terms as $each){
$i++;
if ($i == 1)
$query .= "keywords LIKE '%each%' ";
else
$query .= "OR keywords LIKE '%each%' ";
}
$query = mysql_query($query);
$numrows = mysql_num_rows($query) or die('Query failed: ' . mysql_error() . "<br />\n$sql");
if ($numrows > 0)
while($row = mysqlfetch_ASSOC($query)){
$id = $row['id'];
$title = $row['title'];
$description= $row['description'];
$keywords = $row['keywords'];
$link = $row['link'];
echo '<h2><a href="$link">$title</a><h2>
$description<br/><br/>';
}
else
echo "No results found";
//disconnect
mysql_close();
?>
老实说,我只了解其中的 70% 并且不知道。数据库连接正确并正常运行。我究竟做错了什么???
mysql_num_rows 之前引起了问题,但是在 WHERE'' 和右引号之间放了一个空格之后,它就把我留在这里了。