String replace with single quotes - php / mysql [duplicate]

-1

Hello

I'm doing an insert / update to a database through a csv file. I am having problems when I try to upload the file and it is given with single quotation marks, such as "SANTA BARBARA D'OESTE".

My insert is:

        $sql = mysql_query( 
        "INSERT INTO tabela SET
        'dado1'= '$data[0]',
        'dado2'= '$data[1]',
        'dado3'= '$data[2]'")

I thought of doing a string_replace to see if it worked, with single quotation marks does not work, because it understands that it is an open quotation mark:

    $dado1= $data[1];
    $dado_alt = str_replace(''','',$dado1);

        //ai o insert ficaria assim:
        "INSERT INTO tabela SET
        'dado1'= '$dado1',
        'dado2'= '$data[1]',
        'dado3'= '$data[2]'")

And with double quotation marks also did not scroll,

    $dado1= $data[1];
    $dado_alt = str_replace("'","",$dado1);

What can I do to fix this? Thank you !!

    
asked by anonymous 01.06.2016 / 21:13

1 answer

0

php accepts both double quotation marks and single quotation marks for a string. In your case it will not work with single quotation marks, because the compiler will interpret that you are opening one more quotes.

The difference between double and single quotes:

Double quotes: "{$data[1]}" or "$dado_alt"; // accepts printa by the variable Home Single quotation marks: 'apenas string'; // accepts string only

As for the syntax, it is correct. Copy this snippet of code and use that site to run ( link ).

$string  = "SANTA BARBARA D'OESTE";
<br>
$dado_alt = str_replace("'","",$string);
<br>
echo $dado_alt;

In my opinion instead of removing, you could put a special character to later be able to handle that value. This would show the real value.

    
01.06.2016 / 21:34