I've been happily parsing JSON files to a MYSQL DB but now have one with - and + characters, from what I have read these are not allowed to be used in $variables in PHP. so when I run the PHP script in get this error.
Parse error: syntax error, unexpected '=' in C:\xampp\htdocs\TestSite\frl2_odds_test.php on line 26
Once I remove the 3 variables from the PHP script with the -, + from the script it works and parses the JSON to the DB.
I have read this http://php.net/manual/en/language.variables.variable.php and this https://www.experts-exchange.com/questions/28628085/json-encode-fails-with-special-characters.html but haven't been able to get a solution. any help appreciated... thanks.. code as follows...
PHP
<?php
$host = "localhost";
$username = "";
$password = "";
$dbname = "football";
$con = mysqli_connect($host, $username, $password, $dbname) or die('Error in Connecting: ' . mysqli_error($con));
$st = mysqli_prepare($con, 'INSERT INTO frl2_odd(match_id, odd_bookmakers, odd_date, odd_1, odd_x, odd_2, odd_1x, o+0.5, u+0.5, o+1.5, bts_yes, bts_no) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)');
mysqli_stmt_bind_param($st, 'ssssssssssss', $match_id, $odd_bookmakers, $odd_date, $odd_1, $odd_x, $odd_2, $odd_1x, $o+0.5, $u+0.5, $o+1.5, $bts_yes, $bts_no);
$filename = 'json/testodds.json';
$json = file_get_contents($filename);
$data = json_decode($json, true);
foreach($data as $row) {
$match_id = $row['match_id'];
$odd_bookmakers = $row['odd_bookmakers'];
$odd_date = $row['odd_date'];
$odd_1 = $row['odd_1'];
$odd_x = $row['odd_x'];
$odd_2 = $row['odd_2'];
$odd_1x = $row['odd_1x'];
$o+0.5 = $row['o+0.5'];
$u+0.5 = $row['u+0.5'];
$o+1.5 = $row['o+1.5'];
$bts_yes = $row['bts_yes'];
$bts_no = $row['bts_no'];
mysqli_stmt_execute($st);
}
mysqli_close($con);
?>
JSON
[
{
"match_id": "345064",
"odd_bookmakers": "Interwetten.es",
"odd_date": "2018-10-24 04:39:45",
"odd_1": "2.05",
"odd_x": "2.95",
"odd_2": "3.95",
"odd_1x": "",
"odd_12": "",
"odd_x2": "",
"ah-4.5_1": "",
"ah-4.5_2": "",
"ah-4_1": "",
"ah-4_2": "",
"ah-3.5_1": "",
"ah-3.5_2": "",
"ah-3_1": "",
"ah-3_2": "",
"ah-2.5_1": "",
"ah-2.5_2": "",
"ah-2_1": "",
"ah-2_2": "",
"ah-1.5_1": "",
"ah-1.5_2": "",
"ah-1_1": "3.00",
"ah-1_2": "1.33",
"ah0_1": "1.42",
"ah0_2": "2.65",
"ah+0.5_1": "",
"ah+1_1": "",
"ah+1_2": "",
"ah+1.5_1": "",
"ah+1.5_2": "",
"ah+2_1": "",
"ah+2_2": "",
"ah+2.5_1": "",
"ah+2.5_2": "",
"ah+3_1": "",
"ah+3_2": "",
"ah+3.5_1": "",
"ah+3.5_2": "",
"ah+4_1": "",
"ah+4_2": "",
"ah+4.5_1": "",
"ah+4.5_2": "",
"o+0.5": "",
"u+0.5": "",
"o+1": "",
"u+1": "",
"o+1.5": "",
"u+1.5": "",
"o+2": "",
"u+2": "",
"o+2.5": "2.45",
"u+2.5": "1.48",
"o+3": "",
"u+3": "",
"o+3.5": "",
"u+3.5": "",
"o+4": "",
"u+4": "",
"o+4.5": "",
"u+4.5": "",
"o+5": "",
"u+5": "",
"o+5.5": "",
"u+5.5": "",
"bts_yes": "2.05",
"bts_no": "1.67"
}
]