从MySQL数据库中获取记录时,php显示为null

时间:2015-03-23 15:29:04

标签: php mysql push-notification google-cloud-messaging

我正在尝试使用php类使用GCM将数据发送到app(phonegap)。 这里数据存储到数据库中,并使用Php-GCM类发送。 问题是它在发送所有列时显示空值。

<?php
class GCMPushMessage {
    var $url = 'https://android.googleapis.com/gcm/send';
    var $serverApiKey = "xxxxxxx";
    var $devices = 0;

    function setDevices($deviceIds)
    {

        if(is_array($deviceIds)){
            $this->devices = $deviceIds;
        } else {
            $this->devices = array($deviceIds);
        }
    }
    function send($message, $data = false)
       {
        if(!is_array($this->devices) || count($this->devices) == 0){
            $this->error("No devices set");
        }

        if(strlen($this->serverApiKey) < 8){
            $this->error("Server API Key not set");
        }

        $fields = array(
            'registration_ids'  => $this->devices,
            'data'              => array( "message" => $message ),
        );

        if(is_array($data)){
            foreach ($data as $key => $value) {
                $fields['data'][$key] = $value;
        }
        }
        $headers = array( 
            'Authorization: key=' . $this->serverApiKey,
            'Content-Type: application/json'
        );
        // Open connection
        $ch = curl_init();

        // Set the url, number of POST vars, POST data
        curl_setopt( $ch, CURLOPT_URL, $this->url );

        curl_setopt( $ch, CURLOPT_POST, true );
        curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );

        curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $fields ) );

        // Avoids problem with https certificate
        curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false);

        // Execute post
        $result = curl_exec($ch);

        // Close connection
        curl_close($ch);

        return $result;
    }

}
    $id=$_POST['id'];
    $diagnosis=$_POST['diagnosis'];
    $instructions=$_POST['instructions'];
    $doc_name=$_POST['doc_name'];
    $med_id=time().rand(11,99).time();
    $str="insert into prescription values('$id','$diagnosis','$instructions','$doc_name','$med_id')";
    $res=@mysql_query($str)or die(mysql_error());
    $nf=$_POST['nf'];
    $i=1;
    while($i<=$nf)
        {
        $medicine=' ';
        $tm1=$tm2=$tm3=0;
        $medicine=$_POST['medicine_'.$i];
        $tm='';
        if(isset($_POST['tm_1_'.$i]))
            {$tm1=1;}
        if(isset($_POST['tm_2_'.$i]))
            {$tm2=1;}
        if(isset($_POST['tm_3_'.$i]))
            {$tm3=1;}
        $dosage=$_POST['dosage_'.$i];
        $str="insert into medicine values('$med_id','$dosage','$medicine','$tm1','$tm2','$tm3')";
        $res=@mysql_query($str)or die(mysql_error());
        $i++;
        }
        $id = $_POST['id']; 
        $gcpm = new GCMPushMessage();
$sql=mysql_query("select token from device where id=".$id);
$rs=mysql_fetch_assoc($sql);
$gcpm->setDevices($rs['token']);
$query1=mysql_query("select * from medicine,prescription where med_id=mid and id=".$id);
while($rs1=mysql_fetch_assoc($query1))
{
$rows[]['medicine_name']=$rs['medicine_name'];
$rows[]['tm_1']=$rs['tm_1'];
$rows[]['tm_2']=$rs['tm_2'];
$rows[]['tm_3']=$rs['tm_3'];
$rows[]['dosage']=$rs['dosage'];
}
$rows[]['diagnosis']=$rs['diagnosis'];
$rows[]['instructions']=$rs['instructions'];
print_r($rows);
$response = $gcpm->send($message, $rows);
?>

当我尝试显示 $ rows 时,它显示所有项目的空值。但是数据正在插入到数据库中。很抱歉发布了整个代码。我是新手。请帮忙。

1 个答案:

答案 0 :(得分:0)

由于您尚未发布日志cat,因此很难找出问题的根本原因。错误的确切原因可能是NULL值,但它出现在哪一行会有助于更多地诊断问题。

我想请你看看这个tutorial处理从两个表中插入和提取多行的问题。

对于GCM实施,请查看此tutorial。并确保结构是模块化的,如示例中所述。

希望有助于!!