Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/250.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 PDO搜索引擎?_Php_Mysql_Pdo - Fatal编程技术网

Php PDO搜索引擎?

Php PDO搜索引擎?,php,mysql,pdo,Php,Mysql,Pdo,在我用PDO重写之前,我为我的网站创建了一个搜索功能。我对PHP和PDO在MySQL数据库中的使用都是非常陌生的,我一辈子都不知道如何将这段代码翻译成PDO。我希望有人能稍微跟我谈谈,帮助我学习 我目前的代码是: function doSearch() { $output = ''; if(isset($_POST['search'])) { $searchq = $_POST['search']; $searchq = preg_replace

在我用PDO重写之前,我为我的网站创建了一个搜索功能。我对PHP和PDO在MySQL数据库中的使用都是非常陌生的,我一辈子都不知道如何将这段代码翻译成PDO。我希望有人能稍微跟我谈谈,帮助我学习

我目前的代码是:

function doSearch() {
    $output = '';
    if(isset($_POST['search'])) {
        $searchq = $_POST['search'];
        $searchq = preg_replace ("#[^0-9a-z]#i","",$searchq);
        $sql = "SELECT * FROM entries WHERE name LIKE '%$searchq%' or description LIKE '%$searchq%' or content LIKE '%$searchq%'";
        $query = mysqli_query($connect, $sql);
        $count = mysqli_num_rows($query);
        if($count == 0) {
            $output = '<tr><tr>No results found.</tr></td>';
        } else {
            while($row = mysqli_fetch_array($query)) {
                $eName = $row['name'];
                $eDesc = $row['description'];
                $eCont = $row['content'];
                $id = $row['id'];
                $elvl = $row['level'];
                $ehp = $row['hp'];

                $output .= '<tr><td><a href="http://xxxx.com/xxx?id=' .$id. '" onclick="document.linkform.submit();">'.$eName.'</a></td><td>'.$eDesc.'</td><td>'.$elvl.'</td><td>'.$ehp.'</td></tr>';
            }
        }
    return $output;
    }
 }
PDO连接已经建立,并且似乎是成功的。这在我的functions.php文件中,我的connection.php文件已附加到functions.php


提前谢谢

应该是这样的

 $conn='';  
    try { 
         $conn = new PDO('mysql:host=localhost;dbname=xxx', 'xxx', 'xxx');
 } 
catch (PDOException $e) { 
exit('Database error.'); 
} 
$sql = "SELECT * FROM entries WHERE name LIKE :searchq or description LIKE :searchq or content LIKE :searchq";

$stmt = $conn->prepare($sql);
$stmt->bindParam(":searchq",$searchq,PDO::PARAM_STR);
$stmt->execute();
$count = $stmt->rowCount();
 if($count == 0) {
            $output = '<tr><tr>No results found.</tr></td>';
        } else {
            while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
                $eName = $row['name'];
                $eDesc = $row['description'];
                $eCont = $row['content'];
                $id = $row['id'];
                $elvl = $row['level'];
                $ehp = $row['hp'];

                $output .= '<tr><td><a href="http://xxxx.com/xxx?id=' .$id. '" onclick="document.linkform.submit();">'.$eName.'</a></td><td>'.$eDesc.'</td><td>'.$elvl.'</td><td>'.$ehp.'</td></tr>';
            }
        }

你能把你到目前为止试过的东西贴出来吗?因为您接受用户值,所以应该使用prepare-and-use-bound参数来防止sql注入这似乎是个好办法。但是,现在我遇到了以下错误:在xxxx第11行的非对象上调用成员函数prepare。@JackMiller似乎连接设置不正确。你能在try..catch块中嵌入连接并查看是否抛出错误吗?嗨,我的connections.php文件中有这个:我的functions.php中包含了它。$conn超出了这里的范围,你需要像我在代码中添加一样添加代码,