Web Design Forums

Welcome! Please register or log in: Forgot your password? Why register?
You are here: Web Design Forums » Programming Help » PHP » Pagination problems WHERE clause not working RSS

Pagination problems WHERE clause not working

This thread was started by medlington and has been viewed 332 times, and contains 4 replies, with the last reply made by smoseley.
Post Reply
1
36 points at 100%
medlington, WDF Addict! Home page   Private message  
Posted February 8 '10 at 09:25 AM
      Posts: 160
Hi,

Im having some issues with a search querey that is using pagination and a WHERE clause.

If I just use the querey without the WHERE clause it works fine:

$sql "SELECT * FROM admin ORDER BY $orderby ASC LIMIT $start_from, $display_no"
$rs_result mysql_query ($sql$conn); 

but if I add the WHERE clause in:
$sql "SELECT * FROM admin ORDER BY $orderby WHERE $table_name = '$search_term' ASC LIMIT $start_from, $display_no"
$rs_result mysql_query ($sql$conn); 

I get this error:

Warningmysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/fhlinux129/m/mysql.atumstudios.com/user/htdocs/backend/logged/admin_search_records.php on line 109 

Any ideas?

I've tried moving the WHERE clause around the statement but seem to get the same error.

I did try removing the pagination part of the code and keeping the WHERE clause and that seemed to work its just getting them both to work together.

Sorry if this should be in the SQL forum.

Advertisement Register for free to hide these ads and participate in discussions!

2
1,325 points at 100% Repute WDFplus Member
Posted February 8 '10 at 10:57 AM
      Posts: 8,434
I see a few problems.


1. "ORDER BY" should go after "WHERE"
2. "ASC" should immediately follow "ORDER BY"
3. Your variable name is "$table_name", which concerns me.. you can't do a where clause on a table name... only a column name.
4. PHP variables inline in a string should always contain braces {} around them to prevent confusion in the compiler.

Try this:
$sql "SELECT * 
        FROM admin 
        WHERE {$column_name} = '{$column_value}' 
        ORDER BY {$orderby} ASC 
        LIMIT {$start_from}, {$display_no}"

Steven Moseley
President, Transio

3
1,325 points at 100% Repute WDFplus Member
Posted February 8 '10 at 11:01 AM
      Posts: 8,434
By the way, you need some error checking in your code to ensure that your SQL isn't causing problems.

something like:
$rs mysql_query($sql);
if (
mysql_error()) {
    throw new 
Exception(mysql_error());


Of course, this should go in a function called by a larger scope that gracefully handles such errors...
Steven Moseley
President, Transio

4
36 points at 100%
medlington, WDF Addict! Home page   Private message  
Posted February 10 '10 at 08:50 AM
      Posts: 160
Thanks,

That seems to be working now. Now I've just got to worry about the error checking

do you have any links to tutorials on error checking that youd recommend?

I've found this one: http://forums.mysql.com/read.php?98,54059,54140

is that the type of error checking you were refering to? Im learnig this as I go along and just trying to not make too many mistakes so any help is very appreciated.

5
1,325 points at 100% Repute WDFplus Member
Posted February 10 '10 at 08:54 AM
      Posts: 8,434
No, that's handling the error at the SQL level... you only need to handle at the PHP level by degrading gracefully if an error is encountered.

Read up on PHP Exceptions and try... catch statements for details on how to implement error handling in your code.
Steven Moseley
President, Transio

Post Reply

Similar Threads
Thread Thread Starter Forum Replies Last Post
Parse and replace with function call straight_up PHP 9 December 1 '05 07:04 PM
Converting site to CSS - Problems! will7 HTML and CSS Help 4 October 25 '05 02:10 PM
Table Width within DIV causing problems in IE lucifersam HTML and CSS Help 0 November 1 '04 02:45 PM
SP2 Problems ! Snoogens General Discussion 4 September 8 '04 05:56 AM
Problems, Problems, Problems Burnerp Forum Feedback 6 July 31 '04 07:13 PM