Thread: MySQL + Arrays?
View Single Post
  #2  
Old 09-29-2011, 03:33 PM
Hall of Famer's Avatar
Hall of Famer Hall of Famer is offline
Administrator, Lead Coder
 
Join Date: Dec 2008
Location: South Brunswick
Posts: 4,448
Gender: Male
Credits: 397,837
Hall of Famer is on a distinguished road
Default

Well you cannot send an array or object directly into mysql database, but there is a way to do the trick. Lets assume you have an array as below, the script will be able to perform magic for you:

PHP Code:
$idarray = array(1,2,3,4,5);
$idstring implode(","$idarray);
mysql_query("UPDATE table {$prefix}.adoptables SET idstring = '{$idstring}' WHERE id = '$id'"); 
This way your id array will be stored as a string in the table prefix.adoptables, column idstring, the implode function converts an array into a string.

To retrieve this data from your mysql table, use the explode function:
PHP Code:
$result mysql_query("SELECT * from table {$prefix}.adoptables WHERE id = '$id'");
$row mysql_fetch_array($result);
$idarray explode(","$row['idstring']); 
Its also possible to generate a random number from the id array, I will explain to you later after I return from dinner.
__________________


Mysidia Adoptables, a free and ever-improving script for aspiring adoptables/pets site.
Reply With Quote