-1

我正在尝试从 mysql 转换为使用 PDO。我被困在这个错误上。在大多数情况下,我让它工作,除了绑定变量。

Fatal error: Call to a member function bindParam() on a non-object in

这是我尝试过的:

global $keyword;

$_link = new PDO("mysql:host=$host;dbname=$db_name", "$username", "$password");

$_query = sprintf("SELECT SQL_CALC_FOUND_ROWS * FROM PLD_ANINJA WHERE `Text` REGEXP CONCAT('[[:<:]]',:keyword,'[[:>:]]') LIMIT %d,%d",
    SmartyPaginate::getCurrentIndex(), SmartyPaginate::getLimit());
$_query->bindParam(':keyword',$keyword, PDO::PARAM_STR);
$_query->execute();

这是最初的工作:

global $keyword;

$_query = sprintf("SELECT SQL_CALC_FOUND_ROWS * FROM PLD_ANINJA WHERE `Text` REGEXP '[[:<:]]" . $keyword . "[[:>:]]' LIMIT %d,%d",
    SmartyPaginate::getCurrentIndex(), SmartyPaginate::getLimit());
4

1 回答 1

2

您试图在字符串上调用 PDO 方法,而不是 PDO 语句对象。你需要打电话$_link->prepare()

$_query = $_link->prepare("SELECT SQL_CALC_FOUND_ROWS * FROM PLD_ANINJA WHERE `Text` REGEXP CONCAT('[[:<:]]',:keyword,'[[:>:]]') LIMIT :start,:count");
$_query->bindParam(':keyword', $keyword, PDO::PARAM_STR);
$_query->bindValue(':start', SmartyPaginate::getCurrentIndex());
$_query->bindValue(':count', SmartyPaginate::getLimit());
$_query->execute();
于 2013-09-29T03:41:29.467 回答