传递的变量在 PDO 上不起作用


Passed variable not working on PDO

我有一个传递的变量应该加载到 PHP-PDO 查询上,但我收到此错误:

致命错误:在非对象上调用成员函数 prepare()

这是我的代码:

$color = $_GET['color'];    
$items = $con -> prepare("SELECT * FROM item_descr WHERE color_base1 = :colorbase1");
$items = bindValue(':colorbase1', $color);

有什么小事吗?谢谢!

您必须在

$con->prepare 之前在脚本中声明$con

$con =  new PDO($dns, $user, $pass);
$color = $_GET['color'];    
$items = $con->prepare("SELECT * FROM item_descr WHERE color_base1 = :colorbase1");
$items->bindValue(':colorbase1', $color);
$items->execute();

:D玩得愉快