【问题标题】:on click link display string which first character is between 0-9点击链接显示字符串,第一个字符在 0-9 之间
【发布时间】:2015-01-04 16:19:40
【问题描述】:

我试图从我的数据库中显示一个字符串,其中第一个字符在 0 到 9 之间

if(isset($_REQUEST['num'])){ $num = explode('-',$_REQUEST['num']);
$query1 = "SELECT usercode FROM user WHERE code like '".$num."%'" ORDER BY id DESC ";
    $result1 = mysql_query ($query1) or die('query error');
    while( $line1 = mysql_fetch_assoc($result1)){
    echo $line1[usercode];
    }

<a href="home.php?num=<?php echo urlencode('0-9');?>">#</a>

【问题讨论】:

    标签: php string replace explode


    【解决方案1】:

    您的代码当前正在您的数据库中搜索Array,因为您是explode()'ing $_REQUEST['num'],它返回一个数组。

    您可能希望从您的 $num 创建一个数字范围,然后执行多个 LIKE 或尝试使用 REGEXP

    【讨论】:

      【解决方案2】:

      使用 MySQL 中的REGEXP 函数将code 匹配到^[0-9],这意味着:“第一个字母必须在 0 到 9 之间”,而在 ASCII 中,它们恰好是 0 到 9 之间的数字。

      这会做你想做的事:

      <?php
      if (isset($_REQUEST['num'])) {
          $num = explode('-', $_REQUEST['num']);
          $query1 = "SELECT usercode FROM user WHERE code REGEXP '^[" . $num[0] . "-" . $num[1] . "]' ORDER BY id DESC";
          $result1 = mysql_query ($query1) or die('query error');
          while ($line1 = mysql_fetch_assoc($result1)) {
              echo $line1['usercode'];
          }
      }
      ?>
      
      <a href="home.php?num=<?php echo urlencode('0-9'); ?>">#</a>
      

      cOle2 关于爆炸功能也是正确的。它返回一个数组,其中的元素是由某个分隔符标记的输入字符串。在您的情况下,$_REQUEST['num'] 基于分隔符 -

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-02-15
        • 2011-11-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多