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
https://forums.phpfreaks.com/topic/196385-inserting-data-in-db-help/
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);


?>

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

 

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?

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.

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">
               >
>

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

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!

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.