简体   繁体   English

使用PDO的PHP搜索数据库

[英]PHP Search database with PDO

I thought this would be a fairly simple task but I keep getting this error when I try to search now " Notice: Array to string conversion in xxxxxxxx.php ". 我认为这将是一个相当简单的任务,但是当我尝试立即搜索“ 注意:xxxxxxxx.php中的数组到字符串转换 ”时,我一直收到此错误。

My code is pretty simple and is as follows: 我的代码非常简单,如下所示:

<?php
$content .= "<h1>Search for a comic</h1>";

$form_html = "<form action='' method='POST' >
    <fieldset><label>Name: <input type='text' name='search_title'  placeholder='Title'></label>
    </fieldset><fieldset>
    <input type='submit' value='Search' name='submit'>
    </fieldset>
</form>";

$content .= $form_html;

if(isset($_POST['submit'])){
    $input_search = $_POST['search_title'];

    $results = $link->prepare('SELECT * FROM Comic WHERE name LIKE ?');
    $results->execute(array('%'.$input_search.'%'));
    $all_results = $results->fetchAll();
    $content .= $all_results;

}
?>

I've read loads of other similar questions and tried a few different ways of doing this but none have worked so far. 我读过很多其他类似的问题,并尝试了几种不同的方法来完成此操作,但到目前为止,没有一种方法起作用。

Bit of further info. 进一步的信息。 $content is echoed by my index so its all displayed. $ content由我的索引回显,因此全部显示。 My database has a table called 'Comics' with like 9 columns, the 2nd of which is 'name'. 我的数据库有一个名为“ Comics”的表,其中有9列,其中第二列是“ name”。 I want the user to be able to type in a single word or complete title and have a list of all the comics with that word or title be shown to them (eg: I search 'Doomsday' or 'Atomic Skull' and the comics 'Doomsday is Here!' and 'Curse of the Atomic Skull' are shown). 我希望用户能够输入单个单词或完整标题,并向他们显示所有带有该单词或标题的漫画的列表(例如:我搜索“世界末日”或“原子头骨”,而漫画“显示了世界末日!”和“原子头骨的诅咒”)。

$results->fetchAll(); returns an array of remaining rows in the result set. 返回结果集中剩余行的数组。 Not a string. 不是字符串。 So doing $content .= $all_results; 这样做$content .= $all_results; is trying to append an array to a string. 试图将数组追加到字符串。

You'll want to loop over each of the items in the array it provides, then you can do what you want with them. 您需要遍历它提供的数组中的每个项目,然后可以对它们进行所需的操作。

http://php.net/manual/en/pdostatement.fetchall.php can help you. http://php.net/manual/en/pdostatement.fetchall.php可以为您提供帮助。

So using this: 所以使用这个:

if(isset($_POST['submit'])){
    $input_search = $_POST['search_title'];

    $results = $link->prepare('SELECT name FROM Comic WHERE name LIKE ?');
    $results->execute(array('%'.$input_search.'%'));
    $all_results = $results->fetchAll();
    $row_cnt = $results->rowCount();

    if($row_cnt == 0){
        $content .= "There are no results.";
    }else {
        foreach ($all_results as $row1){
            foreach ($row1 as $data){
                $content .= $data ."<br />";
            }
        }
    }
}

I output the results but there is two of each result (print_r as suggested above showed me something like this: 我输出了结果,但是每个结果都有两个(上面建议的print_r向我显示了这样的内容:

Array ( [0] => Array ( [name] => Man of Steel [0] => Man of Steel ) [1] => Array ( [name] => The Kid Who Talks to Superman [0] => The Kid Who Talks to Superman ) ) Array([0] => Array([name] =>钢铁侠[0] =>钢铁侠)[1] => Array([name] =>与超人说话的孩子[0] => The和超人说话的孩子))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM