查询在PHP中运行一次

时间:2017-08-20 20:05:59

标签: php mysql database cron

我使用while循环创建了一个PHP文件,当我使用cron job运行我的PHP文件时,它只运行一次!

  

/ usr / bin / wget -O / dev / null http://alhyipmonitors.com/sites.php   1> / dev / null 2>& 1

我在我的监视器数据库表中有22个监视器ID,我从中选择。

这是我的代码:

$q='select * from `monitors`';
            $result=@mysql_query($q);
            while($row=@mysql_fetch_array($result))
            {
            $mid =  $row['mid'];

    $q='select * from `monitors` where `mid`="'.$mid.'"';
    $result=mysql_query($q);
    $row=mysql_fetch_array($result);
    echo'<h2>'.$row['monitor_url'].'</h2>';

        if($row['robot_target'])
        {
            $url=$row['robot_target'];
            $ch=curl_init();
            curl_setopt($ch, CURLOPT_URL,$url);
            $handle=fopen('src.txt','w');
            curl_setopt($ch, CURLOPT_FILE, $handle);
            curl_exec($ch);
            curl_close($ch);
            fclose($handle);
            $handle=fopen('src.txt','r');
            $html_code=fread($handle, filesize('src.txt'));
            fclose($handle);

            if($html_code!='')
            {
                if(preg_match_all($row['robot_pattern'], $html_code, $match))
                {
                    $i=0;
                    $j=0;
                    while($j<=50 && $match[$row['robot_id_index']][$i] && $match[$row['robot_key_index']][$i])
                    {
                        $id=$match[$row['robot_id_index']][$i];
                        $key1=$match[$row['robot_key_index']][$i];
                        $key2=remove_extra_in_url($key1);
                        $key3=remove_extra_in_string($key1);
                        $key4=str_replace(' ','-',$key1);
                        $button=$row['button_pattern'].$id.$row['end_button_pattern'];

                        $details=$row['details_pattern'].$id.$row['end_details_pattern'];
                        echo $i.'. ID= <b>'.$id.'</b>, KEY1= <b>'.$key1.'</b>, KEY2= <b>'.$key2.'</b>, KEY3= <b>'.$key3.'</b>, KEY4= <b>'.$key4.'</b>, <a href="'.$button.'" target="_blank">BUTTON</a>, <a href="'.$details.'" target="_blank">DETAILS</a><br/>';
                        $q='select * from `buttons` where `button_url`="'.$button.'"';

                        if($num=mysql_num_rows(mysql_query($q))==0)
                        { 
                            $q='select * from `programs` where `url` like "%'.$key1.'%" or `url` like "%'.$key2.'%" or `url` like "%'.$key3.'%" or `url` like "%'.$key4.'%" or `url`="'.$key2.'" order by `pid` desc';
                            $result=mysql_query($q);
                            $num=mysql_num_rows($result);
                            if($num==1)
                            {
                                $program=mysql_fetch_array($result);
                                $piid = $program['pid'];
                                $urll = $program['url'];

                                // Attempt update query execution

$data='update `programs` set `monitors`=`monitors`+1 where pid="'.$program['pid'].'" and url="'.$program['url'].'"';

$val=mysql_query($data);
if($val == 1){

    $q='insert into `buttons` values("","'.$program['pid'].'","'.$row['mid'].'","'.$details.'","'.$button.'")';
                                if(mysql_query($q))
                                    echo'<div class="green">'.$program['url'].' -> Added.</div>';
                                $j++;                           
}                           

                            }
                            elseif($num>1)
                            {
                                while($program=mysql_fetch_array($result))
                                {
                                    echo'<div class="blue">'.$program['url'].' [ <a href="fr.php?a=add_button&mid='.$row['mid'].'&pid='.$program['pid'].'&bid='.$id.'" target="_blank">ADD FOR THIS PROGRAM</a> ]</div>';
                                }
                                $j++;
                            }
                            else
                                echo'<b style="color: red;">Not Found</b><br/>';
                    }
                        else
                            echo'<b style="color: green;">Button URL is already exist</b><br/>';
                        echo'<br/><hr/>';   
                        $i++;
                    }
                }
                else
                    echo'Preg Match -> Not Found';
            }
            else
                echo'file get contents -> error';
        }
        else
            echo'robot target -> not fount';
    }   

欢迎提供各种帮助,谢谢!

2 个答案:

答案 0 :(得分:0)

在我看来,你正在循环中重用变量$ result。

$q = 'select * from `monitors`';
$result = @mysql_query($q);
while( $row = @mysql_fetch_array($result) ) // <-- $result var
{
    $mid =  $row['mid'];

    $q = 'select * from `monitors` where `mid`="'.$mid.'"';

    // $result = mysql_query($q); // <--- overwrite $result var
    $result1 = mysql_query($q);  // <--- use a different var name
    $row = mysql_fetch_array($result1); // <--- change here too
    echo'<h2>'.$row['monitor_url'].'</h2>';
    // more code etc
}

答案 1 :(得分:0)

由于您在$result循环中重置while,要解决此问题,将内部$result重命名为另一个,我已通过评论指定了已编辑的行

<?php

$q='select * from `monitors`';
            $result=@mysql_query($q);
            while($row=@mysql_fetch_array($result))
            {
            $mid =  $row['mid'];

    $q='select * from `monitors` where `mid`="'.$mid.'"';
    $result=mysql_query($q);
    $row=mysql_fetch_array($result);
    echo'<h2>'.$row['monitor_url'].'</h2>';

        if($row['robot_target'])
        {
            $url=$row['robot_target'];
            $ch=curl_init();
            curl_setopt($ch, CURLOPT_URL,$url);
            $handle=fopen('src.txt','w');
            curl_setopt($ch, CURLOPT_FILE, $handle);
            curl_exec($ch);
            curl_close($ch);
            fclose($handle);
            $handle=fopen('src.txt','r');
            $html_code=fread($handle, filesize('src.txt'));
            fclose($handle);

            if($html_code!='')
            {
                if(preg_match_all($row['robot_pattern'], $html_code, $match))
                {
                    $i=0;
                    $j=0;
                    while($j<=50 && $match[$row['robot_id_index']][$i] && $match[$row['robot_key_index']][$i])
                    {
                        $id=$match[$row['robot_id_index']][$i];
                        $key1=$match[$row['robot_key_index']][$i];
                        $key2=remove_extra_in_url($key1);
                        $key3=remove_extra_in_string($key1);
                        $key4=str_replace(' ','-',$key1);
                        $button=$row['button_pattern'].$id.$row['end_button_pattern'];

                        $details=$row['details_pattern'].$id.$row['end_details_pattern'];
                        echo $i.'. ID= <b>'.$id.'</b>, KEY1= <b>'.$key1.'</b>, KEY2= <b>'.$key2.'</b>, KEY3= <b>'.$key3.'</b>, KEY4= <b>'.$key4.'</b>, <a href="'.$button.'" target="_blank">BUTTON</a>, <a href="'.$details.'" target="_blank">DETAILS</a><br/>';
                        $q='select * from `buttons` where `button_url`="'.$button.'"';

                        if($num=mysql_num_rows(mysql_query($q))==0)
                        { 
                            $q='select * from `programs` where `url` like "%'.$key1.'%" or `url` like "%'.$key2.'%" or `url` like "%'.$key3.'%" or `url` like "%'.$key4.'%" or `url`="'.$key2.'" order by `pid` desc';
                            $innser_result=mysql_query($q); // Here change $result to $inner_result or what you want
                            $num=mysql_num_rows($innser_result); // Here change $result to $inner_result or what you want
                            if($num==1)
                            {
                                $program=mysql_fetch_array($innser_result); // Here change $result to $inner_result or what you want
                                $piid = $program['pid'];
                                $urll = $program['url'];

                                // Attempt update query execution

$data='update `programs` set `monitors`=`monitors`+1 where pid="'.$program['pid'].'" and url="'.$program['url'].'"';

$val=mysql_query($data);
if($val == 1){

    $q='insert into `buttons` values("","'.$program['pid'].'","'.$row['mid'].'","'.$details.'","'.$button.'")';
                                if(mysql_query($q))
                                    echo'<div class="green">'.$program['url'].' -> Added.</div>';
                                $j++;                           
}                           

                            }
                            elseif($num>1)
                            {
                                while($program=mysql_fetch_array($innser_result)) // Here change $result to $inner_result or what you want
                                {
                                    echo'<div class="blue">'.$program['url'].' [ <a href="fr.php?a=add_button&mid='.$row['mid'].'&pid='.$program['pid'].'&bid='.$id.'" target="_blank">ADD FOR THIS PROGRAM</a> ]</div>';
                                }
                                $j++;
                            }
                            else
                                echo'<b style="color: red;">Not Found</b><br/>';
                    }
                        else
                            echo'<b style="color: green;">Button URL is already exist</b><br/>';
                        echo'<br/><hr/>';   
                        $i++;
                    }
                }
                else
                    echo'Preg Match -> Not Found';
            }
            else
                echo'file get contents -> error';
        }
        else
            echo'robot target -> not fount';
    }