1
$brand_condition = ' AND ' . mysql_real_escape_string($brand_selection) . ' IN ';

$brand_condition .= $quote10 . '"'. mysql_real_escape_string($brand_value) . '"' .$quote9;

$brand_conditions[] = $brand_condition;

$query .= implode(' AND ', $brand_conditions) . '';

This produces: AND manufacturer IN ("brand1,brand2")

Since I'm using the IN statement, I need the values to be quoted. At the same time, I am escaping potential quotes with mysql_real_escape_string.

Does anyone see a simple way to get around this small problem?

5 Answers 5

3
function quote_escape(&$str) {
    $str = '"' . mysql_real_escape_string(chop($str)) . '"';
}

$brands = explode(',', $brand_value);
array_walk($brands, "quote_escape");
$brands = implode(',', $brands);

or

function quote_escape($str) {
     return '"' . mysql_real_escape_string(chop($str)) . '"';
}
$brands = implode(',', array_map("quote_escape", explode(',', $brand_value)));
Sign up to request clarification or add additional context in comments.

4 Comments

change it from array_walk to array_map, remove the repeated assignments, and you have my upvote.
I'm not sure that's a readable solution for the OP, but as you wish :P
Where exactly is the AND statement supposed to be placed?
$query .= ('AND manufacturer IN'. '('. $brands).')'; --Nevermind
2

How about $brand_conditions[] = '"'.$brand_condition.'"'; so your adding quotes right before you add the brand_condition in your array.

Comments

1
$concurrent_names = array("O'reilly", 'Tupac "MC New York" Shakur', 'Nemoden');
$escaped_concurrent_names = array_map('mysql_real_escape_string', $concurrent_names);
$condition = 'WHERE name in ("'.implode('", "', $escaped_concurrent_names).'")';

Comments

0

Use this to add quotes for imploded string.

$values = implode( " ',' ", array_values($values) );

Comments

-1
$brands=array(nokia,samsung,xiomi);

$brands=implode(" ',' ",$brand);

//$brands='nokia','samsung','xiomi';

WHERE column_name IN ($brands)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.