PDA

View Full Version : MYSQL field loop with increment by 1 each loop


Nautik97
02-10-2010, 03:54 PM
Im having a problem with a mysql loop.
I dont have it typed out but youll understand what im after better from this example than if i show you the code i have at the moment.

abc.php

$url = ("SELECT url FROM state WHERE id = 1")

$ch=curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
$output = curl_exec($ch)




what im after is to have abc.php run through the curl, loop it, and when it loops auto increment id = 1 to id = 2.

say google.com is id = 1
and yahoo.com is id = 2

the script needs to execute so $url = google.com, then loop, so $url = yahoo.com.


Ive been trying to find it on google but everything is for insert and/or update and i dont need either one, i just have to echo one row then loop and increment to the next.

Any help on this would be much appreciated.


i hope that makes sense.

votter
02-10-2010, 04:03 PM
do you mean something like this:



$url = mysql_query("SELECT `url` FROM `state`") or die(mysql_error());
while ($row = mysql_fetch_array($url)) {

echo $row;

}

Nautik97
02-10-2010, 04:41 PM
eh here.


<?PHP
$link = mysql_connect('localhost', 'root', '');
mysql_select_db('cityfest', $link);

function storeLink($url,$gathered_from) {
$query = "INSERT IGNORE INTO cities (url, gathered_from) VALUES ('$url', '$gathered_from')";
mysql_query($query);
}

-----------this is my php loop-----------
for ($j=0;j<21;$j++){
$urls = 'http://www.xxxxxxx.com/xxxxxxxxxxx.aspx?pn=' . $j . '&c=xxxxx&nt=xxx';
$userAgent = 'Googlebot/2.1 (http://www.googlebot.com/bot.html)';
$target_url = $urls;
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
curl_setopt($ch, CURLOPT_URL,$target_url);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
$html= curl_exec($ch);
if (!$html) {
echo "<br />cURL error number:" .curl_errno($ch);
echo "<br />cURL error:" . curl_error($ch);
exit;
}

$dom = new DOMDocument();
@$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
$hrefs = $xpath->evaluate(".//*[@id='main']/div[2]/a");


for ($i = 0; $i < $hrefs->length; $i++) {
$href = $hrefs->item($i);
$url = $href->getAttribute('href');
storeLink($url,$target_url);
echo "<br />Link stored: $url";
}
}
-------------------------

?>





everything thats inside of my loop is repeated and in $urls is where i need the urls.

But i have 19000 urls to go through, so i cant do it by hand.

i need it to run through my loop the first time with id = 1,

when it logs to the database i need it to change from id = 1 to the next url, which is id = 2

guelphdad
02-10-2010, 04:52 PM
so did you look at the post above yours? It is the most straightforward example of what you would be looking for.

Nautik97
02-10-2010, 05:01 PM
Oh ok. I didnt know that im not too good with the sql thing yet. I can get it to echo data from an sql table just not increase the output by 1 row in a loop as i need it unless it involves a single query for multiple rows and echoing them in html all at once with [1] etc.
im assuming another for statement could take care of that though?

guelphdad
02-10-2010, 08:31 PM
Read through the PHP manual. Search on: mysql_fetch_array and follow the example there.