Inserting an arrays content into MYSQL Table


I’ve reached the end of my tether in trying to make this work, so this is the final place I am going in order to get it working.

I have created a number generator in which the user specifies the maximum number and generate rate (Generate rate is the amount of numbers the user wishes to generate). All this information is collected from a form.

The data collected from this form then needs be sent to a MySQL table. The Table automatically generates an individual ID for each record. (Fields: ID, Minimum_Number, Maximum_Number, Genertate_Rate and Numbers).

Minimum number is always 0, so the user is not given the option to change this - its there just in case it is required in the future.

The Genertated numbers are created using a loop, and is then stored in an array.

for ($i = $MINIMUM_NUMBER; $i <= $GENERATE_RATE; $i++) {

All the other fields are successfully stored in the database, but Numbers will not.

echo "<br />Contents of array:<br />"; //Testing reasons - ensure this is removed later on!

            $ID = $_POST['ID'];
            $NUMBERS = $_POST['NUMBERS'];
            $db1 = new Number_Information();
            $numofrows = $db1->insert_GeneratedNumber('', $MINIMUM_NUMBER, $MAXIMUM_NUMBER, $GENERATE_RATE, $NUMBERS);
            echo "Success. Number of rows affected: <strong>$numofrows<strong>";


Please excuse me for not following the correct naming conventions, and anyone that helps me will be bathed in eternal glory!

Its funny I have just been looking at a similar problem.
As you are dealing with an array one method I found was serialize() and unserialize(). But as you are only dealing with numbers a better recomendation was to implode() the array and explode() it later.


$NUMBERS = implode( ',', $NUMBERS);

Excellent stuff. Eternal glory coming your way.

As I was waiting for a reply I did come across this, but couldn’t figure out how to implement it.

Its still not inserting, but i’m sure it is easy to figure out - I think thats to do with the data type in the mySQL table.

Edit: I forgot to say thanks!

Serialize() is a great way to store arrays directly to your database and then retrieve them later as is with Unserialize(). I use it quite a lot, but as Rubble said, with the current example containing only INTs and it not being an associative array, you should just implode it and save the work later.