I saved a multidimensional array(variable"original") from PHP to one cellfield in mysql.
Now I'm looking for the easiest way to transfer that array right back from mysql to a new variable("duplicate"), so that this new variable contains the same multidimensional array as "original", meaning i want a duplicate of "original" through reading out mysql ...

here's the way that doesnt do the work:
$query = "SELECT cellname
FROM tablename
WHERE id = 1";
$duplicate_unfetched = mysql_query($query);
$duplicate = mysql_fetch_array($duplicate_unfetched, MYSQL_ASSOC);

i guess i messed up in the last 2 lines ... $duplicate doesnt contain the whole multi-array but just 1 result field ... :o

    You select cellname only, so fetch_array will have one element, namely the data from cellname. You access that data with $duplicate[0] (or possibly $duplicate['cellname']).

      after I did this I checked the content of $duplicate and it was the string: "Array" 🙂 well, I didnt know that I cant just save an array to one cell in a mysql table. because after that the only value in that cell would be the string "Array" ... Yet I found there is a way to save an array to db using serialize() which converts the array to a string and on the way back from mysql using unserialize() doing the opposite job. That solved my problem. Now I have a perfect dupicate 😉

        serialize/unserialize do the trick:

        $dbHost = '';
        $dbUser = '';
        $dbPwd = '';
        $dbDb = '';
        
        
        $bla = array('a'=>'x','y','z');
        
        $dbConn = new mysqli($dbHost, $dbUser, $dbPwd, $dbDb);
        $sql = sprintf('INSERT INTO myTbl(myField) VALUES(?)');
        $stmt = $dbConn->prepare($sql);
        $bla = serialize($bla);
        $stmt->bind_param('s',$bla);
        $stmt->execute();
        
        $sql = sprintf('SELECT myField FROM myTbl');
        $rst = $dbConn->query($sql) or trigger_error($dbConn->error,E_USER_ERROR);
        $row = $rst->fetch_assoc();
        $blaNeu = $row['myField'];
        
        var_dump($blaNeu);
        $blaNeu = unserialize($blaNeu);
        var_dump($blaNeu);
        exit;

          bah! crossposting - you found out yourself, good good 🙂

            Write a Reply...