Jump to content

Archived

This topic is now archived and is closed to further replies.

pocobueno1388

Problem with query...

Recommended Posts

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:

[code]
$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);
[/code]

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

[code]

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

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

[/code]

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

[code]
$result = mysql_query($query);
[/code]

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

Share this post


Link to post
Share on other sites
Add
[code]$query.="  LIMIT $from, $max_results";[/code]
before
[code]$result = mysql_query($query);[/code]

calling mysql_query($query) runs the query, so you have to prepare desired query with limits before you execute it.

Share this post


Link to post
Share on other sites
That didn't give me any huge errors, so I am sure it will work. But it created a small one.

[code]
Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result
[/code]

Code:

[code]
$row = mysql_fetch_assoc($result);
[/code]

Thanks for the help :)

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

[code]
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);
[/code]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.