...

View Full Version : Resolved INSERTING JSON Data into a SQL DB



Courtney
02-26-2010, 11:09 PM
I am having issues putting my JSON data into my DB. If I leave it off the query the rest of the script is fine. I am thinking it is the ' (apostrophe) but am unsure.

Query:


$sql2 = "REPLACE INTO items (json,jsonEquip) VALUES('{$itemRSS['json']}','{$itemRSS['jsonEquip']}');
$result2 = mysql_query($sql2) or die(mysql_error());


Data in $itemRSS[json]:

'id:9998,name:'5Black Mageweave Vest',level:41,reqlevel:36,armor:54,slot:5,source:[1],sourcemore:[{t:6,ti:12048,n:'Black Mageweave Vest',s:197,c:11}],classs:4,subclass:1,displayid:24352,slotbak:5'

and $itemRSS[jsonEquip]:

'slotbak:5,displayid:24352,reqlevel:36,armor:54,int:11,spi:12,dura:70,sellprice:4815,avgbuyout:20000 '

Error:

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 '5Black Mageweave Vest',level:41,reqlevel:36,armor:54,slot:5,source:[1],sourcemor' at line 1

I tried:



$replace = "'";
$replacewith = "''";
$sql2 = string_replace($replace, $replacewith, $sql2);

skywalker2208
02-26-2010, 11:13 PM
Try mysql_real_escape_string

Courtney
02-26-2010, 11:32 PM
That worked! My original concern was that the apostrophe was needed for the json but it appears the delimiter is a comma so it must have been placed there by the person to encode it.

Thank you.

bdl
02-27-2010, 05:43 PM
A bit late, but why are you using a relational database to store a JSON object as a string?

Courtney
02-27-2010, 06:12 PM
This is mainly a self learning type of thing. I pln on using the JSON in the future however when I started this I did know it was available and never included it into my project design.

I want the info available though so I can in the futue use it.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum