doubiao9775 2015-03-01 02:08
浏览 26
已采纳

简单的PHP搜索代码不返回和结果

I am trying to get a simple php search script to work. It currently captures the data from an html and stores it in $input. The problem is that I keep getting no results from the script below. there are no error messages at all though. I know the database has the exact match data in it, but I keep getting the message from my code below for when there is no data. Is something wrong with my SQL?

<?php 
//declaring variable 
$input = $_POST['find']; 
//If they did not enter a search term we give them an error 
if ($input == "") { 
echo "You forgot to enter a search term"; 
exit; 
} 
//open connection 
$dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING );


//the sql statement 

$results = $dbh->prepare("select 
wp_users.ID,
wp_users.display_name,
stories.SID,
stories.story_name,
stories.category,
stories.genre
FROM stories
LEFT JOIN wp_users ON stories.ID=wp_users.ID
WHERE stories.story_name = '$input' OR stories.genre = '$input'");

$results->execute();
$row = $results->fetchAll(PDO::FETCH_ASSOC);

//giving names to the fields 
$storyname = $row['story_name']; 
$category = $row['category']; 
$genre = $row['genre']; 

//put the results on the screen 
echo "<b>$storyname</b>"; 
echo "$categoy"; 
echo "$genre<br>"; 

//This counts the number or results – and if there wasn’t any it gives a  
little message explaining that 
$anymatches=$row; 
if ($anymatches == 0) 
{ 
echo "<h3>Results</h3>"; 
echo "<p>Sorry, your search: &quot;" . $input . "&quot; returned zero  
results</p>"; 
}

?> 
  • 写回答

2条回答 默认 最新

  • douaipi3965 2015-03-01 02:39
    关注
    $results = $dbh->prepare("select 
    wp_users.ID,
    wp_users.display_name,
    news.SID,
    news.story_name,
    news.genre
    FROM stories
    LEFT JOIN wp_users ON news.ID=wp_users.ID
    WHERE news.story_name = $input OR news.genre = $input");
    

    Basically, when writing these queries you need to enclose strings in quotes. Right now, your query would contain

    news.story_name=Five Cats Go Missing

    You need it to be

    news.story_name='Five Cats Go Missing'

    Enclose it in quotes news.story_name='$input' and so on.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么