DESCRIPTION
I want to be able to eliminate special characters in mysql. I WAS using mysql_real_escape_string but I was told it was not correct, and soon to be discontinued
Question
What is an alternative for using mysql_real_escape_string
in my case below? Or is there another approach?
ISSUE
I am parsing CSV form URL, and creating INSERT statements. In my case I'm reading in data from an CSV via URL, examples online don't take into account $f_pointer
.
Here is an example of one of data lines to insert:
Note: the '
in Green's Mill
is causing the issue.
INSERT INTO scores(place,person,score,grade,round,location)values('-','Alex','2','5','','Green's Mill')
My Failed Approach for solving the ISSUE:
I was using mysql_real_escape_string - clearly badly
SCRIPT - Parse Data & Create Insert
<?php
$servername = "localhost";
$username = "root";
$password = "XXXX";
$dbname = "player";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
$f_pointer=fopen("compress.zlib://https://www.url.com/api/v2/projects/xxxx/last_ready_run/data?api_key=xxx&format=csv","r");
$sql = "TRUNCATE TABLE scores";
$conn->query($sql);
if ( $f_pointer === false )
die ("invalid URL");
$ar=fgetcsv($f_pointer);
while(! feof($f_pointer)){
$ar=fgetcsv($f_pointer)
;
$sql="INSERT INTO scores(place,person,score,grade,round,location)values('$ar[0]','$ar[1]','$ar[2]','$ar[3]','$ar[4]','$ar[5]')";
$conn->query($sql);
echo $sql;
echo "<br>";
}
?>
OUTPUT RESULTS
Fails to insert because of the special character '
in the location field.
DESIRED RESULTS
INSERT INTO scores(place,person,score,grade,round,location)values('-','Alex','2','5','','Greens Mill')
Note: Green's Mill
would needs to be Greens Mill
without the '
.