Jump to content

need a little help replacing a comma with apostrophe comma apostrophe


sptrsn

Recommended Posts

It's probably not the right way, but it'll do for now. This is part of a query builder.

 

I want to replace a comma with apostraphe comma apostraphe.

 

in other words this......

 

city1, city2, city3

 

becomes

 

city1', 'city2', 'city3

 

Ultimately this is helping me build a dynamic query that will read.....

select * from table where city in ('city1','city2','city3')

the surrounding parens and apostraphes are in the whereclause[] statement, so I don't need them here.

 

I've tried a multiple iterations, but they all end up looking similar to this, which obviously doesn't work...

$city= preg_replace(",", "', '", $city); 

 

I appreciate any help.

 

Link to comment
Share on other sites

I get the following error...

A fatal MySQL error occured.

Query:

Error: (1136) Column count doesn't match value count at row 1

 

Not completely sure why. My insert statement works fine without the str_replace.

I've been able to get it to insert several city names with the commas.

 

Is there something about the apostrophe that makes mysql think it's an additional column?

The field is varchar 200 (innodb)

Link to comment
Share on other sites

You're correct. That statement works great. But the moment I add the insert statement back in, I get that same error.

 

Is there something about this value ......"city1',' city2',' city3 " that would cause a problem that you're aware of?

Link to comment
Share on other sites

yea, that worked great. Thank you very much for your help.

I just insert the comma separated values in the db. (cleaner that way anyway)

Then when I build the query, I applied your str_replace right before building the array.

 

if ($row['city'] !='')
$city = str_replace(',',"','",$row['city']);								
$whereClause[]="prop_city in ('".$city."')";

 

Thanks again.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.