I am fairly new to PHP and mysql and need some help to select data from a MYSQL database field, and based on that update another field using a php script. I managed to piece together the PHP script that returns the URL of the first image result.
Note: There are multiple rows in the table
Is this possible?
The last word in this piece of script eg "ystervark" needs to be the contents from field 1.
get_url_contents('http://ajax.googleapis.com/ajax/services/search/images?v=1.0&q=ystervark'); // I need this word 'ystervark' to be replaced by the contents of field1
This returns the first googled image url:
<?php
function get_url_contents($url) {
$crl = curl_init();
curl_setopt($crl, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)');
curl_setopt($crl, CURLOPT_URL, $url);
curl_setopt($crl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($crl, CURLOPT_CONNECTTIMEOUT, 5);
$ret = curl_exec($crl);
curl_close($crl);
return $ret;
}
$json = get_url_contents('http://ajax.googleapis.com/ajax/services/search/images?v=1.0&q=ystervark'); // I need this word 'ystervark' to be replaced by the contents of field1
$data = json_decode($json);
foreach ($data->responseData->results as $result) {
$results[] = array('url' => $result->url, 'alt' => $result->title);
}
//print_r($results);
?>
<?php foreach($results as $image):
?>
<?php echo ($image['url']);?></br> // This result needs to be updated to field2
<?php
break;
?>
<?php endforeach; ?>
Then the result from the code needs to be updated/inserted into field 2
<?php echo ($image['url']);?></br> // This result needs to be updated to field2
Thank you.