1

I have a long string.

<?xml version="1.0" encoding="utf-8"?>
<GamePlaySession xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <selectedFederation>-1</selectedFederation>
   <selectedComissioner>-1</selectedComissioner>
   <myCash>0</myCash>
   <myTokens>0</myTokens>
   <myLevel>0</myLevel>
   <shownBlurbCount>0</shownBlurbCount>
   <isNewPlayer>true</isNewPlayer>
   <inTutorialMode>true</inTutorialMode>
   <flavorTutorialClear>false</flavorTutorialClear>
   <calendarTutorialClear>false</calendarTutorialClear>
   <resultTutorialClear>false</resultTutorialClear>
   <storePurchaseTutorialClear>false</storePurchaseTutorialClear>
   <storeRefreshTutorialClear>false</storeRefreshTutorialClear>
   <storeTutorialClear>false</storeTutorialClear>
   <transitionMessageClear>false</transitionMessageClear>
   <storeCardsBought>0</storeCardsBought>
   <storePurchases>
       <boolean>false</boolean>
       <boolean>false</boolean>
       <boolean>false</boolean>
   </storePurchases>
   <newGamePlus>false</newGamePlus>
   <currentYear>0</currentYear>
   <currentMonth>0</currentMonth>
   <numberOfYears>10</numberOfYears>
   <worldChampCardNumber>0</worldChampCardNumber>
   <titleChampCardNumber>0</titleChampCardNumber>
   <tagChampCardNumbers>
       <int>0</int>
       <int>0</int>
   </tagChampCardNumbers>
   <worldChampLastCardNumber>0</worldChampLastCardNumber>
   <titleChampLastCardNumber>0</titleChampLastCardNumber>
   <tagChampLastCardNumbers>
       <int>-1</int>
       <int>-1</int>
   </tagChampLastCardNumbers>
   <firstShopCards>false</firstShopCards>

I want to store it inside a column. I am using urlencode as well as mysql_real_escape_string() but it doesn't work. Anyone knows how can I store it?

Dharman
  • 21,838
  • 18
  • 57
  • 107
Ali Zia
  • 3,539
  • 3
  • 23
  • 63
  • Try BLOB as type of your database-column. Use the CDATA-tags to keep your xml-string clean. Not urlencode or mysql_real_escape_string(). – Oliver Jan 27 '17 at 15:03
  • @Oliver can you please show an example? my column type is LONG TEXT. Is that okay? – Ali Zia Jan 27 '17 at 15:04
  • Related http://stackoverflow.com/questions/2204261/how-do-i-store-xml-data-into-a-mysql-database-i-dont-want-foreign-keys-like-cr – Eric Jan 27 '17 at 15:06
  • @AliZia Have a look here: http://stackoverflow.com/questions/6749606/mysql-which-is-more-efficient-longtext-text-or-blob-improving-insert-efficie ... I would use BLOB, because you want to save something which should not be changed by character set. What example do you want to have? A code example? – Oliver Jan 27 '17 at 15:07
  • An explanation for CDATA in XML is here: http://stackoverflow.com/questions/2784183/what-does-cdata-in-xml-mean – Oliver Jan 27 '17 at 15:14
  • [Please stop using mysql_ functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) as they were removed in PHP 7.0. Also, [escaping is not sufficient protection against SQL injection](http://stackoverflow.com/documentation/php/2784/php-mysqli/12843/escaping-strings) – Machavity Jan 27 '17 at 15:17
  • @Oliver yes something like $a = 'XML string'. How would I store it? – Ali Zia Jan 27 '17 at 15:21
  • @AliZia I would use the php function http://php.net/manual/de/function.simplexml-load-string.php. This checks also if xml is valid. Then "insert into table (fieldname) values ('".$xml->asXml()."')". And that is all. But keep in mind, that you have to check your database-settings like max_allowed_packet. Also php settings could cut this string and causes an error. If you run into performance-problems try to split this string and save it to database with concat() in loop – Oliver Jan 27 '17 at 16:25

0 Answers0