我在PHP中有一个脚本,该脚本使用MysqlSelect子句从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 ');

10-04 10:54