Jump to content

inserting data in db help


cosmicsea

Recommended Posts

hi im using this code to crawl and grab images from a single page and i want to setup a db for it.

<?php

$url  = "http://domain.com";
$data = file_get_contents($url); 

preg_match_all("/<img[^>]*>/", $data, $match);

$list = $match[0];

print_r($list);

$username="username";
$password="password";
$database="database";



mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

mysql_close();

?>

 

i can seem to connect to the db ok using the above code but how do i insert the data to the db? i want the data to goto the table links and column url. can somebody help me?

Thanks

 

Link to comment
Share on other sites

Ok here is my code now after going over that link. It almost works, when i run it, it will put the word Array in the db. im not sure what do do now can anyone help? i need multiple entries to go in there. Thanks.

<?php
mysql_connect("localhost", "user", "pass") or die(mysql_error());
mysql_select_db("data") or die(mysql_error());



$url  = "http://doamin.com";
$data = file_get_contents($url); 

preg_match_all("/<img[^>]*>/", $data, $match);

$list = $match[0];

mysql_query("INSERT INTO links 
(url) VALUES('$match') ") 
or die(mysql_error()); 


print_r($list);


?>

Link to comment
Share on other sites

yes that's correct. $match is an array.

 

i think you want this:

mysql_connect("localhost", "user", "pass") or die(mysql_error());
mysql_select_db("data") or die(mysql_error());



$url  = "http://doamin.com";
$data = file_get_contents($url); 

preg_match_all("/<img[^>]*>/", $data, $match);

$list = $match[0];

foreach($match as $url){

mysql_query("INSERT INTO links 
(url) VALUES('$url') ") 
or die(mysql_error()); 


}


print_r($list);

 

Link to comment
Share on other sites

Referring

 

$list = $match[0];

 

mysql_query("INSERT INTO links

(url) VALUES('$match') ")

or die(mysql_error());

 

modify it as

 

mysql_query("INSERT INTO links

(url) VALUES('$list') ")

or die(mysql_error());

 

as $list  contains the value.

Link to comment
Share on other sites

yes that's correct. $match is an array.

 

i think you want this:

mysql_connect("localhost", "user", "pass") or die(mysql_error());
mysql_select_db("data") or die(mysql_error());



$url  = "http://doamin.com";
$data = file_get_contents($url); 

preg_match_all("/<img[^>]*>/", $data, $match);

$list = $match[0];

foreach($match as $url){

mysql_query("INSERT INTO links 
(url) VALUES('$url') ") 
or die(mysql_error()); 


}


print_r($list);

 

that still displays it in the db as Array. do you have another suggestion?

Link to comment
Share on other sites

Referring

 

$list = $match[0];

 

mysql_query("INSERT INTO links

(url) VALUES('$match') ")

or die(mysql_error());

 

modify it as

 

mysql_query("INSERT INTO links

(url) VALUES('$list') ")

or die(mysql_error());

 

as $list  contains the value.

this inserts into the db as Array also.

Link to comment
Share on other sites

do

echo '<pre>.';

print_r($match);

 

and print the output here.

here is the output.

<pre>.Array
<
        [0] => Array
               <
                          [0] => <img src="picture.jpg">
                          [1] => <img src="picture2.jpg">
               >
>

Link to comment
Share on other sites

basically what i had before except it's not a multi dimensional array:

foreach($match[0] as $url){

mysql_query("INSERT INTO links 
(url) VALUES('$url') ") 
or die(mysql_error()); 


}


assuming all your urls end up in the $match[0] array else you will need nested foreach loops

Link to comment
Share on other sites

basically what i had before except it's not a multi dimensional array:

foreach($match[0] as $url){

mysql_query("INSERT INTO links 
(url) VALUES('$url') ") 
or die(mysql_error()); 


}


assuming all your urls end up in the $match[0] array else you will need nested foreach loops

well it seems to be working now. i changed what you said to foreach($match[0] as $list) and it seems to be working correctly, thanks for all your help everyone!

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.