php在从MySQL数据库获取记录时显示null


php showing null when fetching records from MySQL db

我正在尝试使用php类使用GCM将数据发送到应用程序(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时,它显示所有项的空值。但是数据正在被插入数据库中。很抱歉发布了整个代码。我是个新手。请帮忙。

由于您尚未发布日志cat,因此很难找出问题的根本原因。错误的确切原因可能是NULL值,但发生错误的行将有助于诊断问题。

我想让你看看这个教程,它处理从两个表中插入和提取多行的问题。

对于GCM的实现,请看一下本教程。并确保该结构是模块化的,如示例中所述。

希望有帮助!!