acee12345 Posted September 23, 2010 Share Posted September 23, 2010 Purpose: The following is some very rough code in the attempt to grab images off google search and shove them in a mysql database. This is Just a test run for a learning algorithm. Final will not keep images, just scan them State of code: So far code works to the point that it will grab images and put the URL in the database then display the images(not extracted from database). Need: I would like to put the images themselves in the database but cannot find a successful way to do so. Any help is greatly appreciated. <?php // get list of images from google images // Create MySQL login values and // set them to your login information. $username = "*******"; $password = "**********"; $host = "*******"; $database = "binary"; // Make the connect to MySQL or die // and display an error. $link = mysql_connect($host, $username, $password); if (!$link) { die('Could not connect: ' . mysql_error()); } // Select your database mysql_select_db ($database); $k='*****some query****'; $url = "http://www.google.com/images?hl=en&source=imghp&biw=1920&bih=859&q=##query##&gbv=2&aq=f&aqi=g10&aql=&oq=&gs_rfai="; $web_page = file_get_contents( str_replace("##query##",urlencode($k), $url )); $tieni = stristr($web_page,"dyn.setResults("); $tieni = str_replace( "dyn.setResults(","", str_replace(stristr($tieni,");"),"",$tieni) ); $tieni = str_replace("[]","",$tieni); $m = preg_split("/[\[\]]/",$tieni); $x = array(); for($i=0;$i<count($m);$i++) { $m[$i] = str_replace("/imgres?imgurl\\x3d","",$m[$i]); $m[$i] = str_replace(stristr($m[$i],"\\x26imgrefurl"),"",$m[$i]); $m[$i] = preg_replace("/^\"/i","",$m[$i]); $m[$i] = preg_replace("/^,/i","",$m[$i]); if ($m[$i]!="") { $tmpName = $m[$i]; // Create the query and insert // into our database. $query = "INSERT INTO tbl_images "; $query .= "(image) VALUES ('$tmpName')"; $results = mysql_query($query, $link); // echo "<img src='".$m[$i]."' />"; array_push($x,$m[$i]); } } $ar = $x; ?> Quote Link to comment https://forums.phpfreaks.com/topic/214152-web-image-to-mysql-via-php/ Share on other sites More sharing options...
acee12345 Posted September 23, 2010 Author Share Posted September 23, 2010 * your MySQL server version --5.1.50 * the raw MySQL statement in question [in a CODE block, and without any PHP variables] INSERT INTO tbl_images ##image## VALUES ##URL## ; -currently the URL is being put in the database but I would like to put the whole image file in it. * the table structure & column indexes of the relevant tables [via SHOW CREATE TABLE is preferred] CREATE TABLE `tbl_images` ( `id` TINYINT(3) UNSIGNED NOT NULL AUTO_INCREMENT, `image` BLOB NOT NULL, PRIMARY KEY (`id`) ) Quote Link to comment https://forums.phpfreaks.com/topic/214152-web-image-to-mysql-via-php/#findComment-1114333 Share on other sites More sharing options...
acee12345 Posted September 28, 2010 Author Share Posted September 28, 2010 Is my format incorrect? Quote Link to comment https://forums.phpfreaks.com/topic/214152-web-image-to-mysql-via-php/#findComment-1116790 Share on other sites More sharing options...
fenway Posted September 29, 2010 Share Posted September 29, 2010 Is my format incorrect? Yes -- echo the actual query, not with placeholders. Quote Link to comment https://forums.phpfreaks.com/topic/214152-web-image-to-mysql-via-php/#findComment-1117161 Share on other sites More sharing options...
seth001 Posted October 26, 2010 Share Posted October 26, 2010 Hey I was looking for something similar. Would you happen to have the final code once your done? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/214152-web-image-to-mysql-via-php/#findComment-1126854 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.