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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.