Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我的PHP搜索函数没有输出数据_Php_Mysql - Fatal编程技术网

我的PHP搜索函数没有输出数据

我的PHP搜索函数没有输出数据,php,mysql,Php,Mysql,所以我正在尝试创建一种搜索我的网站的方法。我在索引页上创建了一个搜索栏。您可以在此处找到此页面: 我的搜索表单如下所示: <form method="post" action="search.php"> <input type="text" name="search" class="search" value="Skeleton" onFocus="this.value=''"> </form> 我附加了一个functions

所以我正在尝试创建一种搜索我的网站的方法。我在索引页上创建了一个搜索栏。您可以在此处找到此页面:

我的搜索表单如下所示:

        <form method="post" action="search.php">
    <input type="text" name="search" class="search" value="Skeleton" onFocus="this.value=''">
    </form>
我附加了一个functions.php文件,这个页面也连接到我的mysql数据库。我已准备好可供阅读/搜索的内容

下面是我在functions.php上的搜索函数:

function doSearch() {
$output = '';
if(isset($_POST['search'])) {
    $searchq = $_POST['search'];
    $searchq = preg_replace ("#[^0-9a-z]#i","",$searchq);

    $query = mysql_query("SELECT * FROM entries WHERE name LIKE '%$searchq%' or description LIKE '%$searchq%' or content LIKE '%$searchq%'") or die("Could not search");
    $count = mysql_num_rows($query);
    if($count == 0) {
        $output = 'there was no search results!';
    } else {
        while($row = mysql_fetch_array($query)) {
            $eName = $row['name'];
            $eDesc = $row['description'];
            $eCont = $row['content'];
            $id = $row['id'];

            $output .= '<div>'.$eName.' '.$eDesc.'</div>';
        }
    }
}
}

my search.php上除了通常的html布局之外,唯一的内容如下:

<?php

include('includes/functions.php');

if(!isset($_POST['search'])) {
    header("Location:index.php");
}

?>
再往下看标签

<?php print("$output");?>
现在我对PHP和MySQL还很陌生。但是,我的error.log文件没有错误,这使得第一次进行故障排除有点困难。有什么建议吗?我敢肯定这是一个非常简单的错误,可能只是拼错了什么,但我就是看不出来。

您的doSearch函数不会返回任何内容

返回$output

但是$output只在函数中声明。所以你需要使用

印刷研究

或者将$output声明为全局变量,但我们不希望这样做:

function doSearch() {
$output = '';
if(isset($_POST['search'])) {
    $searchq = $_POST['search'];
    $searchq = preg_replace ("#[^0-9a-z]#i","",$searchq);

    $query = mysql_query("SELECT * FROM entries WHERE name LIKE '%$searchq%' or description LIKE '%$searchq%' or content LIKE '%$searchq%'") or die("Could not search");
    $count = mysql_num_rows($query);
    if($count == 0) {
        $output = 'there was no search results!';
    } else {
        while($row = mysql_fetch_array($query)) {
            $eName = $row['name'];
            $eDesc = $row['description'];
            $eCont = $row['content'];
            $id = $row['id'];

            $output .= '<div>'.$eName.' '.$eDesc.'</div>';
        }
    }

 //make sure your function returns $output
 return $output;
}
确保函数返回输出,然后回显函数:

<?php echo doSearch(); ?>
这是因为PHP变量是如何使用的

…那么我们当然需要加入所有的标准但书。。。不要使用mysql库,它几乎像挪威蓝一样死气沉沉。如果您使用mysqli或PDO,您可以将参数/值绑定到一个已准备好的语句,这不仅可以提高效率,而且可以确保您的输入得到正确的清理,这比您正在使用的临时preg_替换要好得多

您不想在查询失败时终止脚本-这有点奇怪,请正确处理错误


在SQL中有更好的搜索方法,例如搜索。。或者,如果可以的话,可以实现,而不是尝试自己实现。

您的php.ini文件似乎设置为不显示错误。在代码开头添加以下代码行,然后重试:


如下文所述。。。你没有归还任何东西,但问题不止这些。