我在PHP
中有一个脚本,该脚本使用Mysql
和Select
子句从Where
数据库中选择数据。
$lrn= "PU2017LN11K";
$stmt = $user_home->runQuery('SELECT * FROM mcq WHERE LRN = :crn ORDER BY LRN,Sr ASC ');
$stmt->bindParam(':crn',$lrn);
$stmt->execute();
现在介绍如何编写代码脚本,以便使用
LRN
从所有WHERE LRN = :crn
中进行选择。我试过了:
$lrn= "*";
$stmt = $user_home->runQuery('SELECT * FROM mcq WHERE LRN = :crn ORDER BY LRN,Sr ASC ');
$stmt->bindParam(':crn',$lrn);
$stmt->execute();
但是没有显示任何结果!
如果要显示所有结果,很容易跳过
WHERE
子句,但是$lrn
的值将由$_GET
方法定义。 最佳答案
处理此问题的正常方法是:
$lrn= "*";
$stmt = $user_home->runQuery('SELECT * FROM mcq WHERE LRN = :crn OR :crn = \'*\' ORDER BY LRN, Sr ASC ');
或者,您可以使用
LIKE
:$lrn = "%";
$stmt = $user_home->runQuery('SELECT * FROM mcq WHERE LRN LIKE :crn ORDER BY LRN, Sr ASC ');