gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 22:54:09 25 4
gpt4 key购买 nike

我正在尝试让一个简单的 php 搜索脚本工作。它当前从 html 捕获数据并将其存储在 $input 中。问题是我一直从下面的脚本中得不到任何结果。但根本没有错误消息。我知道数据库中有完全匹配的数据,但当没有数据时,我不断从下面的代码中收到消息。我的 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>";
}

?>

最佳答案

$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");

基本上,在编写这些查询时,您需要将字符串括在引号中。现在,您的查询将包含

news.story_name=五只猫失踪

你需要它

news.story_name='五只猫失踪'

将其括在引号中 news.story_name='$input' 等等。

关于php - 简单的php搜索代码不返回和结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28789306/

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