2012-07-13 83 views
0

我使用下面的腳本進行追加的序列化陣列無法訪問數據庫,PHP

mysql_select_db("formdemo", $con); 

    $result=mysql_query("select * from rohit where fid='$y'"); 
    if(mysql_num_rows($result)!=0) 
    { 
     if($result) 
     { 
      while($row=mysql_fetch_array($result)) 
      { 
       $n=$row['tags']; 

       $a=explode(',', $n); 
       print_r($a); 
       $cnt=count($a); 
       $x=unserialize($row['data']); 
       for($i=0;$i<$cnt;$i++) 
       { 
        $d=$_GET[$a[$i]]; 
        array_push($x,$d); 
       } 
       array_push($x,"<br/>"); 
      } 
     } 
    } 
    $str=serialize($x); 
    $sql="update rohit set data='$str' where fid='$y'"; 
    if(!mysql_query($sql,$con)) 
     die(mysql_error()); 
    else 
     echo "Your data has been updated successfully\n"; 

當我在第一次運行這個當數據庫爲空,則給了一個錯誤: array_push ()期望參數1爲數組, 並在數據庫中存儲值b; 0。在這種情況下我該怎麼做。請幫助...

+0

什麼是'$ d'?你試圖把'$ d'推到'$ x'上,但'$ x'是一個字符串 – JamesHalsall 2012-07-13 13:05:39

+0

是的,所以問題是什麼 – 2012-07-13 13:06:50

+0

'$ d'是什麼意思? – JamesHalsall 2012-07-13 13:07:41

回答

1

最簡單的解決方案是在開始處理之前用數組初始化變量$x。但請回顧我的評論Here

+0

這就是我沒有得到如何初始化數組 – 2012-07-13 13:15:28

+0

@Rohitashv:'$ x = array();'? – 2012-07-13 13:16:45

+0

是的,它已經完成了。謝謝 – 2012-07-13 13:17:53