473,395 Members | 1,343 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

help with str_replace in select statement

Hi group!
Why str_replace does not work in this case?
Thanks in advance!

if ($cat0 != ''){ $query_cat0 = "cat0 LIKE '$cat0%'"; }
if ($cat1 != ''){ $query_cat1 = "AND cat1 LIKE '$cat1%'"; }
if ($cat2 != ''){ $query_cat2 = "AND cat2 LIKE '$cat2%'"; }

$mysql_query = "SELECT * FROM mytable WHERE ".$query_cat0." ".
$query_cat1." ".$query_cat2;
$what = 'WHERE AND';
$with = 'WHERE';

$mysql_query = str_replace($what, $with, $mysql_query);

echo $mysql_query;

Sep 6 '07 #1
2 2208
On Thu, 06 Sep 2007 14:03:22 +0200, 4sgcv5hy6d0r
<fj*************@temporaryinbox.comwrote:
Hi group!
Why str_replace does not work in this case?
Thanks in advance!

if ($cat0 != ''){ $query_cat0 = "cat0 LIKE '$cat0%'"; }
if ($cat1 != ''){ $query_cat1 = "AND cat1 LIKE '$cat1%'"; }
if ($cat2 != ''){ $query_cat2 = "AND cat2 LIKE '$cat2%'"; }

$mysql_query = "SELECT * FROM mytable WHERE ".$query_cat0." ".
$query_cat1." ".$query_cat2;
$what = 'WHERE AND';
$with = 'WHERE';

$mysql_query = str_replace($what, $with, $mysql_query);

echo $mysql_query;

Because the string is 'WHERE AND', not 'WHERE AND', notice the double
space. You could use a regex matching whitespace, clearly overkill. I'd do
it like this:

$where_clauses = array();

if ($cat0 != ''){ $where_clauses[] = "cat0 LIKE '$cat0%'"; }
if ($cat1 != ''){ $where_clauses[] = "cat1 LIKE '$cat1%'"; }
if ($cat2 != ''){ $where_clauses[] = "cat2 LIKE '$cat2%'"; }

$mysql_query = "SELECT * FROM mytable";
if(!empty($where_clauses)) $mysql_query .= ' WHERE '.implode(' AND
',$where_clauses);
echo $mysql_query;
--
Rik Wasmus
Sep 6 '07 #2
On 6 Sep., 14:19, "Rik Wasmus" <luiheidsgoe...@hotmail.comwrote:
On Thu, 06 Sep 2007 14:03:22 +0200, 4sgcv5hy6d0r

<fj4sgcv5hy6d...@temporaryinbox.comwrote:
Hi group!
Why str_replace does not work in this case?
Thanks in advance!
if ($cat0 != ''){ $query_cat0 = "cat0 LIKE '$cat0%'"; }
if ($cat1 != ''){ $query_cat1 = "AND cat1 LIKE '$cat1%'"; }
if ($cat2 != ''){ $query_cat2 = "AND cat2 LIKE '$cat2%'"; }
$mysql_query = "SELECT * FROM mytable WHERE ".$query_cat0." ".
$query_cat1." ".$query_cat2;
$what = 'WHERE AND';
$with = 'WHERE';
$mysql_query = str_replace($what, $with, $mysql_query);
echo $mysql_query;

Because the string is 'WHERE AND', not 'WHERE AND', notice the double
space. You could use a regex matching whitespace, clearly overkill. I'd do
it like this:

$where_clauses = array();

if ($cat0 != ''){ $where_clauses[] = "cat0 LIKE '$cat0%'"; }
if ($cat1 != ''){ $where_clauses[] = "cat1 LIKE '$cat1%'"; }
if ($cat2 != ''){ $where_clauses[] = "cat2 LIKE '$cat2%'"; }

$mysql_query = "SELECT * FROM mytable";
if(!empty($where_clauses)) $mysql_query .= ' WHERE '.implode(' AND
',$where_clauses);
echo $mysql_query;
--
Rik Wasmus
Thank you very much! Browser shows only one space. Now I know it
again ;)

Sep 6 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Jon | last post by:
Why does the following not replace the ":)"? str_replace(":)", "&nbsp;<img src=http://www.com.com/forum/emoticons/smiley.gif>&nbsp;", $_POST); But, the following does replace the ":("? ...
3
by: JDJones | last post by:
I'm having some trouble getting this replacement to work efficiently. I have the following code: $thing = preg_replace("(\r\n|\n|\r|\t)", "", $thing1); What I want to do now is to get it to...
0
by: Dafella | last post by:
The following is code from Xeoport. It is suppose to access my pop3 account and load email into Mysql database. It's not inserting and there is no log or anything to tell me why. Here is the...
6
by: Aaron Collins | last post by:
Hello, i have a string that was entered in a web form and stored in a mysql database where the user entered carriage returns as well as <br>'s after each line in the form. the data is stored as...
0
by: Baby Blue | last post by:
I have 2 code like below to grab a news website for my site. However, when I click some links (such as : http://wwww.vnexpress.net/xxx/xxxx ) inside the site which I want to grab, it has some...
3
by: Baby Blue | last post by:
I have 2 codes below to grap data from another site. I use them to get the data from one News site. However, when I click on some link inside (such as :...
7
by: ojsimon | last post by:
Hi I found this script on a forum and have been trying to make it work, but all it returns is a blank screen, i tried using the debug error reporting but got nothing from that either just a blank...
15
by: fjm | last post by:
Hello everyone and happy Sunday. :) I have a problem that I *think* I may know the solution to but have no idea how to write the code for it. I am working on a templating system wher I have...
4
by: mattehz | last post by:
Hey there, I am trying to upload old source files and came across these errors: Warning: Invalid argument supplied for foreach() in /home/mattehz/public_html/acssr/trunk/inc_html.php on line 59...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.