Page 1 of 1

How to get a query after unserialize array?

Posted: Tue Nov 16, 2010 12:03 am
by celso
[text]I have a serialized code in db. At first I did the unserialize and after that I dont know how use the unserialized code to select other database. Anyone can help me?
[/text]

Code: Select all

mysql_connect('localhost','user','password') or die (mysql_error());
mysql_select_db('database') or die (mysql_error());

    $query = "SELECT * FROM cscart_order_details";
    $result = mysql_query($query) or die (mysql_error());
    $num = mysql_numrows($result);
    mysql_close($link); 

       
        for ($i = 0; $i < $num; $i++) {
        $extra = mysql_result($result,$i,"extra");
	$serializedData = $extra;
	$data = @unserialize($serializedData);

print_r($data);
echo '<br>';

        } 

[text]And shows me this unserialized lines, example: the number 97 is option_id and 141 is a variant_id. The variant_id represents a variant_name in other table. How can I get this numbers to put into other query?[/text]


Array ( [product_options] => Array ( [97] => 141 ) [is_edp] => [base_price] => 0 )
Array ( [product_options] => Array ( [96] => 140 ) [is_edp] => [base_price] => 0 )
Array ( [product_options] => Array ( [81] => 108 ) [is_edp] => [base_price] => 0 )
Array ( [product_options] => Array ( [81] => 108 ) [is_edp] => [base_price] => 0 )
Array ( [product_options] => Array ( [95] => 139 ) [is_edp] => [base_price] => 0 )

Re: How to get a query after unserialize array?

Posted: Tue Nov 16, 2010 12:25 am
by s992
I didn't understand your post very well, but it sounds like you want to pull data from more than one table based on a primary/foreign key. Rather than using PHP and querying twice, why not use INNER JOIN?

For example,
[syntax]SELECT cscart_order_details.option_id, cscart_order_details.variant_id, variant_table.variant_name
FROM cscart_order_details
INNER JOIN variant_table
ON cscart_order_details.variant_id = variant_table.id[/syntax]

I'm not 100% sure the syntax is correct, but it should get you started. Also see http://www.sqlbook.com/SQL/INNER-JOIN-37.aspx for a good explanation.

If I'm wrong about what you're trying to do, sorry to waste your time! :P