【问题标题】:PHP: unserialize() expects parameter 1 to be stringPHP: unserialize() 期望参数 1 是字符串
【发布时间】:2016-05-17 16:12:06
【问题描述】:

如果我删除此代码一切正常,但我需要从收到的数据(序列化)中循环遍历产品。当我使用此代码时,它会中断,我不知道为什么。

$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'];
  }
}

在日志中:

[Mon Feb 08 03:59:42 2016] [error] [client 90.199.142.58] PHP 警告:unserialize() 期望参数 1 是字符串,对象在 /home/ambcom/public_html/staging/beanbags/ app/addons/abandoned_cart_extended/controllers/backend/ac.php 在第 24 行,referer:/admin.php?dispatch=cart.cart_list

实际上是反序列化中断了。我试过只删除那部分,其余的都可以。现在是凌晨 4 点,当我需要在办公室时,我需要在早上 9 点之前完成这项工作。

【问题讨论】:

  • 错误信息对我来说似乎很清楚
  • 也许对你来说哈哈,但我对 PHP 太陌生了,不知道如何修改来解决它:(
  • db_query 不是 php 核心函数,所以不知道它在做什么。 var_dump($products) 应该告诉你更多
  • 试试$products = db_get_field("SELECT cart FROM ?:abandoned_cart WHERE user_id = ?s", $acId);

标签: php cs-cart


【解决方案1】:
$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'];
      }
    }

}

【讨论】:

  • 它确实使用 mysqli 是的,我应该更改查询并将其放在 foreach 中吗?抱歉,我对此很陌生。
  • @James 在使用fetch_assoc() or fetch_array() 获取值后,您可以在循环中使用它。在此语句 $products = unserialize($row['cart']); 之后打印值 print_r($products); 以检查您是否获得了所需的值
  • 我试了一下:pastebin.com/BXgUArWa 但得到了:[Mon Feb 08 04:40:17 2016] [error] [client 90.199.142.58] PHP 致命错误:调用成员函数 fetch_assoc () 在第 25 行的 /home/ambcom/public_html/staging/beanbags/app/addons/abandoned_cart_extended/controllers/backend/ac.php 中的非对象上,引用:admin.php?dispatch=cart.cart_list
  • 我猜db_get_field() 是您定义的自定义方法,用于从查询结果中获取值。在这种情况下,您的 $products 现在已经是一个关联数组,因此您将不再需要使用 $data = $products-&gt;fetch_assoc();,删除该语句并尝试使用 $products = unserialize($products['cart']); 而不是 $products = unserialize($row['cart']);
  • 我刚刚完成并认为做得正确,我做了一个 pastebin:pastebin.com/7U6h98s3 但是我尝试了请求并且响应是空的,php 日志中没有错误
【解决方案2】:
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 之前使用serialize 将其转换回PHP 值。

更新

尝试使用 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'];
  }
}

【讨论】:

  • 停止破解代码,并通过并收到电子邮件,但我收到的电子邮件从未显示产品等...来自购物车查询的任何数据...
  • 我在该查询后使用:fn_print_die($products);并返回 {"debug_info":[{"current_field":null,"field_count":null,"lengths":null,"num_rows":null,"type":null}],"notifications":[]}
  • 请使用print_r($products),然后用print_r的结果更新你的帖子。这样我们就可以看到$products的输出值了
  • 我刚刚添加了对评论的回复
  • @James 当你使用反序列化时结果不同吗?
【解决方案3】:

简单使用标准cs-cart方法db_get_field代替db_query:

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

您的 $products 将仅包含 abandoned_cart 表的 cart 字段。然后,您可以根据需要使用此字符串进行反序列化并使用给定的对象。

【讨论】:

    【解决方案4】:

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

    The following is from the php manual.

    整数的大小取决于平台,尽管有最大值 大约 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 起。

    【讨论】:

      猜你喜欢
      • 2021-01-13
      • 2016-05-09
      • 1970-01-01
      • 2012-09-02
      • 2014-07-11
      • 1970-01-01
      • 1970-01-01
      • 2017-02-24
      • 2016-11-26
      相关资源
      最近更新 更多