PHP读取Mysql数据库问题

●reach.html<html><head><title>Book-O-RamaCatalogSearch</title></head><body><h1>Book-O... ●reach.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>

●results.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);
}

$connection=mysql_connect ("localhost","root","123");
mysql_select_db("myshop",$connection);

$result=mysql_query("select * from books where ".$searchtype." like '%".$searchterm."%'",$connection);

$num_results= mysql_num_fields($result);
echo '<p>Number of books found: '.$num_results.'</p>';

for ($i=0; $i<$num_results; $i++)
{
$row = mysql_fetch_row($result);
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>';
}

mysql_close($connection);

?>
</body>
</html>

Describe books
+--------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+------------+------+-----+---------+-------+
| isbn | char(13) | NO | PRI | NULL | |
| author | char(50) | YES | | NULL | |
| title | char(100) | YES | | NULL | |
| price | float(4,2) | YES | | NULL | |
+--------+------------+------+-----+---------+-------+
展开
 我来答
ys5955092
2009-12-03 · TA获得超过671个赞
知道小有建树答主
回答量:551
采纳率:100%
帮助的人:565万
展开全部
$row = mysql_fetch_row($result);
结果集中取得一行作为枚举数组
$row = mysql_fetch_assoc($result)
从结果集中取得一行作为关联数组
你的($row['title']))取了关联索引
改成mysql_fetch_assoc($result)就可以了
不然就是这样:
while ($row = mysql_fetch_row($result))
{

echo '<p><strong>'.($i+1).'. Title: ';
echo htmlspecialchars(stripslashes($row[0]));
echo '</strong><br />Author: ';
echo stripslashes($row[1]);
echo '<br />ISBN: ';
echo stripslashes($row[2]);
echo '<br />Price: ';
echo stripslashes($row[3]);
echo '</p>';
$i++;
}
刑慕诗Lw
2009-12-03 · TA获得超过356个赞
知道小有建树答主
回答量:118
采纳率:100%
帮助的人:80.7万
展开全部
其实用mysql_fetch_array()就可以解决了
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式