gpt4 book ai didi

php - 用 PHP 搜索 MySQL

转载 作者:行者123 更新时间:2023-11-29 04:31:52 24 4
gpt4 key购买 nike

我正在做一个项目,我希望一个人在文本框中输入任何艺术家/乐队的名字,它会在我的 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com