php - 用 PHP 搜索 MySQL

标签 php mysql search

我正在做一个项目,我希望一个人在文本框中输入任何艺术家/乐队的名字,它会在我的 mysql 数据库中搜索事件信息,并在另一个页面上显示结果/内容。下面的代码在我的 index.php 中,它应该从 search.php 中获取信息(也在下面)。我已经看了一遍,我不确定为什么它不起作用,我也不知道该怎么做。帮助会很棒! (我真的需要通过这门课!):)

(索引.php)

<form name="search" action="search.php" method="get">
    <div align="center"><input type="text" name="q" />
    <p><input type="submit" name="Submit" value="Search" /></p>
</form>

(搜索.php)

<?php

//Get the search variable from URL

$var=@&_GET['q'];
$trimmed=trim($var); //trim whitespace from the stored variable

//rows to return
$limit=10;

//check for an empty string and display a message.
if($trimmed=="")
    {
    echo"<p>Please enter a name.</p>";
    exit;
    }

//check for a search parameter
if(!isset($var))
    {
    echo"<p>We don't seem to have a search parameter!</p>";
    exit;
    }

//connect to database
mysql_connect("localhost","root","password");

//specify database
mysql_select_db("itour") or die("Unable to select database");

//Build SQL Query
$query = "select * from events where artist_name like \"%trimmed%\" order by date";

$numresults=mysql_query($query);
$numrows=mysql_num_rows(numresults);

//If no results, offer a google search as an alternative

if ($numrows==0)
    {
    echo"<h3>Results</h3>";
    echo"<p>Sorry, your search: &quot;" .$trimmed . "&quot; returned zero results</p>";

    //google
    echo"<p><a href=\"http://www.google.com/search?q=".$trimmed . "\" target=\"_blank\" title=\"Look up ".$trimmed ." on Google\">
    Click here</a> to try the search on google</p>";
    }

//next determine if s has been passed to script, if not use 0
if(empty($s)) {
    $s=0;
    }

//get results
$query .=" limit $s,$limit";
$result = mysql_query($query) or die("Couldn't execute query");

//display what was searched for
echo"<p>You searched for: &quot;" .$var . "&quot;</p>";

//begin to show results set
echo "Results";
$count = 1 + $s;

//able to display the results returned
while ($row=mysql_fetch_array($result)) {
$title = $row["artist_name"];

echo"$count.)&nbsp;$title";
$count++;
}

$currPage = (($s/$limit) + 1;

echo"<br  />";

//links to other results
if ($s>=1){
    //bypass PREV link if s is 0
    $prevs=($s-$limit);
    print"&nbsp;<a href=\"$PHP_SELF?s=$prevs&q=$var\">&lt;&lt;
    Prev 10</a>&nbsp;&nbsp;";
}

//calculate number of pages needing links
$pages = intval($numrows/$limit);

//$pages now contains int of pages needed unless there is a remainder from diviison

if($numrows%$limit){
//has remainder so add one page
$pages++;
}

//check to see if last page
if (!((($s+$limit)/$limit)==$pages) && $pages!=1){

//not last page so give NEXT link
$news = $s+$limit;

echo "&nbsp;<a href=\"$PHP_SELF?s=$news&q=$var\">Next 10 &gt;&gt;</a>";
}

$a = $s +($limit);
if($a > $numrows){$a = $numrows;}
$b = $s + 1;
echo "<p>Showing results $b to $a of $numrows</p>";

?>

最佳答案

您的 where 子句很愚蠢...尝试将其更改为:

WHERE artist_name like '%$trimmed%'

只是将 trimmed 字面解释为字符串“trimmed”。但是,在双引号字符串中使用变量 $trimmed 将给出实际变量的值。

关于php - 用 PHP 搜索 MySQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/826439/

相关文章:

php - 通过准备好的语句从数据库中获取行

search - 上下文建议的索引,例如使用Elasticsearch的Flipkart,亚马逊

php - 显示来自 WordPress 帖子的图像

mysql - 如何模拟mysql中一行的死锁?

java - MySql 中包含逗号的 CSV 数据插入失败

使用来自不同表的 SELECT SUM 的 MySQL UPDATE

matlab - 在矩阵 MATLAB 中查找数组

python - 如果只能获取完整的用户列表,则从 REST GET 读取单个用户条目

php - 为什么 Laravel 5.6 中没有删除表中的用户

php - 在使用 PHP 转换 JSON 数组中的 MySql 输出时卡住了