dsnw2651 2012-08-23 06:15
浏览 323

使用MySQL INSERT的脚本在我直接运行时起作用,但不作为函数运行

I have a script that makes 2-3 database queries , 2 inserts, and 1 update. The issue I am having is that when I manually test the script by going to the web server page like www.domain.com/handler.php?a=1&b=2&c=3, I get the 3 updates perfectly. But when I use this function, it doesn't work... more specifically, the INSERT INTO doesn't work. I get the update in the database perfectly fine!, but no new entries are made.

while (($data = fgetcsv(STDIN)) !== false){
    $url='http://domain.com';
    $date = date('Y-m-d');
    $type = trim($data[0]);
    if ($type != ''){
        $send_date = $data[2];
        $email = $data[4];
        $status = $data[7];
        $error = $data[8];
        $bounce = $data[10];
        $ip = $data[14];
        $job_id = $data[19];
        $params = 'pmta_token='.$pmta_token.'&type='.$type.'&send_date='.$send_date.'&email='.$email.'&status='.$status.'&error='.$error.'&bounce='.$bounce.'&ip='.$ip.'&job_id='.$job_id;
        $params= array('server'=>$_SERVER['SERVER_NAME'],'type'=>$data[0],'time'=>$data[1],'message'=>$data[8],'bouncecat'=>$data[10],'jobid'=>$data[19],'domip'=>$data[21]);
        $line = $type.','.$email.','.$ip.','.$status.','.$error.','.$bounce.','.$job_id.','.date('c')."
";

        curl_post_async($url, $params);
    }
}
    function curl_post_async($url, $params){
        foreach($params as $key => $val){
          if (is_array($val)) $val = implode(',', $val);
        $post_params[] = $key.'='.urlencode($val);
    }
    $post_string = implode('&', $post_params);

    $parts=parse_url($url);

    $fp = fsockopen($parts['host'],
        isset($parts['port'])?$parts['port']:80,
        $errno, $errstr, 30);

    $out = "POST ".$parts['path']." HTTP/1.1
";
    $out.= "Host: ".$parts['host']."
";
    $out.= "Content-Type: application/x-www-form-urlencoded
";
    $out.= "Content-Length: ".strlen($post_string)."
";
    $out.= "Connection: Close

";
    if (isset($post_string)) $out.= $post_string;

    fwrite($fp, $out);
    fclose($fp);
}

I must be missing something! the reason I am using this function instead of curl, is because of the time curl takes connecting and waiting for a response... this script is handling about 2,000 requests a minute.

here is the handler.php

this is the one that is failing and not entering anything into the DB, no errors when I do it manually, it works like a charm, but using the socket fails, ONLY on the INSERT, the UPDATE works through the socket connect.?.?.?

mysql_connect('localhost','root','');
mysql_select_db('analyzer');
date_default_timezone_set('America/Los_Angeles');

if(isset($_POST['type'])){
    $domip = @explode('/',$_POST['domip']);
    $server = @strtolower($_POST['server']);
    $type = @$_POST['type'];
    $time = @$_POST['time'];
    $message = @$_POST['message'];
    $bouncecat = @$_POST['bouncecat'];
    $jobid = @$_POST['jobid'];
    $domain = @$domip[0];
    $md5 = @$_POST['md5'];
    $ip = @$domip[1];
    if(isset($_POST['ip'])){$ip=$_POST['ip'];}
}else{
    $domip = @explode('/',$_REQUEST['domip']);
    $server = @strtolower($_REQUEST['server']);
    $type = @$_REQUEST['type'];
    $time = @$_REQUEST['time'];
    $message = @$_REQUEST['message'];
    $bouncecat = @$_REQUEST['bouncecat'];
    $jobid = @$_REQUEST['jobid'];
    $domain = @$domip[0];
    $md5 = @$_REQUEST['md5'];
    $ip = @$domip[1];
}
$now=date('Y-m-d H:i');
@mysql_query("INSERT INTO `log` (`type`,`time`,`message`,`cat`,`jobid`,`domain`,`ip`,`server`) VALUES('$type','$time','$message','$bouncecat','$jobid','$domain','$ip','$server')");
if(strpos($message,'ALERT'!==false)){
    @mysql_query("INSERT INTO `alerts` (`type`,`time`,`message`,`level`,`value`,`page`) VALUES('error','$now','$message','5','$ip','apps/loganalyzer/incl/datatracker.php')");
}
if($type=='d'){
    @mysql_query("UPDATE `$server` SET `delivered`=`delivered`+1 WHERE `time`='$now'");
}elseif(($type=='b')&&($bouncecat!='bad')){
    @mysql_query("UPDATE `$server` SET `errored`=`errored`+1 WHERE `time`='$now'");
}elseif(($type=='b')&&($bouncecat=='bad')){
    @mysql_query("UPDATE `$server` SET `bounced`=`bounced`+1 WHERE `time`='$now'");
}elseif($type=='c'){
    @mysql_query("UPDATE `$server` SET `unsubscribed`=`unsubscribed`+1 WHERE `time`='$now'");
}
  • 写回答

1条回答 默认 最新

  • duanmengsuo9302 2012-09-15 22:10
    关注

    While I don't know exactly what the problem is, I have 2 suggestions for you to debug it:

    1. Remove the @ sign before the functions - that is hiding you the error you need to fix.
    2. Make a small script to test the function with curl out of your system, so that you can actually get to see what is wrong with the INSERT query.
    评论

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀