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 Link to comment https://forums.phpfreaks.com/topic/196385-inserting-data-in-db-help/ Share on other sites More sharing options...
schilly Posted March 24, 2010 Share Posted March 24, 2010 http://www.tizag.com/mysqlTutorial/mysqlinsert.php Link to comment https://forums.phpfreaks.com/topic/196385-inserting-data-in-db-help/#findComment-1031117 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); ?> Link to comment https://forums.phpfreaks.com/topic/196385-inserting-data-in-db-help/#findComment-1031142 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); Link to comment https://forums.phpfreaks.com/topic/196385-inserting-data-in-db-help/#findComment-1031146 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. Link to comment https://forums.phpfreaks.com/topic/196385-inserting-data-in-db-help/#findComment-1031151 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? Link to comment https://forums.phpfreaks.com/topic/196385-inserting-data-in-db-help/#findComment-1031153 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. Link to comment https://forums.phpfreaks.com/topic/196385-inserting-data-in-db-help/#findComment-1031156 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. Link to comment https://forums.phpfreaks.com/topic/196385-inserting-data-in-db-help/#findComment-1031167 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"> > > Link to comment https://forums.phpfreaks.com/topic/196385-inserting-data-in-db-help/#findComment-1031172 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">? Link to comment https://forums.phpfreaks.com/topic/196385-inserting-data-in-db-help/#findComment-1031184 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. Link to comment https://forums.phpfreaks.com/topic/196385-inserting-data-in-db-help/#findComment-1031187 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? Link to comment https://forums.phpfreaks.com/topic/196385-inserting-data-in-db-help/#findComment-1031188 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]. Link to comment https://forums.phpfreaks.com/topic/196385-inserting-data-in-db-help/#findComment-1031191 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 Link to comment https://forums.phpfreaks.com/topic/196385-inserting-data-in-db-help/#findComment-1031192 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!! Link to comment https://forums.phpfreaks.com/topic/196385-inserting-data-in-db-help/#findComment-1031196 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? Link to comment https://forums.phpfreaks.com/topic/196385-inserting-data-in-db-help/#findComment-1031199 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 Link to comment https://forums.phpfreaks.com/topic/196385-inserting-data-in-db-help/#findComment-1031202 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! Link to comment https://forums.phpfreaks.com/topic/196385-inserting-data-in-db-help/#findComment-1031209 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.