duanpuluan0480
2017-11-05 16:44
浏览 247
已采纳

MySQL中的WHERE子句用于选择所有

I have a script in PHP, which selects data from a Mysql Database using Select & Where clause.

$lrn= "PU2017LN11K";
$stmt = $user_home->runQuery('SELECT * FROM mcq WHERE LRN = :crn ORDER BY LRN,Sr ASC ');
$stmt->bindParam(':crn',$lrn);
$stmt->execute();

Now how to script the code so that it selects from all the LRN, using WHERE LRN = :crn.

I tried:

$lrn= "*";
$stmt = $user_home->runQuery('SELECT * FROM mcq WHERE LRN = :crn ORDER BY LRN,Sr ASC ');
$stmt->bindParam(':crn',$lrn);
$stmt->execute();

But it didn't show any results!

It's easy to skip the WHERE clause if I want to show all results, but the value of $lrn will be defined by the $_GET method.

图片转代码服务由CSDN问答提供 功能建议

我在 PHP 中有一个脚本,它从 Mysql <中选择数据 / code>数据库使用选择&amp; Where 子句。

  $ lrn =“PU2017LN11K”; 
 $ stmt = $ user_home-&gt; runQuery('SELECT * FROM mcq WHERE LRN  =:crn ORDER BY LRN,Sr ASC'); 
 $ stmt-&gt; bindParam(':crn',$ lrn); 
 $ stmt-&gt; execute(); 
   
 
 

现在如何编写代码脚本,以便使用 WHERE LRN =:crn 从所有 LRN 中进行选择。

我试过了:

  $ lrn =“*”; 
 $ stmt = $ user_home-&gt; runQuery('SELECT * FROM mcq WHERE LRN =:  crn ORDER BY LRN,Sr ASC'); 
 $ stmt-&gt; bindParam(':crn',$ lrn); 
 $ stmt-&gt; execute(); 
   \  n 
 

但它没有显示任何结果!

如果我想显示所有结果,可以轻松跳过 WHERE 子句,但是\ $ _ Gn 方法将定义 $ lrn 的值。

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

3条回答 默认 最新

  • dpchen2004 2017-11-05 16:51
    最佳回答

    The normal way of handling this would be:

    $lrn= "*";
    $stmt = $user_home->runQuery('SELECT * FROM mcq WHERE LRN = :crn OR :crn = \'*\' ORDER BY LRN, Sr ASC ');
    

    Alternatively, you could use LIKE:

    $lrn = "%";
    $stmt = $user_home->runQuery('SELECT * FROM mcq WHERE LRN LIKE :crn ORDER BY LRN, Sr ASC ');
    
    评论
    解决 无用
    打赏 举报
查看更多回答(2条)

相关推荐 更多相似问题