Re: Query a Array Field in MySQL [message #179715 is a reply to message #179714] |
Mon, 26 November 2012 20:00 |
Scott Johnson
Messages: 196 Registered: January 2012
Karma:
|
Senior Member |
|
|
On 11/26/2012 9:04 AM, heitorfaria(at)gmail(dot)com wrote:
> Hello folks,
>
> I have the following function:
> #====================================================================
> #=======================================
> # @ Build Search Drop
> # Builds a search field drop-down list.
> #=======================================
>
> function build_search_drop($select='')
> {
>
>
> $fields = array( 'id' => 'Ticket ID', 'subject' => 'Subject', 'message' => 'Message', 'mname' => 'Submitted By', 'email' => 'Email' );
>
>
> $ift_html = "<select name='field' id='field'>";
>
> foreach( $fields as $id => $name )
> {
> if ( $select == $id )
> {
> $ift_html .= "<option value='{$id}' selected='yes'>{$name}</option>";
> }
> else
> {
> $ift_html .= "<option value='{$id}'>{$name}</option>";
> }
> }
>
> $ift_html .= "</select>";
>
> return $ift_html;
> }
>
> }
>
> ?>
> #====================================================================
>
> What in need: in $fields=array(...), I want to be able to query another (more complex) database field witch contains a MySql Array (I guess). It has this kind of information:
> #====================================================================
> a:4:{s:10:"rincidente";s:2:"ri";s:13:"codigoservico";s:5:"00000";s:3: "url";b:0;s:8:"localcpd";s:3:"bsa";}
> #====================================================================
>
> So I want to add a query that can return the value of "rincidente" or "codigoservico", for exemple. Is that possible? How I do that?
>
> Regards,
>
> Heitor Faria
>
If the
'a:4:{s:10:"rincidente";s:2:"ri";s:13:"codigoservico";s:5:"00000";s:3: "url";b:0;s:8:"localcpd";s:3:"bsa";}'
is what you are querying from the DB, that is a 'serialized' value.
Once you have pulled that value out of the DB just unserialize it:
$data_ary = unserialize($serialized_data);
This will create an Associative array of that data.
You can then plug the $data_ary in place of the $fields var.
This will not account for error checking or html code in the data that
could muck up your page.
if you are in control of the data in, then the data out can be controlled.
If you are getting this data from a third party, make sure you have some
good data checking methods in place.
You can verify the array structure returned by using print_r(); or
var_export();
The data you gave for example is:
Array
(
[rincidente] => ri
[codigoservico] => 00000
[url] =>
[localcpd] => bsa
)
If you are unsure of how to query the DB then you may want to hope over
to c.d.mysql group.
|
|
|