I write code to save foreach data to the database.
foreach data is arrays .
For this reason firs convert array to string and then save to the database but display following error on firefox.
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'change,lowest,topest) VALUES ('[{"Name":"س\u06a9ه بها\u' at line 1 my code
$con = @mysql_connect ("localhost","root", "");
mysql_select_db("coin", $con);
if (!$con)
{
die(mysql_error());
}else {
foreach($table_rows as $tr) { // foreach row
$row = $tr->childNodes;
if($row->item(0)->tagName != 'tblhead') { // avoid headers
$data[] = array(
'Name' =>trim($row->item(0)->nodeValue),
'LivePrice' => trim($row->item(2)->nodeValue),
'Change'=> trim($row->item(4)->nodeValue),
'Lowest'=> trim($row->item(6)->nodeValue),
'Topest'=> trim($row->item(8)->nodeValue),
///'Time'=> trim($row->item(10)->nodeValue),
);
}
}
$newstring = json_encode($data);
$date=array();
mysql_select_db ( "coin", $con );
"CREATE TABLE `Dadoo`(id INT NOT NULL AUTO INCREMENT,name VARCHAR(255),liveprice VARCHAR(255),change VARCHAR(255),lowest VARCHAR(255),topest VARCHAR(255),PRIMARY KEY(id)) ENGINE=MyISAM" or die(mysql_error());
$debugquery = mysql_query("INSERT INTO `Dadoo`(name,liveprice,change,lowest,topest) VALUES ('$newstring')");
if (!$debugquery)
{
die(mysql_error());
}
}
mysql_close();
How fix it?
change
is a reserved word and needs to be escaped with backticks.
INSERT INTO `Dadoo`(name,liveprice,`change`,lowest,topest) ...