所以,基本上我想做的是在页面上显示搜索命中。我已经设置了一个发布到下面页面的表单,然后在我的 MySQL 数据库中搜索这些数据,结果应该以以下格式显示:
姓名
个人资料 图片 路径
年龄
我在互联网上进行了广泛搜索,发现您必须使用 while 循环,因为我之前只是使用了 foreach 循环。
但是每当我搜索某些东西时,我的 apache 服务器就会崩溃。我认为这可能是因为 while 循环,因为它之前没有崩溃,但它只会显示同一组结果(名称、图像和年龄)一定次数,具体取决于在数据库中找到的行数.
这是搜索页面代码:
require_once "lib/mysql.php";
if (isset($_POST[search]) && !empty($_POST[search])){
$search = $_POST[search];
// The mysql class used for functions.
$mysql = new mysqlHandle;
// Opens access to the mysql server.
$mysql->accessOpen('root', '', 'Portfolio', 'localhost');
// Sets $rows to number of entries (rows) with the 'Name' value of $search.
$rows = $mysql->tableCheckNum('*', stats, Name, $search, Portfolio);
echo "<div id='content'>
<div id='content_main'>
<h2>Search Results for '$search'</h2>
<br/><hr/><br/>";
// Makes sure that there's at least 1 search hit.
if ($rows>0){
// Sets the current hit number. (the first one is 1, the second is 2 et.c)
$num = 1;
$user = array();
// My mysql function for checking the order of entries, see the attached myqsl code.
while ($result = $mysql->tableCheckOrder('*', stats, Name, $search, ID, DESC, Portfolio)){
// The $user array should hold the user's (searched for) details.
$user[] = $result;
foreach ($user as $result){
// Makes sure that we haven't exceeded the number of rows.
if ($num<=$rows){
// Adds one to the number of displayed rows, as we have dsplayed another.
$num++;
// Prints the user's (searched for) details.
print_r ($user[Name]."<br/>");
print_r ($user[Image]."<br/>");
print_r ($user[Age]."<br/>"."<br/>"."<hr/>"."<br/>");
}
}
}
}else{
echo "No users found";
}
echo '</div>
</div>';
}
这是适用的 MySQL 代码:
// Gets order of entries (rows) The indentations under "function tableCheckOrder" isn't showing up btw.
function tableCheckOrder($span, $table, $column, $value, $order, $mode, $base){
mysql_select_db($base);
$this->query11="SELECT $span FROM $table WHERE $column = '$value' ORDER BY $order $mode";
$this->result11=mysql_query($this->query11) or die("<br/>"."Invalid $table CHECK ORDER query: " .mysql_error());
return mysql_fetch_array($this->result11, $result_type = MYSQL_BOTH);
}
// This function checks amount of rows in table.
function tableCheckNum($span, $table, $column, $value, $base){
mysql_select_db($base);
$this->query="SELECT $span FROM $table WHERE $column = '$value'";
$this->result=mysql_query($this->query) or die("Invalid $table CHECK NUM query: " .mysql_error());
return mysql_num_rows($this->result);
}
这是我的“统计”表结构的图片:
我真的希望能够显示搜索结果而不会崩溃
如果我可以给你更多的信息,请询问!
提前致谢!