Jump to content


Photo

Problem with query...


  • Please log in to reply
2 replies to this topic

#1 pocobueno1388

pocobueno1388
  • Members
  • PipPipPip
  • Advanced Member
  • 3,369 posts
  • LocationPalm Beach, Florida

Posted 24 September 2006 - 04:20 AM

I got some help with this script the other day, but now I am trying to add pagination to the page. This is just a search script where users can type in information and it will pull it from the database and display the matching results, but that is not important.

The query is written in a way where I can't really add something to it, here is the part of the code that I'm talking about:

$query = "SELECT `playerID`, `username`, `online`, `upgrade` FROM `players` WHERE 1";

if($username) $query.=" AND `username` = '$username'";
if($id) $query.=" AND `playerID` = '$id'";

$result = mysql_query($query);

To do pagination I would need to make the query like this:


$max_results = 10;
$from = (($page * $max_results) - $max_results);

$result = mysql_query("SELECT row FROM table WHERE condition LIMIT $from, $max_results");


But as you can see I am kinda stuck having to make my $reslut query look like this:

$result = mysql_query($query);

I can't add 'LIMIT $from, $max_results' anywhere after the mysql_query($query).

I am sure there is a way, I just can't figure it out. Any help is appreciated =) Thanks

#2 invincible_virus

invincible_virus
  • Members
  • PipPip
  • Member
  • 27 posts
  • LocationDelhi, India

Posted 24 September 2006 - 04:30 AM

Add
$query.="  LIMIT $from, $max_results";
before
$result = mysql_query($query);

calling mysql_query($query) runs the query, so you have to prepare desired query with limits before you execute it.
PG accommodations in India - http://www.pgspace.com/
Technical Forum - http://www.enterpriseplatform.net/

#3 pocobueno1388

pocobueno1388
  • Members
  • PipPipPip
  • Advanced Member
  • 3,369 posts
  • LocationPalm Beach, Florida

Posted 24 September 2006 - 04:36 AM

That didn't give me any huge errors, so I am sure it will work. But it created a small one.

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result

Code:

$row = mysql_fetch_assoc($result);

Thanks for the help :)

EDIT: I guess it might be helpful if I post the new code, hah.

if($username) $query.=" AND `username` = '$username'";
if($id) $query.=" AND `playerID` = '$id'";

$max_results = 10;
$from = (($page * $max_results) - $max_results);

$query.="  LIMIT $from, $max_results";

$result = mysql_query($query);

$row = mysql_fetch_assoc($result);





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users