String replace with single quotes - php/mysql

Asked

Viewed 7,655 times

-1

Hello!

I’m doing an Insert/update in a database through a csv file. I have problems when I try to upload the file and it finds data with simple quotes, for example: "SANTA BARBARA D'OESTE".

My Internet has stayed:

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

Then I thought to make a string_replace to see if it worked, with simple quotes does not work, because he understands that it is an open quote:

    $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 quotes also did not roll, even because the syntax is with single quotes right

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

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

  • 1

    I believe it is the same problem, properly solved: Problem with php query because of quotation marks

  • Since there was no other activity in the post, I closed it as a duplicate. I understand that the solution is on the link indicated, but if you have any questions, leave a comment that we will try to help.

1 answer

1

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

The difference between double and single quotes:

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

About the syntax, it is correct. Copy this code snippet and use this site to execute (http://phpfiddle.org/).

$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 so that later you could treat this value. So it would show the real value.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.