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

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).")");
}

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.