Newbie select statement questions 'WHERE'

This is a discussion on Newbie select statement questions 'WHERE' within the PHP General forums, part of the PHP Programming Forums category; NEWBIE! I have some GET data coming from a previous search form. How do I add the WHERE part ? orig: $...


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-24-2008
revDAVE
 
Posts: n/a
Default Newbie select statement questions 'WHERE'

NEWBIE! I have some GET data coming from a previous search form.

How do I add the WHERE part ?

orig:

$query_get1 = "SELECT p_First, p_id, p_Last, p_Lvl, p_Sel
FROM contacts";

------W / WHERE...???

$query_get1 = "SELECT p_First, p_id, p_Last, p_Lvl, p_Sel
FROM contacts
WHERE p_First like $_GET['p_First'] or p_Last like $_GET['p_Last']";


I tried various things that make errors:

where p_First like '%$_GET['p_First ']%'";
where p_First like ".$_GET['p_First ']."";
Etc...

How can I make this work?

--
Thanks - RevDave
Cool @ hosting4days . com
[db-lists]



Reply With Quote
  #2 (permalink)  
Old 04-24-2008
Robert Cummings
 
Posts: n/a
Default Re: [PHP] Newbie select statement questions 'WHERE'


On Wed, 2008-04-23 at 19:07 -0700, revDAVE wrote:
> NEWBIE! I have some GET data coming from a previous search form.
>
> How do I add the WHERE part ?
>
> orig:
>
> $query_get1 = "SELECT p_First, p_id, p_Last, p_Lvl, p_Sel
> FROM contacts";
>
> ------W / WHERE...???
>
> $query_get1 = "SELECT p_First, p_id, p_Last, p_Lvl, p_Sel
> FROM contacts
> WHERE p_First like $_GET['p_First'] or p_Last like $_GET['p_Last']";
>
>
> I tried various things that make errors:
>
> where p_First like '%$_GET['p_First ']%'";
> where p_First like ".$_GET['p_First ']."";
> Etc...
>
> How can I make this work?


<?php

$escape = 'mysql_real_escape_string';

$query_get1 =
"SELECT "
." p_First, "
." p_id, "
." p_Last, "
." p_Lvl, "
." p_Sel "
."FROM "
." contacts "
."WHERE "
." p_First LIKE '".$escape( $_GET['p_First'] )."' "
." OR "
." p_Last LIKE '".$escape( $_GET['p_Last'] )."' ";

?>

But really... you're not doing any partial matching so don't bother with
'LIKE'. Just do the following:

<?php

$query_get1 =
"SELECT "
." p_First, "
." p_id, "
." p_Last, "
." p_Lvl, "
." p_Sel "
."FROM "
." contacts "
."WHERE "
." p_First = '".$escape( $_GET['p_First'] )."' "
." OR "
." p_Last = '".$escape( $_GET['p_Last'] )."' ";

?>

Cheers,
Rob.
--
http://www.interjinn.com
Application and Templating Framework for PHP

Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 10:26 AM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0