rstandley Posted August 17, 2010 Share Posted August 17, 2010 Hi Guys, I have been racking my brain all of today trying to get this to work, im pretty new to it all! What im trying to do is use the following code whcih works fine and returns all the correct information in an Array, and insert this into a MySQL DB. Can anyone point me in the right direction? foreach($html->find('div.rsshelper') as $article) { $item['title'] = $article->find('div.prodlistboxbg', 0)->plaintext; $item['link'] = $article->find('.prodimgurl', 0)->href; $item['image_url'] = $article->find('.image-thumb', 0)->src; $item['old_price'] = $article->find('span.text-pricestrike', 0)->plaintext; $item['current_price'] = $article->find('span.text-pricespecial', 0)->plaintext; $item['saving'] = $article->find('span.price-percentage', 0)->plaintext; $articles[] = $item; } print_r($articles); This is the code that generates the Array with the given data. I know how to connect to the DB but how do I get each row into each row of the DB? Thanks Rory Link to comment https://forums.phpfreaks.com/topic/210965-php-array-into-mysql/ Share on other sites More sharing options...
AbraCadaver Posted August 17, 2010 Share Posted August 17, 2010 You need to check out some of the tutorials and look up the INSERT syntax. Assuming that you have connected and selected a database, and assuming that your array keys are the same as the database column names, then here is an example: $columns = implode(",", array_keys($articles[0])); foreach($articles as $row) { $data = "'" . implode("','", array_map('mysql_real_escape_string', $row)) . "'"; mysql_query("INSERT INTO table_name ($columns) VALUES ($data)"; } Link to comment https://forums.phpfreaks.com/topic/210965-php-array-into-mysql/#findComment-1100399 Share on other sites More sharing options...
AbraCadaver Posted August 17, 2010 Share Posted August 17, 2010 Sorry... Parse error... $columns = implode(",", array_keys($articles[0])); foreach($articles as $row) { $data = "'" . implode("','", array_map('mysql_real_escape_string', $row)) . "'"; mysql_query("INSERT INTO table_name ($columns) VALUES ($data)"); } Link to comment https://forums.phpfreaks.com/topic/210965-php-array-into-mysql/#findComment-1100433 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.