doudao9915 2013-11-10 15:36
浏览 61


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:

<title> Catalog Search</title>
<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>
<br />
Enter Search Term:<br />
<input name="searchterm" type="text">
<br />
<input type="submit" value="Search">

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

<title>Search Results</title>
<h1>Search Results</h1>
// create short variable names
if (!$searchtype || !$searchterm)
echo 'You have not entered search details.
 Please go back and try again.';

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

// Create connection

// 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>';

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.

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



  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错
  • ¥20 @microsoft/fetch-event-source 流式响应问题