Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Serialize (PHP)

Posted on 2009-05-12
6
267 Views
Last Modified: 2013-12-12
Following up from Webvogel's excellent advice, I'm now trying to retrieve serialized() data from a MySQL database. The data is being sent to the database row as "a:1:{i:0;s:25:"CompanyName field changed";}" which is great, as I know it works. The problem therein, however, is pulling it back to unserialize it.

Even if I do a simple <?php echo unserialize($row2['Adjustment']); ?> it returns a blank value. Yet, doing an echo $row2['Adjustment'] returns the serialized stuff mentioned above. For everything else, it returns "Array".

I'll attach the code I'm using. Any help would, as always, be much appreciated. I expect I'm doing something really quite simple (as usual). Thanks.
/* snippet */
/* the <div> is basically a mouseover tooltip that is meant to display the unserialized data from the database....hence why I called the variable $tooltip....  ;)  */
 
<?php do { ?>
 
<?php if ($row2['Adjustment'] == "emailed") { echo "emailed"; } else if ($row2['Adjustment'] == "printed") { echo "printed"; } else if ($row2['Adjustment'] == "bookmarked") { echo "bookmarked"; } else if ($row2['LatestModification'] <> "") { echo "<div onMouseover=\"ddrivetip('".$tooltip."', 250)\"; onMouseout=\"hideddrivetip()\">modified</div>"; } ?></td>
<?php } while ($row2 = mysql_fetch_assoc($sql2));
					$tooltip = unserialize($row2['Adjustment']);  # <-- !!!!!! this here !!!!!!!
					$rows = mysql_num_rows($sql2);
					if($rows > 0) {
						mysql_data_seek($sql, 0);
							$row2 = mysql_fetch_assoc($sql2);
					}
			?>
 
<?php } ?>
 
 
 
 
/* and just in case you need to know the Array....here it is. Thanks again for this Webvogel! */
 
<?php 
 
foreach($row as $key => $val){
        if ($row[$key] != $_POST[$map[$key]]){
                $change[] = $key.' field changed';
        }
}
 
$adjustment = serialize($change);
 
?>

Open in new window

0
Comment
Question by:BenthamLtd
  • 3
  • 2
6 Comments
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 24364635
You have a ; at the end of your while loop. Remove it.
0
 
LVL 34

Accepted Solution

by:
Beverley Portlock earned 250 total points
ID: 24364671
Just to explain - that semicolon forms an empty statement and the while loop executes that statement before moving to the next statement. The interpreter sees it as

while ($row2 = mysql_fetch_assoc($sql2))
     ;

$tooltip = unserialize($row2['Adjustment']);  # <-- !!!!!! this here !!!!!!!
$rows = mysql_num_rows($sql2);
if($rows > 0) {
              mysql_data_seek($sql, 0);
               $row2 = mysql_fetch_assoc($sql2);
}
 
0
 

Author Comment

by:BenthamLtd
ID: 24364758
Parse error: syntax error, unexpected T_VARIABLE, expecting ';' on line 1204


The while loop was working anyway. Any other ideas?
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 24364816
I am not clear whether you are trying to achieve this


while ($row2 = mysql_fetch_assoc($sql2))
     $tooltip = unserialize($row2['Adjustment']);  # <-- !!!!!! this here !!!!!!!


$rows = mysql_num_rows($sql2);

if($rows > 0) {
     mysql_data_seek($sql, 0);
     $row2 = mysql_fetch_assoc($sql2);
}


or this



while ($row2 = mysql_fetch_assoc($sql2)) {
     $tooltip = unserialize($row2['Adjustment']);  # <-- !!!!!! this here !!!!!!!


     $rows = mysql_num_rows($sql2);

     if($rows > 0) {
          mysql_data_seek($sql, 0);
          $row2 = mysql_fetch_assoc($sql2);
     }

}
0
 

Assisted Solution

by:switch7
switch7 earned 250 total points
ID: 24382904
What if you try:
<?php print_r( unserialize($row2['Adjustment']) ); ?>

?
0
 

Author Comment

by:BenthamLtd
ID: 24382930
Whoops sorry, I forgot to update this post. We've solved it now but thank you for all your help guys. Seems we didn't need to use serialize after all. Just implode.

Happy to give a throw a few points around for the useful comments.
<?php
 
// GET VALUES FROM DATABASE TABLE. STORE THEM INTO ARRAY. //
 
$query0 = "SELECT Status, ActioneeID, TypeID, Sales_Ledger_No, ProblemsClass, CompanyName, InvoiceDate, ContactName, AccountManager, Postcode, OrderConfirmedByID, Email, Phone, GoodsToBeReturned_1, GoodsToBeReturned_1_qty, GoodsToBeReturned_2, GoodsToBeReturned_2_qty, GoodsToBeReturned_3, GoodsToBeReturned_3_qty, GoodsToBeReturned_4, GoodsToBeReturned_4_qty, GoodsToBeReturned_5, GoodsToBeReturned_5_qty, GoodsToBeReturned_6, GoodsToBeReturned_6_qty, GoodsToBeReturned_7, GoodsToBeReturned_7_qty, GoodsToBeReturned_8, GoodsToBeReturned_8_qty, GoodsToBeReturned_9, GoodsToBeReturned_9_qty, GoodsToBeReturned_10, GoodsToBeReturned_10_qty, GoodsToBeReturned_11, GoodsToBeReturned_11_qty, GoodsToBeReturned_12, GoodsToBeReturned_12_qty from vedit_sheet WHERE Sheet_No='$sheet'";
$sql0 = mysql_query($query0) or die(mysql_error());
$row0 = mysql_fetch_assoc($sql0);
$rows0 = mysql_num_rows($sql0);
 
# FORMAT: a<form> = hidden form for array lookup (drop down boxes). f<form> = original form name from sheet (text fields).
$map = array('Status' => 'aStatus', 'Actionee' => 'aActionee', 'Type' => 'aType', 'Sales_Ledger_No' => 'fSalesRef', 'ProblemsClass' => 'aProblems', 'CompanyName' => 'fCompany', 'InvoiceDate' => 'fInvoice', 'ContactName' => 'fContact', 'AccountManager' => 'aAccountMan', 'Postcode' => 'fPostcode', 'OrderConfirmedByID' => 'fConfirmedBy', 'Email' => 'fEmail', 'Phone' => 'fPhone');  // map array: name of database field = name of form field
 
foreach($row0 as $key => $val){
        if ($row0[$key] != $_POST[$map[$key]]){
                $array[] = $key.' field changed from '.$val.' ' ;
       	    }
}
 
$comma_sep = implode("<br>",$array);
 
$update_sheet_history = "INSERT INTO sheet_history (Sheet_No, UserID, LatestModification, Adjustment) VALUES ('$sheet', '$_SESSION[userID]', now(), '$comma_sep') "; 

Open in new window

0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

792 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question