Re: problems with array again
| From: | Zak Greant | Date: | Fri, 01 Dec 2000 16:37:30 +0000 |
| Subject: | Re: problems with array again | ||
| References: | 1 | Groups: | php.windows |
| Request: | Send a blank email to php-windows+get-4227@lists.php.net to get a copy of this message | ||
At 11:26 AM 12/1/00 -0800, afan wrote:
Hi guys, again problems with arrays: $content = array("afan", "pasalix", "astoria", "new york"); $result = mysql_query("UPDATE test SET content='$content' WHERE id=1 ",$db); $result = mysql_query("SELECT * FROM test WHERE id=1", $db); $myrow = mysql_fetch_array($result); $NewContent = $myrow["content"]; $counter = count($NewContent); echo"\$counter: $counter<br>"; for($x=0; $x<$counter; $x++) { echo"Content[$x]: $contentdb[$x]<br>"; } On screen is: $counter: 1 $contentdb[0]: A $contentdb[1]: r $contentdb[2]: r $contentdb[3]: a If, instead of: $NewContent = $myrow["content"]; I put $NewContent[] = $myrow["content"]; results look like: $counter: 1 $contentdb[0]: Array $contentdb[1]: $contentdb[2]: $contentdb[3]: Why I can't put an array in database?Databases have no idea how to handle a PHP-style array - to store an array in a database, you need to serialize the array. Serialize is a function that converts a php value into a special string based format that can later be turned back into the original value using unserialize. $content = array("afan", "pasalix", "astoria", "new york"); /* Use addslashes to stop any quotes in the array contents from breaking the quoting in the query */ $serialized_content = addslashes (serialize ($content)); $result = mysql_query("UPDATE test SET content='$serialized_content' WHERE id=1 ",$db); $result = mysql_query("SELECT * FROM test WHERE id=1", $db); $myrow = mysql_fetch_array($result); // convert the data back into an array $NewContent = unserialize (stripslashes ($myrow["content"])); $counter = count($NewContent); echo"\$counter: $counter<br>"; for($x=0; $x<$counter; $x++) { echo"Content[$x]: $contentdb[$x]<br>"; } Good Luck! --zak