PHP:unserialize() 期望参数 1 为字符串

问题描述 投票:0回答:4

如果我删除此代码,一切都会正常工作,但我需要循环接收到的数据(序列化)中的产品。当我使用这段代码时,它会崩溃,我不知道为什么。

$products = db_query("SELECT cart FROM ?:abandoned_cart WHERE user_id = ?s", $acId);
//fn_print_die($products);
$products = unserialize($products);
$shippingCost = db_get_field("SELECT shipping FROM ?:abandoned_cart WHERE user_id = ?s", $acId);
$tax = db_get_field("SELECT tax FROM ?:abandoned_cart WHERE user_id = ?s", $acId);
$orderTotal = db_get_field("SELECT order_total FROM ?:abandoned_cart WHERE user_id = ?s", $acId);
$email = db_get_field("SELECT email FROM ?:abandoned_cart WHERE user_id = ?s", $acId);

$sum=0;
//echo $products;
if (!empty($products)) {

  foreach ($products as $product) {
    $text .='
      <tr>
        <td><a  href="http://'.$_SERVER['SERVER_NAME'].'?dispatch=products.view&product_id='.$product['product_id'].'"> <img title="" height="120" width="120" alt="" src="'.$product['main_pair']['detailed']['image_path'].'"></a></td>
        <td><a href="#" style=" font-weight:bold; color:#333; font-size:13px; text-decoration:none;">'.$product['product'].'</a><a href="#">&nbsp;<i></i></a><div style=" font-weight:bold; color:#333; font-size:12px; margin-top:4px; text-decoration:none;"> CODE: <span>'.$product['product_code'].'<!--product_code_update_2512012004--></span> </div></td>
        <td style=" text-align:center;"><span style=" font-weight:bold; color:#333; font-size:12px; margin-top:4px; text-decoration:none;">$</span><span style=" font-weight:bold; color:#333; font-size:12px; margin-top:4px; text-decoration:none;">'.$product['price'].'</span> </td>
        <td><div style="display: inline-block;vertical-align: top;width: 56px;"><input type="text" disabled value="'.$product['amount'].'" size="3"  style="border:1px solid #c2c9d0; box-shadow:0 1px 3px rgba(0, 0, 0, 0.1) inset; border-radius:3px; float: left;height: 33px;text-align: center;width: 36px;"></div></td>
        <td style="font-size:14px;  font-weight:bold; color:#333; text-align:center; font-size:13px; text-decoration:none;"><span>$</span><span stye=" color:#000;">'.$product['price']*$product['amount'].'</span> </td>
      </tr>';
    $sum =$sum+$product['price']*$product['amount'];
  }
}

日志中:

[2016 年 2 月 8 日星期一 03:59:42] [错误] [客户端 90.199.142.58] PHP 警告:unserialize() 期望参数 1 为字符串,在 /home/ambcom/public_html/staging/beanbags/app/ 中给出的对象第 24 行的 addons/abandoned_cart_extended/controllers/backend/ac.php,引用地址:/admin.php?dispatch=cart.cart_list

这实际上是反序列化造成的。我试过只删除那部分,其余部分都有效。现在是凌晨 4 点,当我需要在办公室时,我需要在上午 9 点之前完成此工作。

php cs-cart
4个回答
0
投票
$products = db_query("SELECT cart FROM ?:abandoned_cart WHERE user_id = ?s", $acId);

$products
是一个对象,您首先需要在变量中获取购物车值并将其传递给
unserialize
方法,假设您使用的是mysqli,您可以尝试这样

if( $products->num_rows > 0 ){
    $data = $products->fetch_assoc();

    $products = unserialize($row['cart']);

    $shippingCost = db_get_field("SELECT shipping FROM ?:abandoned_cart WHERE user_id = ?s", $acId);
    $tax = db_get_field("SELECT tax FROM ?:abandoned_cart WHERE user_id = ?s", $acId);
    $orderTotal = db_get_field("SELECT order_total FROM ?:abandoned_cart WHERE user_id = ?s", $acId);
    $email = db_get_field("SELECT email FROM ?:abandoned_cart WHERE user_id = ?s", $acId);

    $sum=0;
    //echo $products;
    if (!empty($products)) {

      foreach ($products as $product) {
        $text .='
          <tr>
            <td><a  href="http://'.$_SERVER['SERVER_NAME'].'?dispatch=products.view&product_id='.$product['product_id'].'"> <img title="" height="120" width="120" alt="" src="'.$product['main_pair']['detailed']['image_path'].'"></a></td>
            <td><a href="#" style=" font-weight:bold; color:#333; font-size:13px; text-decoration:none;">'.$product['product'].'</a><a href="#">&nbsp;<i></i></a><div style=" font-weight:bold; color:#333; font-size:12px; margin-top:4px; text-decoration:none;"> CODE: <span>'.$product['product_code'].'<!--product_code_update_2512012004--></span> </div></td>
            <td style=" text-align:center;"><span style=" font-weight:bold; color:#333; font-size:12px; margin-top:4px; text-decoration:none;">$</span><span style=" font-weight:bold; color:#333; font-size:12px; margin-top:4px; text-decoration:none;">'.$product['price'].'</span> </td>
            <td><div style="display: inline-block;vertical-align: top;width: 56px;"><input type="text" disabled value="'.$product['amount'].'" size="3"  style="border:1px solid #c2c9d0; box-shadow:0 1px 3px rgba(0, 0, 0, 0.1) inset; border-radius:3px; float: left;height: 33px;text-align: center;width: 36px;"></div></td>
            <td style="font-size:14px;  font-weight:bold; color:#333; text-align:center; font-size:13px; text-decoration:none;"><span>$</span><span stye=" color:#000;">'.$product['price']*$product['amount'].'</span> </td>
          </tr>';
        $sum =$sum+$product['price']*$product['amount'];
      }
    }

}

0
投票
unserialize in php needs a string and your $product variable was interpreted in php as an object.

错误就是这样出来的。

unserialize() 期望参数 1 为字符串

您可以使用 print_r 或 var_dump 检查 $products 的返回值是什么,以确认它是一个对象而不是字符串。

$products = db_query("SELECT cart FROM ?:abandoned_cart WHERE user_id = ?s", $acId);
print_r($products);

我不明白使用 unserialize 的意义,因为在您发布的代码中您没有使用 serialize

来自unserialize

的文档

unserialize() 接受单个序列化变量并将其转换回来 转换为 PHP 值。

意味着您应该在使用 unserialize 将其转换回 PHP 值之前使用 serialize

已更新

尝试使用 db_get_array()

 $products = db_get_array("SELECT cart FROM ?:abandoned_cart WHERE user_id = ?s", $acId);
//now you can use $products as an array format
if (!empty($products)) {

  foreach ($products as $product) {
    $text .='
      <tr>
        <td><a  href="http://'.$_SERVER['SERVER_NAME'].'?dispatch=products.view&product_id='.$product['product_id'].'"> <img title="" height="120" width="120" alt="" src="'.$product['main_pair']['detailed']['image_path'].'"></a></td>
        <td><a href="#" style=" font-weight:bold; color:#333; font-size:13px; text-decoration:none;">'.$product['product'].'</a><a href="#">&nbsp;<i></i></a><div style=" font-weight:bold; color:#333; font-size:12px; margin-top:4px; text-decoration:none;"> CODE: <span>'.$product['product_code'].'<!--product_code_update_2512012004--></span> </div></td>
        <td style=" text-align:center;"><span style=" font-weight:bold; color:#333; font-size:12px; margin-top:4px; text-decoration:none;">$</span><span style=" font-weight:bold; color:#333; font-size:12px; margin-top:4px; text-decoration:none;">'.$product['price'].'</span> </td>
        <td><div style="display: inline-block;vertical-align: top;width: 56px;"><input type="text" disabled value="'.$product['amount'].'" size="3"  style="border:1px solid #c2c9d0; box-shadow:0 1px 3px rgba(0, 0, 0, 0.1) inset; border-radius:3px; float: left;height: 33px;text-align: center;width: 36px;"></div></td>
        <td style="font-size:14px;  font-weight:bold; color:#333; text-align:center; font-size:13px; text-decoration:none;"><span>$</span><span stye=" color:#000;">'.$product['price']*$product['amount'].'</span> </td>
      </tr>';
    $sum =$sum+$product['price']*$product['amount'];
  }
}

0
投票

简单使用标准的 cs-cart 方法 db_get_field 而不是 db_query:

$products = db_get_field("SELECT cart FROM ?:abandoned_cart WHERE user_id = ?s", $acId);

您的 $products 将仅包含

cart
表的
abandoned_cart
字段。然后您可以根据需要使用此字符串进行反序列化并处理给定的对象。


0
投票

如果您的数据来自 Linux/Unix 类型的服务器,并且您在 Windows 服务器上运行它,则可能是由于 Windows 上的 PHP 的整数限制。在 Windows 上设置开发环境时,我遇到了同样的问题。

以下内容来自php手册。

整数的大小与平台相关,尽管最大值 大约 20 亿是通常的值(即 32 位有符号)。 64 位平台的最大值通常约为 9E18,除了 PHP 7 之前的 Windows 始终是 32 位。 PHP 没有 支持无符号整数。整数大小可以使用以下方式确定 常量 PHP_INT_SIZE,使用常量 PHP_INT_MAX 的最大值 从 PHP 4.4.0 和 PHP 5.0.5 开始,使用常量求最小值 PHP_INT_MIN 自 PHP 7.0.0 起。

© www.soinside.com 2019 - 2024. All rights reserved.