Deserializing a specific column
UPDATE: I removed some print_r & echo tests I had above the loop and I was able to view the data.
Am fetching everything from a table with the following query.
$result = mysql_query("SELECT * FROM campaign_manager ORDER BY 'date'") or die(mysql_error());
I have a loop that displays everything in a table.
while($row = mysql_fetch_array( $result )) {
// echo 开发者_如何学Cout the contents of each row into a table
echo "<tr>";
echo '<td>' . $row['campaign_id'] . '</td>';
echo '<td>' . $row['paragraph_id'] . '</td>';
echo '<td>' . $row['que_id'] . '</td>';
echo "</tr>";
}
So far pretty straight forward so far... Here's where it gets tricky.
The $row['paragraph_id'] is serialized. I ran a test to get the data to appear how I need it to as follows;
$do = mysql_fetch_array($result);
print_r(implode(", ",unserialize($do['paragraph_id'])));
Which prints out the data exactly how I need it to. But how do I implement that into my while loop? I've tried several variations with all being failure. I've tried saving into variables outside and inside the loop. I've also tried deserializing in each row iteration. Nothing seems to work and the rows will not show up in the table.
How can I get the data from that table to display using that while loop while deserializing that one column?
You should use print_r with option true like
print_r(implode(", ",unserialize($do['paragraph_id'])), true);
or simple
echo '<td>' . implode(", ",unserialize($do['paragraph_id'])) . '</td>';
while($row = mysql_fetch_array( $result )) {
// echo out the contents of each row into a table
echo "<tr>";
echo '<td>' . $row['campaign_id'] . '</td>';
echo '<td>' .
print_r(implode(", ",unserialize($row['paragraph_id'])), true)
. '</td>';
echo '<td>' . $row['que_id'] . '</td>';
echo "</tr>";
}
精彩评论