cosmicsea Posted March 24, 2010 Share Posted March 24, 2010 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 Quote Link to comment Share on other sites More sharing options...
schilly Posted March 24, 2010 Share Posted March 24, 2010 http://www.tizag.com/mysqlTutorial/mysqlinsert.php Quote Link to comment Share on other sites More sharing options...
cosmicsea Posted March 24, 2010 Author Share Posted March 24, 2010 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); ?> Quote Link to comment Share on other sites More sharing options...
schilly Posted March 24, 2010 Share Posted March 24, 2010 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); Quote Link to comment Share on other sites More sharing options...
priti Posted March 24, 2010 Share Posted March 24, 2010 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. Quote Link to comment Share on other sites More sharing options...
cosmicsea Posted March 24, 2010 Author Share Posted March 24, 2010 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? Quote Link to comment Share on other sites More sharing options...
cosmicsea Posted March 24, 2010 Author Share Posted March 24, 2010 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. Quote Link to comment Share on other sites More sharing options...
priti Posted March 24, 2010 Share Posted March 24, 2010 do echo '<pre>.'; print_r($match); and print the output here. Quote Link to comment Share on other sites More sharing options...
cosmicsea Posted March 24, 2010 Author Share Posted March 24, 2010 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"> > > Quote Link to comment Share on other sites More sharing options...
schilly Posted March 24, 2010 Share Posted March 24, 2010 i think your preg pattern is wrong. you just want the url right? not <img src="picture.jpg">? Quote Link to comment Share on other sites More sharing options...
cosmicsea Posted March 24, 2010 Author Share Posted March 24, 2010 i think your preg pattern is wrong. you just want the url right? not <img src="picture.jpg">? yes i do but this is just for testing, i will change the regex to whatever i need after i can get a result from this. Quote Link to comment Share on other sites More sharing options...
cosmicsea Posted March 24, 2010 Author Share Posted March 24, 2010 ok i just found something weird to me. if i change preg_match_all to just preg_match it will index just fine but only the first link. can anyone help to fix this? Quote Link to comment Share on other sites More sharing options...
priti Posted March 24, 2010 Share Posted March 24, 2010 so you can access the values from match array as $match[0][0]. Quote Link to comment Share on other sites More sharing options...
cosmicsea Posted March 24, 2010 Author Share Posted March 24, 2010 so you can access the values from match array as $match[0][0]. yes but i can only get one value Quote Link to comment Share on other sites More sharing options...
priti Posted March 24, 2010 Share Posted March 24, 2010 run the loop over array and you will get all values!! Quote Link to comment Share on other sites More sharing options...
cosmicsea Posted March 24, 2010 Author Share Posted March 24, 2010 run the loop over array and you will get all values!! how do i do that? Quote Link to comment Share on other sites More sharing options...
schilly Posted March 24, 2010 Share Posted March 24, 2010 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 Quote Link to comment Share on other sites More sharing options...
cosmicsea Posted March 24, 2010 Author Share Posted March 24, 2010 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! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.