Jump to content

PHP unserialize a serialized value stored in a MySql database


hithere87

Recommended Posts

Hi guys, firstly im a php newbie, so if i sound dumb try not to laugh, what i would like help with if possible is, i have a serialized value representation of items and quantities of a shopping cart stored in my database, done using this code:

 

mysql_query("INSERT INTO `orders` 
(`name`, `surname`, `email`, `address_1`, `address_2`, `county`, `postcode`, `telephone`, `mobile`, `products`, `total`) 
VALUES ('" . $_POST['name'] . "', '" . $_POST['surname'] . "', '" . $_POST['email'] . "', '" . $_POST['address_1'] . "', '" . $_POST['address_2'] . "',
'" . $_POST['county'] . "', '" . $_POST['postcode'] . "', '" . $_POST['telephone'] . "',
'" . $_POST['mobile'] . "', '" . serialize($_SESSION['products']) . "', '" . $total . "')") 

or die(mysql_error());

 

 

Now when i want recall the data in a table on a seperate page, I currently have the following code:

 

$sql = 'SELECT * FROM `orders`'; 
if ($result=mysql_query($sql)) {
    while ($row=mysql_fetch_row($result)) {
    echo "<table cellpadding='5' cellspacing='5' border='1'><tr>";
echo "<td><strong>Order ID:</strong> <br />".$row[0]."<br></td>";
    echo "<td><strong>Name:</strong> <br />".$row[1]."<br></td>";
echo "<td><strong>Surname:</strong> <br />".$row[2]."<br></td>";
echo "<td><strong>E-mail address:</strong> <br />".$row[3]."<br></td>";
echo "<td><strong>Address line 1:</strong> <br />".$row[4]."<br></td>";
echo "<td><strong>Address line 2:</strong> <br />".$row[5]."<br></td>";
echo "<td><strong>County:</strong> <br />".$row[6]."<br></td>";
echo "<td><strong>Postcode:</strong> <br />".$row[7]."<br></td>";
echo "<td><strong>Telephone:</strong> <br />".$row[8]."<br></td>";
echo "<td><strong>Mobile number:</strong> <br />".$row[9]."<br></td>";
echo "<td><strong>Products:</strong> <br />".$row[10]."<br></td>";
echo "<td><strong>Total:</strong> <br />".$row[11]."<br></td>";
echo "</tr></table><br /><br />";
  }
} else {
  echo "<!-- SQL Error ".mysql_error()." -->";
}

 

But this is obviously returning my serialized value in the table cell, and what i am unsure how to do is unserialize it to display the item id and quantiy.

 

Any help will be GREATLY APPRECIATED,

 

Thanks guys  :)

Link to comment
Share on other sites

Thanks guys looks like its the way to do it and what I need.

 

Just having one problem though, how do i get it to be displayed within my table cell, i.e. incorporated with the code below:

 

echo "<td><strong>Products:</strong> <br />".$row[10]."<br></td>";

 

would it be something like this:

 

echo "<td><strong>Products:</strong> <br />".unserialize($row['10'])."<br></td>";

 

 

because it doesnt seem to be working.

 

 

Also once it prints the unserialised array out to me, i.e.

 

Array ( [7] => 2 [5] => 2 ) 

 

by where this is telling me product id 7 was purchased in quantity of 2 and product id 5 was purchased in quantity of 2, how would i get it to display the product instead of the product id, along with the quantity purchased, all instead of the output array. I assume i'd have to assign a if else statement by where if the product id matches this, then output this, but im unsure of the php method for working with and accessing the array.

 

Sorry for being a pain ???, really are doing me a big favour helping me out with finishing this shopping cart. Thanks guys.

Link to comment
Share on other sites

You need to unserialize it first separately and then echo it, like this:

 

    while ($row=mysql_fetch_row($result)) {
    $products = unserialize($row['10']);
    echo "products[7] = {$products[7]}<br>";
  }

 

The {} syntax is another way of writing this:

 

echo "products[7] = " . $products[7] . "<br>";
Link to comment
Share on other sites

You need to unserialize it first separately and then echo it, like this:

 

    while ($row=mysql_fetch_row($result)) {
    $products = unserialize($row['10']);
    echo "products[7] = {$products[7]}<br>";
  }

 

The {} syntax is another way of writing this:

 

echo "products[7] = " . $products[7] . "<br>";

 

Unsure about where the 7 within products has come from, could you explain that bit.

 

As for implementation, i tried the above code and my webpage is just coming out with the word 'Array' now instead....lol so frustrating, seems like i'm making this much harder than it should be. Thanks for the help so far though, been very useful  :)

Link to comment
Share on other sites

Good question :)  I got the 7 from here:

 

Array ( [7] => 2 [5] => 2 )

 

But I guess the array will in general have different numbers there?  In which case you need to loop through it:

 

    while ($row=mysql_fetch_row($result)) {
    $products = unserialize($row['10']);
    foreach ($products as $p_key => $p_val) {
      echo "products[$p_key] = $p_val <br>";
    }
  }

 

That will display each item in $products in turn.  Once you decide how you want it displayed, just change that echo to something else, using $p_key and $p_val.

Link to comment
Share on other sites

Excellent....I'm almost there, and greatly appreciate all the help, theres just one last bit, and that is that the foreach loop is running through all the serialized array values in the database and outputting them all in one block, instead of associating them with the order they are supposed to be with. So it stands as

 

Block of unserialized arrays e.g.

 

Product ID = [4] and Quantity = [9] 
Product ID = [7] and Quantity = [2] 
Product ID = [5] and Quantity = [2]

 

Followed by order details e.g.

 

Order 1 - Name, address, email etc.

Order 2 - Name, address, email etc.

 

Whereas I need it in the following format:

 

 

Product ID = [4] and Quantity = [9]
Order 1 - Name, address, email etc.

Product ID = [7] and Quantity = [2] 
Product ID = [5] and Quantity = [2]
Order 2 - Name, address, email etc.

 

By where the product id's and quantitys are the orders placed by the customers below them.

 

Promise this is the last bit i'll bother you with lol.

 

Thanks though been great help.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.