Jump to content

PHP array into MYSQL


rstandley

Recommended Posts

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

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

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

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.