使用bindParam显示数据库中的数据
问题描述:
i want to try to showing my data from database using bindParam but i get some error.
Recoverable fatal error: Object of class PDOStatement could not be converted to string in C:\xampp\htdocs\piratefiles\search.php on line 15
here my code
$category = htmlentities($_GET['c']);
$query = htmlentities($_GET['q']);
$page = (isset($_GET['page'])) ? $_GET['page'] : 1;
$limit = 20;
$limit_start = ($page - 1) * $limit;
$query = $db->prepare ("SELECT * FROM `posting` WHERE 'category' = :category AND 'file_name' like :query ORDER BY date DESC LIMIT ".$limit_start.",".$limit);
$query->bindParam(":category", $category);
$query->bindParam(":query", $query);
$query->execute();
我想尝试使用bindParam从数据库显示我的数据,但是我收到了一些错误。 p>
可恢复的致命错误:第15行的C:\ xampp \ htdocs \ piratefiles \ search.php中无法将类PDOStatement的对象转换为字符串 p> blockquote>
这里我的代码 p>
$ category = htmlentities($ _ GET ['c']); $ query = htmlentities($ _ GET ['q ']); $ page =(isset($ _ GET ['page']))? $ _GET ['page']:1; $ limit = 20; $ limit_start =($ page - 1)* $ limit; $ query = $ db-> prepare(“SELECT * FROM `posts` WHERE'category'=:category AND'file_name'like:query ORDER BY date DESC LIMIT“。$ limit_start。”,“。$ limit); $ query-> bindParam(”:category“ ,$ category); $ query-> bindParam(“:query”,$ query); $ query-> execute(); code> pre> DIV>
答
$query
was the user input, then you assigned it as the PDOStatement, then your the passing it back to bindParam
Change the var name.
$category = htmlentities($_GET['c']);
$query = htmlentities($_GET['q']);
$page = (isset($_GET['page'])) ? $_GET['page'] : 1;
$limit = 20;
$limit_start = ($page - 1) * $limit;
$stmt = $db->prepare ("SELECT * FROM `posting` WHERE 'category' = :category AND 'file_name' like :query ORDER BY date DESC LIMIT ".$limit_start.",".$limit);
$stmt->bindParam(":category", $category);
$stmt->bindParam(":query", $query);
$stmt->execute();
答
Since im using LIKE
so, need to make another variable.
$keyword1 = "%".$category."%";
$keyword2 = "%".$query1."%";
Here's Full code.
$category = htmlentities($_GET['c']);
$query1 = htmlentities($_GET['q']);
$page = (isset($_GET['page'])) ? $_GET['page'] : 1;
$limit = 20;
$limit_start = ($page - 1) * $limit;
$query = $db->prepare ("SELECT * FROM `posting` WHERE category LIKE :category AND file_name LIKE :query1 ORDER BY date DESC LIMIT ".$limit_start.",".$limit);
$keyword1 = "%".$category."%";
$keyword2 = "%".$query1."%";
$query->bindParam(":category", $keyword1);
$query->bindParam(":query1", $keyword2);
$query->execute();