ccnaboy_2000 Posted March 27, 2007 Share Posted March 27, 2007 I have an array of data. I want to write each key in the array to its own row in SQL. what would be the best way to accomplish this? Link to comment https://forums.phpfreaks.com/topic/44431-write-array-keys-to-sql/ Share on other sites More sharing options...
MadTechie Posted March 27, 2007 Share Posted March 27, 2007 you a foreach loop to build the SQL statment thats only my suggection Link to comment https://forums.phpfreaks.com/topic/44431-write-array-keys-to-sql/#findComment-215777 Share on other sites More sharing options...
genericnumber1 Posted March 27, 2007 Share Posted March 27, 2007 To elaborate on what MadTechie said... You could just use <?php foreach($array as $key => $value) { // $key = the key // $value = the value // Do insert queries } ?> just use $key and $value for the insert queries More info on how to actually insert values into sql... http://www.google.com/search?hl=en&safe=off&client=firefox-a&rls=org.mozilla%3Aen-US%3Aofficial&hs=Ebe&q=php+sql+insert&btnG=Search Link to comment https://forums.phpfreaks.com/topic/44431-write-array-keys-to-sql/#findComment-215778 Share on other sites More sharing options...
ccnaboy_2000 Posted March 27, 2007 Author Share Posted March 27, 2007 thank you i'll try it out. Link to comment https://forums.phpfreaks.com/topic/44431-write-array-keys-to-sql/#findComment-215782 Share on other sites More sharing options...
ccnaboy_2000 Posted March 27, 2007 Author Share Posted March 27, 2007 well I can get it to print the array data properly to the screen. But i think my SQL Query may be malformed. It keeps die'ing on the query. $query = "INSERT INTO repository ('url') VALUES ('$link')"; mysql_query($query) or die("cannot execute query"); Is my syntax wrong? Link to comment https://forums.phpfreaks.com/topic/44431-write-array-keys-to-sql/#findComment-215800 Share on other sites More sharing options...
desithugg Posted March 27, 2007 Share Posted March 27, 2007 //database connection here $url['0'] = "whatever1"; $url['1'] = "whatever2"; $url['2'] = "whatever3"; //same thing as $url = array('whatever1','whatever2','whatever3'); echo "The fallowing links were enterd into the database:<br>"; foreach($url as $link) { $query = "INSERT INTO repository ('url') VALUES ('$link')"; mysql_query($query) or die("cannot execute query"); echo $link."<br>"; } Link to comment https://forums.phpfreaks.com/topic/44431-write-array-keys-to-sql/#findComment-215834 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.