SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Feb 2012
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Inserting an arrays content into MYSQL Table

    Hello,

    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.

    Code:
    for ($i = $MINIMUM_NUMBER; $i <= $GENERATE_RATE; $i++) {
        $NUMBERS[$i] = rand($MINIMUM_NUMBER, $MAXIMUM_NUMBER);
    All the other fields are successfully stored in the database, but Numbers will not.

    Code:
    echo "<br />Contents of array:<br />"; //Testing reasons - ensure this is removed later on!
    print_r($NUMBERS); 
    
                $ID = $_POST['ID'];
                $MINIMUM_NUMBER = $_POST['MINIMUM_NUMBER'];
                $MAXIMUM_NUMBER = $_POST['MAXIMUM_NUMBER'];
                $GENERATE_RATE = $_POST['GENERATE_RATE'];
                $NUMBERS = $_POST['NUMBERS'];
                $db1 = new Number_Information();
                $db1->openDB();
                $numofrows = $db1->insert_GeneratedNumber('', $MINIMUM_NUMBER, $MAXIMUM_NUMBER, $GENERATE_RATE, $NUMBERS);
                echo "Success. Number of rows affected: <strong>$numofrows<strong>";
                $db1->closeDB();
            
            ?>

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

  2. #2
    SitePoint Mentor silver trophy
    Rubble's Avatar
    Join Date
    Dec 2005
    Location
    Cambridge, England
    Posts
    2,411
    Mentioned
    81 Post(s)
    Tagged
    3 Thread(s)
    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.

    Try:
    PHP Code:
    $NUMBERS implode','$NUMBERS); 

  3. #3
    SitePoint Member
    Join Date
    Feb 2012
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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!

  4. #4
    SitePoint Evangelist smftre's Avatar
    Join Date
    Dec 2008
    Location
    London
    Posts
    436
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.
    Statvoo.com The Website Traffic Monitor
    The best way to monitor traffic to your sites for free!


    Web Development London UK We make web 3.0 applications


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •