这是我在PHP PDO中计算行数的代码
$sql = "SELECT count(name) FROM `info` WHERE name='$name' ";
$result = $conn->prepare($sql);
$result->execute();
$totalrows = $result->fetchColumn();
但问题是,如果$name
包含'
,则会出错。准备好的声明快速,安全且更好。如何将其转换为准备好的声明?
我试图让它成为PHP PDO语句,但它无法正常工作
$stmt = $conn->prepare("SELECT count(name) FROM `info` WHERE name='$name' ");
$stmt->execute(array('name' => $name));
$totalrows = $stmt->fetchColumn();