Query a Array Field in MySQL [message #179714] |
Mon, 26 November 2012 17:04 |
heitorfaria
Messages: 3 Registered: November 2012
Karma: 0
|
Junior Member |
|
|
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
|
|
|
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: 0
|
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.
|
|
|
Re: Query a Array Field in MySQL [message #179716 is a reply to message #179714] |
Mon, 26 November 2012 20:10 |
Jerry Stuckle
Messages: 2598 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
On 11/26/2012 12:04 PM, 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
>
MySQL does not have arrays. If this is what you got back from a column
in MySQL, then the database is not properly normalized (one column
contains multiple values - fails 1st normal condition).
The data is output from the php serialize() function. You will need to
unserialize() it, which will create an array for you. Then you can
access the rincidente member of the array.
And for pete's sake - get a good database design before going any
further. The current one will cause you no end of problems! (For
further information on database design, try comp.databases.mysql).
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
|
|
|
Re: Query a Array Field in MySQL [message #179717 is a reply to message #179714] |
Tue, 27 November 2012 00:28 |
Denis McMahon
Messages: 634 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
On Mon, 26 Nov 2012 09:04:42 -0800, heitorfaria wrote:
> if ( $select == $id )
> {
> $ift_html .= "<option value='{$id}'
> selected='yes'>{$name}</option>";
> }
Aside from the issues discussed by others, you may also find it
worthwhile checking the permissible values for the selected attribute of
the option element in whatever W3C DTD relates to the the version of HTML
that you are presenting, as off the top of my head, and without looking
it up to check, I don't recall "yes" being valid for any of the flavours
of (x)html in common use at the current time, and I suspect the correct
format is either:
<option selected value="some value">some text</option>
in html, or:
<option selected="selected" value="some value">some text</option>
in xhtml.
Rgds
Denis McMahon
|
|
|
Re: Query a Array Field in MySQL [message #179718 is a reply to message #179714] |
Tue, 27 November 2012 00:28 |
Denis McMahon
Messages: 634 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
On Mon, 26 Nov 2012 09:04:42 -0800, heitorfaria wrote:
> if ( $select == $id )
> {
> $ift_html .= "<option value='{$id}'
> selected='yes'>{$name}</option>";
> }
Aside from the issues discussed by others, you may also find it
worthwhile checking the permissible values for the selected attribute of
the option element in whatever W3C DTD relates to the the version of HTML
that you are presenting, as off the top of my head, and without looking
it up to check, I don't recall "yes" being valid for any of the flavours
of (x)html in common use at the current time, and I suspect the correct
format is either:
<option selected value="some value">some text</option>
in html, or:
<option selected="selected" value="some value">some text</option>
in xhtml.
Rgds
Denis McMahon
|
|
|
|
Re: Query a Array Field in MySQL [message #179720 is a reply to message #179715] |
Tue, 27 November 2012 14:56 |
heitorfaria
Messages: 3 Registered: November 2012
Karma: 0
|
Junior Member |
|
|
> Array
>
> (
>
> [rincidente] => ri
>
> [codigoservico] => 00000
>
> [url] =>
>
> [localcpd] => bsa
>
> )
Scott,
Another dummie question (sorry, Im new at this).
Is there a way to do it like im planning? What am I doing wrong? =>
$cdfields = unserialize('cdfields');
$fields = array( $cdfiels[1] => 'bsa, spo, rjo?', $cdfiels[2] => 'bsa, spo, rjo?', ... );
|
|
|
Re: Query a Array Field in MySQL [message #179723 is a reply to message #179720] |
Wed, 28 November 2012 05:10 |
Scott Johnson
Messages: 196 Registered: January 2012
Karma: 0
|
Senior Member |
|
|
On 11/27/2012 6:56 AM, heitorfaria(at)gmail(dot)com wrote:
>> Array
>>
>> (
>>
>> [rincidente] => ri
>>
>> [codigoservico] => 00000
>>
>> [url] =>
>>
>> [localcpd] => bsa
>>
>> )
>
> Scott,
>
> Another dummie question (sorry, Im new at this).
> Is there a way to do it like im planning? What am I doing wrong? =>
>
> $cdfields = unserialize('cdfields');
> $fields = array( $cdfiels[1] => 'bsa, spo, rjo?', $cdfiels[2] => 'bsa, spo, rjo?', ... );
>
Well that all depends. There are several unanswered questions here.
First off the data you are plugging into the unserialize() function is
not a variable but a string.
But don't fret.
In order to help we need to know how you are getting the data out of the
Database and what the data looks like outside that DB. (From your
previous post that is what I remember you are doing)
Once this is known we can move forward.
If you are unsure what I am talking about, then go to the
comp.databases.mysql. Most people (including myself) also visit over
there and will be able to help you out getting what you need out of the DB.
GL.
Scotty
|
|
|