Re: [PHP] Newbie: PHP/MySQL (SELECT)
if one of $titulotxt or $cdstxt is not set, your query will look something like this: SELECT * FROM divx WHERE && cds like '$cdstxt' ORDER BY titulo As you see, there is unnecessery &&. I build my search queries using this form: $query_cond=''; foreach($_GET as $col => $val) { switch($col) { case 'cds': // you can add more cases here for conditions that need to be exact $query_cond .= " $col LIKE '$val' AND "; break; case 'titulo': // you can add more cases here for conditions that need not to be exact $query_cond .= " $col LIKE '%$val%' AND "; break; } } // get rid of final AND $query_cond = substr($query_cond, 0, strlen($query_cond) - 4); // and as you don't have any other conditions, $query_cond cannot be empty - we would have excessive WHERE // so if it is empty, make it 1 if($query_cond=='') $query_cond='1'; $sql="SELECT * FROM divx WHERE $query_cond ORDER BY titulo"; Mr. BuNgL3 wrote: Hi... I'm with a little sintax problem... The question is that i have two search fields (titulotxt and cdstxt) and i want to create an mysql condition... i trying: $sql1=($titulotxt) ? "titulo like '%".$titulotxt."%'":""; $sql2=($cdstxt) ? "cds like '$cdstxt'":""; $sql="SELECT * FROM divx WHERE" .$sql1 " && " .$sql2 " ORDER BY titulo"; but he's giving me a sintax error on the 3 line... Can anyone teach me how i must do to validate the mysql condition and make it work? Thanks -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Newbie: php/mysql (Select)
At 20:23 20.11.2002, Mr. BuNgL3 said: [snip] >Hi... >I'm with a little sintax problem... >The question is that i have two search fields (titulotxt and cdstxt) and i >want to create an mysql condition... i trying: > > $sql1=($titulotxt) ? "titulo like '%".$titulotxt."%'":""; > $sql2=($cdstxt) ? "cds like '$cdstxt'":""; > $sql="SELECT * FROM divx WHERE" .$sql1 " && " .$sql2 " ORDER BY titulo"; > >but the bastard is giving me a sintax error on the 3 line... Can anyone >teach me how i must do to validate the mysql condition and make it work? [snip] As already said, put a space after the WHERE clause. What happens if $sql1 or $sql2 are empty (as your example provisons)? Create an $sql3 that combines $sql1 and $sql2, and construct your SQL accordingly: $sql1 = ($titulotxt ? null : "titulo like '%$titulotxt%'"); $sql2 = ($cdstxt? null : "cds like '$cdstxt'"); $sql3 = $sql1 . ($sql1 && $sql2 ? ' AND ' : null) . $sql2; $sql = 'SELECT * FROM divx ' . ($sql3 ? "WHERE $sql3 " : null) . 'ORDER BY titulo'; -- >O Ernest E. Vogelsinger (\)ICQ #13394035 ^ http://www.vogelsinger.at/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Newbie: PHP/MySQL (SELECT)
$sql1 = "titulo like '%$titulotxt%'" $sql2 = "cd like '$cdstxt'"; $sql = "SELECT * FROM divx WHERE $sql1 AND $sql2 ORDER BY titulo"; I think you are getting the error because of the ($titulotxt) ? portion of your sql statements. You really only need the two statements listed above and then put into your actual sql statement. Robbert van Andel -Original Message- From: Mr. BuNgL3 [mailto:[EMAIL PROTECTED]] Sent: Wednesday, November 20, 2002 11:54 AM To: [EMAIL PROTECTED] Subject: [PHP] Newbie: PHP/MySQL (SELECT) Hi... I'm with a little sintax problem... The question is that i have two search fields (titulotxt and cdstxt) and i want to create an mysql condition... i trying: $sql1=($titulotxt) ? "titulo like '%".$titulotxt."%'":""; $sql2=($cdstxt) ? "cds like '$cdstxt'":""; $sql="SELECT * FROM divx WHERE" .$sql1 " && " .$sql2 " ORDER BY titulo"; but he's giving me a sintax error on the 3 line... Can anyone teach me how i must do to validate the mysql condition and make it work? Thanks -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php "The information transmitted is intended only for the person or entity to which it is addressed and may contain confidential and/or privileged material. Any review, retransmission, dissemination or other use of, or taking of any action in reliance upon, this information by persons or entities other than the intended recipient is prohibited. If you received this in error, please contact the sender and delete the material from all computers."
Re: [PHP] Newbie: PHP/MySQL (SELECT)
> On Thursday 21 November 2002 03:53, Mr. BuNgL3 wrote: > > Hi... > > I'm with a little sintax problem... > > The question is that i have two search fields (titulotxt and cdstxt) and i > > want to create an mysql condition... i trying: > > > > $sql1=($titulotxt) ? "titulo like '%".$titulotxt."%'":""; > > $sql2=($cdstxt) ? "cds like '$cdstxt'":""; > > $sql="SELECT * FROM divx WHERE" .$sql1 " && " .$sql2 " ORDER BY titulo"; > > > > but he's giving me a sintax error on the 3 line... Can anyone > > teach me how i must do to validate the mysql condition and make it work? > > Try: > > $sql="SELECT * FROM divx WHERE" .$sql1. " && " .$sql2. " ORDER BY titulo"; and add a space after the WHERE. Also consider the positioning of the single quotes (') and double-quotes (") in the $sql1 assignment statement - they must be nested. Finally, consider echoing sql1, $sql2, and $sql to be able to see with your own eyes! =dn -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Newbie: PHP/MySQL (SELECT)
On Thursday 21 November 2002 03:53, Mr. BuNgL3 wrote: > Hi... > I'm with a little sintax problem... > The question is that i have two search fields (titulotxt and cdstxt) and i > want to create an mysql condition... i trying: > > $sql1=($titulotxt) ? "titulo like '%".$titulotxt."%'":""; > $sql2=($cdstxt) ? "cds like '$cdstxt'":""; > $sql="SELECT * FROM divx WHERE" .$sql1 " && " .$sql2 " ORDER BY titulo"; > > but he's giving me a sintax error on the 3 line... Can anyone > teach me how i must do to validate the mysql condition and make it work? Try: $sql="SELECT * FROM divx WHERE" .$sql1. " && " .$sql2. " ORDER BY titulo"; -- Jason Wong -> Gremlins Associates -> www.gremlins.biz Open Source Software Systems Integrators * Web Design & Hosting * Internet & Intranet Applications Development * /* He thinks by infection, catching an opinion like a cold. */ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php