cmor Posted July 27, 2007 Share Posted July 27, 2007 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 https://forums.phpfreaks.com/topic/61960-insert-data-into-database-possible-to-use-array/ Share on other sites More sharing options...
Illusion Posted July 27, 2007 Share Posted July 27, 2007 If u r matrix elements are not interpreted then try this ................VALUES ('{$snowdata['$i'][0]}','{$snowdata['$i'][1]}',............................ Link to comment https://forums.phpfreaks.com/topic/61960-insert-data-into-database-possible-to-use-array/#findComment-308672 Share on other sites More sharing options...
bubblegum.anarchy Posted July 27, 2007 Share Posted July 27, 2007 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 https://forums.phpfreaks.com/topic/61960-insert-data-into-database-possible-to-use-array/#findComment-308695 Share on other sites More sharing options...
Barand Posted July 27, 2007 Share Posted July 27, 2007 or <?php $query = "INSERT INTO weather VALUES ('" . join ("','", $snowdata[$i]) . "')"; Link to comment https://forums.phpfreaks.com/topic/61960-insert-data-into-database-possible-to-use-array/#findComment-309037 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.