Re: [PHP] searching through a mysql db/tbl

2005-09-10 Thread Stephen Johnson



On 9/10/05 3:13 PM, bruce [EMAIL PROTECTED] wrote:

 if i allow a user to search on say 'aa', i'd like the user to be able to
 get:
 
   name   email   foo...
   aa [EMAIL PROTECTED] 
   b1 [EMAIL PROTECTED]123
 
 any ideas as to how i could go about and create the query, or what would i
 need to do to have this result...

Use like in your select statement ...

Select name, email, foo from tbl where email like '%aa%';



 2) if i have a query that produces a number of rows, how/what would i need
 to do, to limit the number of rows displayed, and to allow the user to
 select a 'back/next' button that would generate/display the next 'N' items
 in the list/query results...
 

Use limit in your select statement

Select * from tbl limit 10;

You can also use a start in the limit so the next button would call this
select. 

Select * from tbl limit 10, 10;

 if anybody could direct me to sample docs/code that kind of describes/solves
 what i've described, i'd appreciate it!!!

The folks on the MySQL list can help you with these questions better then
the folks here on the PHP list.



 
 thanks
 
 -bruce
 [EMAIL PROTECTED]

-- 
Stephen Johnson
The Lone Coder

http://www.ouradoptionblog.com
*Join us on our adoption journey*

[EMAIL PROTECTED]
http://www.thelonecoder.com

*Continuing the struggle against bad code*
--

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] searching through a mysql db/tbl

2005-09-10 Thread Murray @ PlanetThoughtful
 hi...
 
 i'm trying to figure out how to approach/solve a few issues. looking
 through
 google hasn't made the light shine!!
 
 1) i'm trying to figure out how to allow a user to search through a
 query/tbl for a given string. ie, if i have the following as the result of
 a
 query:
 
   name   email   foo...
   aa [EMAIL PROTECTED] 
   b1 [EMAIL PROTECTED]123
   bb [EMAIL PROTECTED]qwe
 
 
 if i allow a user to search on say 'aa', i'd like the user to be able to
 get:
 
   name   email   foo...
   aa [EMAIL PROTECTED] 
   b1 [EMAIL PROTECTED]123
 
 any ideas as to how i could go about and create the query, or what would i
 need to do to have this result...

Hi,

Basically what you need to do is dynamically create the WHERE clause of your
query string.

In the example above, the WHERE clause might look something like:

$qry = SELECT * FROM table WHERE name LIKE '%$searchterm%' OR email LIKE
'%$searchterm%';

If you want to span the search across more fields, simply add them as extra
OR elements to the WHERE clause. 


 
 2) if i have a query that produces a number of rows, how/what would i need
 to do, to limit the number of rows displayed, and to allow the user to
 select a 'back/next' button that would generate/display the next 'N' items
 in the list/query results...
 
 if anybody could direct me to sample docs/code that kind of
 describes/solves
 what i've described, i'd appreciate it!!!

Here I'm making the assumption that you're using MySQL. If that's the case,
you need to familiarize yourself with the LIMIT clause. This allows you to
specify a starting point and number of rows to return for the resultset.

So, using the query above again:

$qry = SELECT * FROM table WHERE name LIKE '%$searchterm%' OR email LIKE
'%$searchterm%' LIMIT 0,10;

...will return the first 10 results from your query (records 0 to 9). Note
that the 'first' row is at position 0 in the recordset. Also note: if there
are less than 10 records returned by your query (ie in your example, only 2
match your pseudo request), only those records will be returned.

Then, issuing:

$qry = SELECT * FROM table WHERE name LIKE '%$searchterm%' OR email LIKE
'%$searchterm%' LIMIT 9,10;

...will return the next 10 results from your query (records 10 to 19), and
so on.

This requires you to pass some variables from one search result page to the
next, particularly the variable that indicates where the 'next' results
should begin, allowing you to factor that in when building the LIMIT clause
of the query string for the search results that should be displayed on that
page.

A Google search on PHP pagination or PHP paginate should return a number
of online resources explaining how to paginate results returned from a db
query in PHP.

Hope this helps.

Much warmth,

Murray
---
Lost in thought...
http://www.planetthoughtful.org

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php