Jump to content

[SOLVED] how do you store array in db?


bobocheez

Recommended Posts

So I have a random number script, but I'm not sure how to store the array values in once cell. Then query the db again and set individual variables for each number.

I would just make 5 cells for each number, but more numbers will be added so I don't want the table to get too big.

 

$max = 5;
$min = 1;
$amount = 5;
$x = 0;
$numbers[0] = 0;

if (($max - $min) > $amount) die('$amount is larger number than $max - $min');

while (count($numbers) < $amount) {
        $ranvar = rand($min, $max);
        while(in_array($ranvar, $numbers)) {
                $ranvar = rand($min, $max);
        }
        $numbers[$x] = $ranvar;
        $x++;
}
echo "$numbers[0]<br/>";
echo "$numbers[1]<br/>";
echo "$numbers[2]<br/>";
echo "$numbers[3]<br/>";
echo "$numbers[4]<br/>";

$query_rand = "UPDATE hits WHERE id=2 SET order='something', ";
mysql_query($query_rand);

Link to comment
https://forums.phpfreaks.com/topic/180584-solved-how-do-you-store-array-in-db/
Share on other sites

You should probably make a new table and relate the numbers to your other table. Otherwise, you can implode() the array before you put it in your database and explode it after you grab it back.

 

http://us.php.net/manual/en/function.implode.php

http://us.php.net/manual/en/function.explode.php

Method 1: Use serialize on the array you want to store in the database and then use unserialize when pulling that same value from the database

 

-- or, as lemmin suggests --

 

Method 2: implode the array before inserting it into the database. And then explode that string when pulling it from the database.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.