最近在学PHP,看的书是PHP与MYSQL WEB开发,第11章的MYSQL数据库里面的二个源码,我运行的时候,总是连接 出现500 服务器内部错误。找了一个下午也没找到原因,在百度和谷歌搜索出来的方法都试了,还是不行,小弟现在实在是找不到原因了,由于卡在这里,后面的内容也没法继续学下去,很郁闷,我发代码上来,请大家帮忙看看
search.html部分:
<html>
<head>
<title>Book-O-Rama Catalog Search</title>
</head><body>
<h1>Book-O-Rama Catalog Search</h1> <form action="results.php" method="post">
Choose Search Type:<br />
<select name="searchtype">
<option value="author">Author</option>
<option value="title">Title</option>
<option value="isbn">ISBN</option>
</select>
<br />
Enter Search Term:<br />
<input name="searchterm" type="text">
<br />
<input type="submit" value="Search">
</form></body>
</html>result.php:
<html>
<head>
<title>Book-O-Rama Search Results</title>
</head>
<body>
<h1>Book-O-Rama Search Results</h1>
<?php
// create short variable names
$searchtype=$_POST['searchtype'];
$searchterm=$_POST['searchterm']; $searchterm= trim($searchterm); if (!$searchtype || !$searchterm)
{
echo 'You have not entered search details. Please go back and try again.';
exit;
}
if (!get_magic_quotes_gpc())
{
$searchtype = addslashes($searchtype);
$searchterm = addslashes($searchterm);
} @ $db = new mysqli('localhost', 'bookorama', 'bookorama123', 'books'); if (mysqli_connect_errno())
{
echo 'Error: Could not connect to database. Please try again later.';
exit;
} $query = "select * from books where ".$searchtype." like '%".$searchterm."%'";
$result = $db->query($query); $num_results = $result->num_rows; echo '<p>Number of books found: '.$num_results.'</p>'; for ($i=0; $i <$num_results; $i++)
{
$row = $result->fetch_assoc();
echo '<p><strong>'.($i+1).'. Title: ';
echo htmlspecialchars(stripslashes($row['title']));
echo '</strong><br />Author: ';
echo stripslashes($row['author']);
echo '<br />ISBN: ';
echo stripslashes($row['isbn']);
echo '<br />Price: ';
echo stripslashes($row['price']);
echo '</p>';
}
$result->free();
$db->close();?>
</body>
</html>
mysql 直接查询是可以查询到数据的。
search.html部分:
<html>
<head>
<title>Book-O-Rama Catalog Search</title>
</head><body>
<h1>Book-O-Rama Catalog Search</h1> <form action="results.php" method="post">
Choose Search Type:<br />
<select name="searchtype">
<option value="author">Author</option>
<option value="title">Title</option>
<option value="isbn">ISBN</option>
</select>
<br />
Enter Search Term:<br />
<input name="searchterm" type="text">
<br />
<input type="submit" value="Search">
</form></body>
</html>result.php:
<html>
<head>
<title>Book-O-Rama Search Results</title>
</head>
<body>
<h1>Book-O-Rama Search Results</h1>
<?php
// create short variable names
$searchtype=$_POST['searchtype'];
$searchterm=$_POST['searchterm']; $searchterm= trim($searchterm); if (!$searchtype || !$searchterm)
{
echo 'You have not entered search details. Please go back and try again.';
exit;
}
if (!get_magic_quotes_gpc())
{
$searchtype = addslashes($searchtype);
$searchterm = addslashes($searchterm);
} @ $db = new mysqli('localhost', 'bookorama', 'bookorama123', 'books'); if (mysqli_connect_errno())
{
echo 'Error: Could not connect to database. Please try again later.';
exit;
} $query = "select * from books where ".$searchtype." like '%".$searchterm."%'";
$result = $db->query($query); $num_results = $result->num_rows; echo '<p>Number of books found: '.$num_results.'</p>'; for ($i=0; $i <$num_results; $i++)
{
$row = $result->fetch_assoc();
echo '<p><strong>'.($i+1).'. Title: ';
echo htmlspecialchars(stripslashes($row['title']));
echo '</strong><br />Author: ';
echo stripslashes($row['author']);
echo '<br />ISBN: ';
echo stripslashes($row['isbn']);
echo '<br />Price: ';
echo stripslashes($row['price']);
echo '</p>';
}
$result->free();
$db->close();?>
</body>
</html>
mysql 直接查询是可以查询到数据的。
@ $db = new mysqli('localhost', 'bookorama', 'bookorama123', 'books');类都没何来对象个人意见
error_reporting(E_ALL);
这样可以排除是否是你的代码问题
确认是你的代码问题再去再代码的问题,注释,一点一点的注释代码