无法从数据库中检索序列化数据

时间:2014-06-23 11:13:01

标签: php mysql serialization

我能够使用序列化数据正确更新数据库,但我无法正确检索它。当我尝试从数据库中反序列化并从数据库中检索它时,它只回显Array。

以下是我使用序列化进行更新的方法:

$query ="Update dashboard_widget_users set dashboard_widget_users.configuration='".serialize($tmp)."' where dashboard_widget_users.dsnr_yw_user =12 and dashboard_widget_users.dsnr_dashboard_widget=1";

结果:

a:4:{i:0;s:30:""Anzahl_Fahrzeuge_ohne_Bilder"";i:1;s:22:""Fahrzeuge_ohne_Preis"";i:2;s:23:""Fahrzeuge_mit_Fehlern"";i:3;s:19:""Herausforderungen"";}

这就是我尝试unserialize()并从数据库中检索它但我没有正确输出的方法:

$config= unserialize($row['configuration']);
echo $config['configuration'];

这是我的代码:

if($param['aktion'] == 'save-widget-vehicle')
{
    $page['register-fahrzeuge'] = array(
        1   => array( 'Fahrzeug','aktiv',$page['script'],''),
        0   => array( 'Edit-Fahrzeug','enabled',$page['script'],'',''), 
    );

        $opts = !empty($param['filterOpts']) ? $param['filterOpts'] : array();

      $tmp = array();
      foreach ($opts as $opt) {
            $tmp[] = '"'.$opt.'"';
      }
      $data = array();

     $query ="UPDATE dashboard_widget_users w SET w.configuration=\"".mysql_real_escape_string(serialize($tmp))."\" WHERE w.dsnr_yw_user =12 AND w.dsnr_dashboard_widget=1";            
     $result = mysql_query($query, $myConnection);

        $html = '<table width="538" cellspacing="0" cellpadding="0" border="0">
                <tr>
                    <td>
                        <div>'.CreateRegister($page['register-news']).'</div>
                        '.CreateMessage().'
                        <div class="cont-liste-verlauf register"> 
                        <table id="fahrzeuge"> 

                                        <tr>
                                    <td>                                        

                                    </td>                               
                            </tr>                               


                        </table>            
                    </div>
                </td>
            </tr>
        </table>';


                    $return = array(
            'status' => 1,
            'html'  => $html
        );

    echo json_encode($return);
    die();

     $param['aktion'] = 'get-widget-vehicle';
}


if($param['aktion'] == 'get-widget-vehicle')
{
    $page['register-fahrzeuge'] = array(
        1   => array( 'Fahrzeug','aktiv',$page['script'],''),
        0   => array( 'Edit-Fahrzeug','enabled',$page['script'],'',''), 
    );

                $data = array();

                    $opts = !empty($param['filterOpts']) ? $param['filterOpts'] : array();

                $tmp = array();
                foreach ($opts as $opt) {
                $tmp[] = '"'.$opt.'"';
                }

            $query ='SELECT dashboard_widget_users.configuration
                                         FROM dashboard_widget_users
                                         INNER JOIN yw_user ON dashboard_widget_users.dsnr_yw_user = yw_user.intern
                                         INNER JOIN dashboard_widget ON dashboard_widget_users.dsnr_dashboard_widget = dashboard_widget.id
                                         WHERE dashboard_widget_users.dsnr_yw_user =12 AND dashboard_widget.id =1';  
        $result = mysql_query($query, $myConnection);

      /*while ($row = mysql_fetch_assoc($result)) {
        if($row['name'] == 'Anzahl_Fahrzeuge_mit_und_ohne_Bilder')
        {
            $sql = 'SELECT * FROM amo_bilder_live l LEFT JOIN afzdaten a ON l.dsnr_afzdaten = a.dsnr WHERE l.dsnr_afzdaten IS NULL';
            $sql_result=mysql_query($sql);
            //get result...
            // $row['number'] = $result
        }
        $data[] = $row;
    }*/

        $html = '<table width="538" cellspacing="0" cellpadding="0" border="0" >
            <tr>
                <td>
                <div>'.CreateRegister($page['register-fahrzeuge']).'</div>
                '.CreateMessage().'
                <div class="cont-liste-verlauf register"> ';
                while ($row = mysql_fetch_assoc($result)) {
                    $config= unserialize($row['configuration']);
                    //$config= $row['configuration'];               
                    $html .= '<table id="fahrzeuge"> 
                                <a href="amo_fahrzeuge.php"> '. $config .'</a>                  
                    </table>';
                }
            $html .= '</div>

            </td>
        </tr>
    </table>';

    $return = array(
        'status' => 1,
        'html'  => $html
    );

    echo json_encode($return);
    die();

}

3 个答案:

答案 0 :(得分:0)

你总是根据的位置转义字符串,所以在将放入数据库之前对值使用mysqli_real_escape_string() - 而不是在检索它时来自数据库:

 $row=mysqli_fetch_assoc($result);
 $config=unserialize($row['config']);

您已使用jsonserializer对此进行了标记 - 但您在此处使用的是PHP序列化。

答案 1 :(得分:0)

只需要将所有内容反序列化。为什么mysql_escape_string?

如果

$dbresult = 'a:4:{i:0;s:30:""Anzahl_Fahrzeuge_ohne_Bilder"";i:1;s:22:""Fahrzeuge_ohne_Preis"";i:2;s:23:""Fahrzeuge_mit_Fehlern"";i:3;s:19:""Herausforderungen"";}';
$data = unserialize($dbresult);
print_r($data);

答案 2 :(得分:0)

$query ="UPDATE dashboard_widget_users w SET w.configuration=\"".mysql_real_escape_string(serialize($tmp))."\" WHERE w.dsnr_yw_user =12 AND w.dsnr_dashboard_widget=1";

新的get-widget-vehicle

<?php
if($param['aktion'] == 'get-widget-vehicle')
{
    $page['register-fahrzeuge'] = array(
            1 => array( 'Fahrzeug','aktiv',$page['script'],''),
            0 => array( 'Edit-Fahrzeug','enabled',$page['script'],'',''), 
        );

        $data = array();
        $opts = !empty($param['filterOpts']) ? $param['filterOpts'] : array();

        $tmp = array();
        foreach ($opts as $opt) {
            $tmp[] = '"'.$opt.'"';
        }

        $query ='SELECT w.configuration
                 FROM dashboard_widget_users w
                 INNER JOIN yw_user ON w.dsnr_yw_user = yw_user.intern
                 INNER JOIN dashboard_widget ON w.dsnr_dashboard_widget = dashboard_widget.id
                 WHERE w.dsnr_yw_user =12 AND dashboard_widget.id =1';

        $result = mysql_query($query, $myConnection);
        $row = mysql_fetch_row($result);
            var_dump($row);
        $row = $row[0]; //take first array element as result -- please LIMIT your query!

        $config = $row; //store config string to $config
            var_dump($config);
            die();
        $config = unserialize($config); //unserialize $config

        $html = '<table width="538" cellspacing="0" cellpadding="0" border="0" >
            <tr>
                <td>
                <div>'.CreateRegister($page['register-fahrzeuge']).'</div>
                '.CreateMessage().'
                <div class="cont-liste-verlauf register"> ';
                /*
                //do whatever you want to do with $config
                foreach ($config as $k => $v)
                    echo "$k = $v<br />";
                */
            $html .= '</div>

            </td>
        </tr>
    </table>';

    $return = array(
        'status' => 1,
        'html'  => $html
    );

    echo json_encode($return);
    die();
}
?>