Figuring SQL QUERY for EACH ISSET?

Hello guys I'm stucked in a point and I need your help. I want to change SQL QUERY for each $_GET parameters. I'm using this:

<?
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");
?>

This does IF music.php?page=1 is set $query = "SELECT * FROM m3_music_mp3 ORDER BY id DESC LIMIT X,X" but I want to do is 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"

I tried

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

something like but does not works. thank you. and sorry for bad english & poor php knowledge of me

Answers


basically sAc is right. but you can reduce your code. cause limit isnt optional this schould work just fine: Edit changed the code bit to be copy- and testable ;)

<?
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");
?>

It should be:

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());

Your solution should work fine, maybe you just need a space before WHERE, like .= ' WHERE... print out your final query string to see where it is going wrong.


Need Your Help

API to install driver from driverstore

windows winapi driver modem

Is there any APIs available to install the driver for my device say for example (PNPC031) from the driverstore or from the inf folder?