Php PDO select查询问题:未知错误
我正试图适应PDO,但无法让它发挥作用 下面是基本搜索框的脚本:Php PDO select查询问题:未知错误,php,pdo,Php,Pdo,我正试图适应PDO,但无法让它发挥作用 下面是基本搜索框的脚本: <?php $sth= new connection(); if (isset($_GET['search'])) { $search_query = $_GET['search']; $search_query = htmlentities($search_query); $result=$sth->con->prepare("SELECT firstname, lastname F
<?php
$sth= new connection();
if (isset($_GET['search'])) {
$search_query = $_GET['search'];
$search_query = htmlentities($search_query);
$result=$sth->con->prepare("SELECT firstname, lastname FROM users WHERE
firstname LIKE '%" . $search_query . "%' OR
lastname LIKE '%" . $search_query . "%' OR
LIMIT 25");
$result->bindParam(1, $search_query, PDO::PARAM_STR, 12);
foreach ($result as $row) {
$firstname = $row["firstname"];
$lastname = $row["lastname"];
if (!($result) == 0) {
?>
<div="foo">Here are your results:</div>
<?php
} else {
?>
<div="bar">No results!</div>
<?php
}
}
?>
首先,直接连接sql字符串,因此不需要使用bindParam
。
你应该这样做:
$result=$sth->con->prepare("SELECT firstname, lastname FROM users WHERE
firstname LIKE ? OR
lastname LIKE ? OR
LIMIT 25");
$result->bindValue(1, "%$search_query%", PDO::PARAM_STR);
$result->bindValue(2, "%$search_query%", PDO::PARAM_STR);
第二步,必须调用PDOStatement::execute
来执行该语句
$result->execute();
第三个,仍然存在一些小问题,请尝试阅读手册并检查示例…首先,直接输入sql字符串,因此不需要bindParam
。
你应该这样做:
$result=$sth->con->prepare("SELECT firstname, lastname FROM users WHERE
firstname LIKE ? OR
lastname LIKE ? OR
LIMIT 25");
$result->bindValue(1, "%$search_query%", PDO::PARAM_STR);
$result->bindValue(2, "%$search_query%", PDO::PARAM_STR);
第二步,必须调用PDOStatement::execute
来执行该语句
$result->execute();
第三个,仍然存在一些小问题,请尝试阅读手册并检查示例…正确的顺序和执行
$con = new PDO('...');
$stmt = $conn->prepare('...');
$stmt->bindParam('...');
$stmt->execute();
$result = $stmt->fetchAll();
foreach($result as $row) {
//...
}
、和。需要正确的顺序和执行
$con = new PDO('...');
$stmt = $conn->prepare('...');
$stmt->bindParam('...');
$stmt->execute();
$result = $stmt->fetchAll();
foreach($result as $row) {
//...
}
,,和一个。@Fred ii-是的,打字错误,谢谢你抓住了那个。@Fred ii-是的,打字错误,谢谢你抓住了那个。