Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Feb 2010
    Posts
    20
    Thanks
    2
    Thanked 2 Times in 2 Posts

    INSERTING JSON Data into a SQL DB

    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:
    Code:
    $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:

    PHP Code:
    $replace "'";
    $replacewith "''";
    $sql2 string_replace($replace$replacewith$sql2); 
    Last edited by Courtney; 02-27-2010 at 12:39 AM.

  • #2
    Regular Coder
    Join Date
    Jan 2009
    Posts
    193
    Thanks
    0
    Thanked 20 Times in 20 Posts
    Try mysql_real_escape_string

  • Users who have thanked skywalker2208 for this post:

    Courtney (02-26-2010)

  • #3
    New Coder
    Join Date
    Feb 2010
    Posts
    20
    Thanks
    2
    Thanked 2 Times in 2 Posts
    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.

  • #4
    bdl
    bdl is offline
    Regular Coder
    Join Date
    Apr 2007
    Location
    Camarillo, CA US
    Posts
    590
    Thanks
    4
    Thanked 83 Times in 82 Posts
    A bit late, but why are you using a relational database to store a JSON object as a string?

  • #5
    New Coder
    Join Date
    Feb 2010
    Posts
    20
    Thanks
    2
    Thanked 2 Times in 2 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •