我想使用多个“?”运行一个带有 PHP 的 select 语句。我怎么做?我认为下面的代码应该这样做,有什么问题?
$con = mysql_connect("database","login","password");
if (!$con){
die('Could not connect: ' . mysql_error());
}
mysql_select_db("posdictionary", $con);
$stmt = $con->prepare("SELECT * FROM WordPOS WHERE WORD LIKE '?%' AND pos LIKE BINARY '%?%'");
$stmt->bind_param('ss', $pattern, $pos);
$pattern = $_POST["pattern"];
$pos = $_POST["pos"];
$value = "";
if ($stmt->execute()) {
while ($row = $stmt->fetch()) {
$value = $value . $row['word'] . " " . $row['pos'] . "<br />";
}
}
更新:
下面的答案很棒。我的 PHP 中有一个错误,'?%' 在 php sql 语句中是不合法的。查询应如下所示:
$pattern = $_POST["pattern"] ."%";
$pos = "%". $_POST["pos"] ."%";
$stmt = $con->prepare("SELECT word, pos FROM WordPOS WHERE word LIKE ? AND pos LIKE BINARY ?");
然后按照其余的答案。