Page 1 of 1

curl loop problem

Posted: Thu Oct 09, 2008 1:33 pm
by rbarnett
I'm trying to grab some data off of a list of webpages and display it to the page. The list of urls are from a mysql table and I then use the curl function to grab the data. The only problem is that I only get the first row.

Can anyone please tell me what I am doing wrong? mysql_num_rows displays 86 rows and when I substitute the $row variable from the mysql_fetch_row with an array I built with all of the urls and put that into the foreach statement it works fine but, of course, I would rather get the urls from mysql.

Here is the code:

Code: Select all

 
$query = "select website from vtiger_account where website <> ''";
$result = mysql_query($query);
 
$num_rows = mysql_num_rows($result);
echo $num_rows . ' rows found<br />';
 
echo '<table border ="1">
    <th>District</th><th>Version</th>';
while ($row = mysql_fetch_row($result))
{ 
    foreach ($row as $url)
    {
        echo '<tr><td>'.$url.'</td>';
 
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
 
        $data = curl_exec ($ch);
        curl_close ($ch);
                
        $length=strpos($data,"Version: ");
        $result=substr($data,$length +9,32);
                
        if (preg_match("/Version:/", $data))
        {
            echo '<td>'.$result . '</td></tr>';
        }
        else
        {
            echo '<td>No version found</td</tr>';       
        }
 
    }
        
 
}   
echo '</table>';
 

Re: curl loop problem

Posted: Thu Oct 09, 2008 2:36 pm
by js2007
You overwrite the mysql result in line 26 of your example with other data. Use another variable name for the substr() result.