Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

If I have the following array $array[0] = array( "1" => bar, "2" => foo, "3" => 13546 ); and I implode() it, the value that is returned will be: bar,foo,13546 which cannot be used in a mysql query... How can I place single quotes just to those values that are strings...

I've tryed a couple of ways (like foreach($array as $key=>$value) to check with is_numeric() the $value, and the check is ok but I dont know how to change the value to '$value'...)

Any toughts on this?

EDIT

I found another way to do this for those of you who are interested:

$result[0] = array(
    "1" => bar,
    "2" => foo,
    "3" => 1232.13
);

$copy_r = $result[0];

foreach($copy_r as $key=>$value)
{
    if(!is_numeric($value))
    {
        $insert_array[] = "`$key` = '$value'";
    }
    else
    {
        $insert_array[] = "`$key` = $value";
    }
}

$final_string = implode(',', $insert_array);
$insert_q = "INSERT INTO `table_name` SET $final_string
             ON DUPLICATE KEY UPDATE ($final_string)";
share|improve this question
6  
That's not how you want to do this. You should be using prepared queries. –  John Conde Feb 3 '14 at 14:04
    
could you please elaborate? –  Ares Draguna Feb 3 '14 at 14:04
1  
Google can, prepared statements –  u_mulder Feb 3 '14 at 14:05
    
take a look at uk1.php.net/pdo and prepared statements. –  Andrew Feb 3 '14 at 14:06
    
also see here: uk1.php.net/manual/en/pdostatement.bindvalue.php –  Andrew Feb 3 '14 at 14:08

4 Answers 4

up vote 3 down vote accepted

Better use prepared queries. But just for funs sake:

implode(',', array_map(function($value) {
    if(!is_numeric($value)) {
        return '"' . $value . '"';
        //adds double quotes, but if you prefer single quotes, use:
        //return "'" . $value . "'";
    } else {
        return $value;
    }
}, $array[0]);
share|improve this answer
1  
Thanks ... solves my issue. –  Ares Draguna Feb 3 '14 at 14:15

The foreach+isnumeric() is probably the best approach. Then to change the value:

$array[$key] = "'" . $value . "'";

which turns foo/"foo" into 'foo'/"'foo'" and so on.

share|improve this answer

Agree that you should look at prepared statements, however to answer your original question you can do that like this:

$array=array('a', 'b', 'c');

$string = "'" . implode("','", $array) . "'";
share|improve this answer
    
He wants to surround only string values with ', numeric values are supposed to remain blank. –  scenia Feb 3 '14 at 14:10

If you use the mysql-driver:

function escape( $x ) {
 return "'" . mysql_real_escape_string($x) . "'";
}

$data = array( "1" => bar, "2" => foo, "3" => 13546 );

echo implode( ',', array_map('escape', $data) );
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.