【问题标题】:Figuring SQL QUERY for EACH ISSET?为每个 ISSET 计算 SQL QUERY?
【发布时间】:2011-03-21 02:20:25
【问题描述】:

大家好,我遇到了一个问题,需要你们的帮助。我想为每个 $_GET 参数更改 SQL QUERY。我正在使用这个:

<?
if (isset($_GET['page']) ) {
   $pageno = $_GET['page'];
} else {
   $pageno = 1;
} // if
$query = mysql_query("SELECT count(id) FROM m3_music_mp3");
$query_data = mysql_fetch_row($query);
$numrows = $query_data[0];
$rows_per_page = 30;
$lastpage      = ceil($numrows/$rows_per_page);
$pageno = (int)$pageno;
if ($pageno > $lastpage) {
   $pageno = $lastpage;
} // if
if ($pageno < 1) {
   $pageno = 1;
} // if
$limit = 'ORDER BY id DESC LIMIT ' .($pageno - 1) * $rows_per_page .',' .$rows_per_page;
$query = mysql_query("SELECT * FROM m3_music_mp3 $limit");
?>

IF music.php?page=1 is set $query = "SELECT * FROM m3_music_mp3 ORDER BY id DESC LIMIT X,X" 但我想做的是 IF music.php?word=A&page=1 set $ query = "SELECT * FROM m3_music_mp3 WHERE title LIKE '$word%' ORDER BY id DESC LIMIT X,X"

我试过了

if(isset($_GET['word']) && isset($_GET['page'])) {
$limit .= 'WHERE....'
}

类似但不起作用的东西。谢谢。很抱歉我的英语不好和我的 php 知识很差

【问题讨论】:

    标签: php mysql post pagination get


    【解决方案1】:

    基本上sAc是正确的。但你可以减少你的代码。原因限制不是可选的,这应该可以正常工作: 编辑将代码位更改为可复制和可测试;)

    <?
    if (isset($_GET['page']) ) {
       $pageno = $_GET['page'];
    } else {
       $pageno = 1;
    } // if
    $limit = "";
        if(isset($_GET['word'])) {
          $word = mysql_real_escape_string($_GET['word']);
    
          $limit = " WHERE title LIKE '%$word%'"
        }
    $query = mysql_query("SELECT count(id) FROM m3_music_mp3" . $limit);
    $query_data = mysql_fetch_row($query);
    $numrows = $query_data[0];
    $rows_per_page = 30;
    $lastpage      = ceil($numrows/$rows_per_page);
    $pageno = (int)$pageno;
    if ($pageno > $lastpage) {
       $pageno = $lastpage;
    } // if
    if ($pageno < 1) {
       $pageno = 1;
    } // if
    $limit .= ' ORDER BY id DESC LIMIT ' .($pageno - 1) * $rows_per_page .',' .$rows_per_page;
    $query = mysql_query("SELECT * FROM m3_music_mp3 $limit");
    ?>
    

    【讨论】:

    • 好的。这也有效。谢谢。但现在分页不能正常工作分页仍然适用于所有数据而不是多少标题行:S
    • 获取 isset 字位并将其放在 $query = mysql_query("SELECT count(id) FROM m3_music_mp3");更改 $query = mysql_query("SELECT count(id) FROM m3_music_mp3"); to $query = mysql_query("SELECT count(id) FROM m3_music_mp3" . $limit);
    【解决方案2】:

    应该是:

    if(isset($_GET['word'])) {
      $word = mysql_real_escape_string($_GET['word']);
    
      $limit = " WHERE title LIKE '%$word%' ORDER BY id DESC LIMIT " .($pageno - 1) * $rows_per_page .',' .$rows_per_page;
    }
    else
    {
      $limit = " ORDER BY id DESC LIMIT " .($pageno - 1) * $rows_per_page .',' .$rows_per_page;
    }
    
    $query = mysql_query("SELECT * FROM m3_music_mp3 $limit") or die(mysql_error());
    

    【讨论】:

    • 感谢您的评论。它工作正常。但是我怎样才能增加 $_GET 的呢?使用 elseif (isset) 的东西应该有用吗?
    【解决方案3】:

    您的解决方案应该可以正常工作,也许您只需要在 WHERE 之前留一个空格,例如 .= ' WHERE... 打印出您的最终查询字符串,看看哪里出错了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-02-22
      • 1970-01-01
      • 1970-01-01
      • 2013-11-23
      • 2019-05-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多