doudao9915 2013-11-10 15:36
浏览 61
已采纳

无法从mysql数据库中检索数据

I have been follwoing some php-mysql tutorial from a textbook. I should be able to retrieve some data from the "books" database, for example if i select author as searchtype and Michael as searchterm i should get some results since the author name is in the database. But, i am not getting any result after i submit the data from form. It just shows following:

Searh Results 

Number of books found:

follwing is my html code for the form:

<html>
<head>
<title> Catalog Search</title>
</head>
<body>
<h1>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>

I have a database named books and a php script named results.php:

<html>
<head>
<title>Search Results</title>
</head>
<body>
<h1>Search Results</h1>
<?php
// create short variable names
$searchtype=$_POST["searchtype"];
$searchterm=$_POST["searchterm"];
if (!$searchtype || !$searchterm)
{
echo 'You have not entered search details.
 Please go back and try again.';
exit;
}

$searchterm= trim($searchterm);
$searchtype = addslashes($searchtype);
$searchterm = addslashes($searchterm);

// Create connection
$con=mysqli_connect("localhost","myusername","mypassword","books");

// Check connection
if (mysqli_connect_errno($con))
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$query = "select * from books where ".$searchtype." like '%".$searchterm."%'";
$result = mysql_query($query);
$num_results = mysql_num_rows($result);
echo '<p>Number of books found: '.$num_results.'</p>';
for ($i=0; $i <$num_results; $i++)
{
$row = mysql_fetch_array($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>';
}
?>
</body>
</html>

What's wrong with the code? thank's in advance.

  • 写回答

1条回答 默认 最新

  • dongzi9196 2013-11-10 15:39
    关注

    Your connection uses mysqli extension, therefore your query has to use that as well and not the mysql_*

    $result = mysql_query($query); // Wrong extension. Use mysqli
    

    should be

    $result = mysqli_query($con,$query);
    

    Then all the subsequent database functions have to use mysqli_*, you cannot mix the two together.

    Side note: use mysqli_real_escape_string instead of addslashes.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图