James
James

Reputation: 1706

PHP: unserialize() expects parameter 1 to be string

If I remove this code everything works fine, but I need to loop over the products from the data (serialized) received. When I use this code it breaks and I don't know why.

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

In log:

[Mon Feb 08 03:59:42 2016] [error] [client 90.199.142.58] PHP Warning: unserialize() expects parameter 1 to be string, object given in /home/ambcom/public_html/staging/beanbags/app/addons/abandoned_cart_extended/controllers/backend/ac.php on line 24, referer: /admin.php?dispatch=cart.cart_list

It's literally the unserialize that breaks. I've tried removing just that part, and the rest works. It's 4am now, and I need to get this working before 9am when I need to be in the office.

Upvotes: -1

Views: 8818

Answers (4)

afk_joe
afk_joe

Reputation: 1

If your data is coming from a Linux/Unix type server and you are running this on a windows server, then it could be due to a integer limitation with PHP on Windows. I had this same problem when setting up a dev environment on a windows box.

The following is from the php manual.

The size of an integer is platform-dependent, although a maximum value of about two billion is the usual value (that's 32 bits signed). 64-bit platforms usually have a maximum value of about 9E18, except on Windows prior to PHP 7, where it was always 32 bit. PHP does not support unsigned integers. Integer size can be determined using the constant PHP_INT_SIZE, maximum value using the constant PHP_INT_MAX since PHP 4.4.0 and PHP 5.0.5, and minimum value using the constant PHP_INT_MIN since PHP 7.0.0.

Upvotes: 0

Leo Stdin
Leo Stdin

Reputation: 23

Simple use the standard cs-cart method db_get_field instead of db_query:

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

Your $products will contains only cart field of abandoned_cart table. Then you can use this string for unserialize and working with given object as you need.

Upvotes: 1

jameshwart lopez
jameshwart lopez

Reputation: 3131

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

Thats how the error came out.

unserialize() expects parameter 1 to be string

you can check what is the returned value of $products by using print_r or var_dump to confirm that it is an Object and not a string.

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

I dont see the point of using unserialize since in the code you posted you haven't use serialize.

From the documenation of unserialize

unserialize() takes a single serialized variable and converts it back into a PHP value.

Means that you should have used serialize before using unserialize to convert it back in a PHP value.

UPDATED

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

Upvotes: 0

Hari Darshan
Hari Darshan

Reputation: 1920

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

$products is an object you first need to fetch the cart value in a variable and pass that to unserialize method, assuming you are using mysqli you can try like this

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

}

Upvotes: 0

Related Questions