Jump to content

Insert data into database, possible to use array?


cmor

Recommended Posts

I have a basic inserting data question.

 

Here's my code:

for($i = 0; $i < $j; $i++){
$query = "INSERT INTO weather VALUES ('$snowdata[$i][0]','$snowdata[$i][1]','$snowdata[$i][2]','$snowdata[$i][3]','$snowdata[$i][4]',
'$snowdata[$i][5]','$snowdata[$i][6]','$snowdata[$i][7]','$snowdata[$i][8]','$snowdata[$i][9]','$snowdata[$i][10]','$snowdata[$i][11]','$snowdata[$i][12]',
'$snowdata[$i][13]','$snowdata[$i][14]')";
$db->query($query);
}

 

First off, it doesn't work because I assume the snowdata matrices aren't being evaluated?  Or is there something else obvious I'm missing?  Second off, is it some how possible to insert an array (or row of a matrix) directly into a row of an sql database?  Thanks for any pointers or leads.

 

-Chris

Link to comment
Share on other sites

What is the value of $j ? - maybe use sizeof($snowdata) instead, depending on application.

 

Consider something like this, for the fun factor:

 

for ($i = 0; $i < sizeof($snowdata); $i++)
{
     $query = "INSERT INTO weather VALUES (";

     for ($n = 1; $n <= 14; $n++) $query .= "'{$snowdata[$i][$n]}',";

     $db->query($query = substr($query, 0, strlen($query) - 1).")");
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.