我正在努力倾向于PDO。这是我的示例代码。
<?php
$dbname = "procedure_testing";
$dbuser = "root";
$dbpass = "mypass";
$host="localhost";
try {
$conn = new PDO('mysql:host=localhost;dbname=$dbname', $dbuser, $dbpass);
$id=3;
$stmt = $conn->prepare('SELECT * FROM salary WHERE empid = :empid');
$stmt->execute(array('empid' => $id));
$result = $stmt->fetchAll();
if ( count($result) ) {
foreach($result as $row) {
print_r($row);
}
} else {
echo "No rows returned.";
}
} catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
?>
但我收到以下错误:
解析错误:语法错误,第10行的C:\ xampp \ htdocs \ pdo_example \ select.php中的意外T_VARIABLE
我不知道是什么问题。请任何人都可以检查我的代码并帮助我吗?
答案 0 :(得分:0)
你几乎忘记了:
$stmt = $conn->prepare('SELECT * FROM salary WHERE empid = :empid');
$stmt->execute(array(':empid' => $id)); //notice : in front of empid.