Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #7645 > unrolled thread

Query a Array Field in MySQL

Started byheitorfaria@gmail.com
First post2012-11-26 09:04 -0800
Last post2012-11-27 00:28 +0000
Articles 8 — 4 participants

Back to article view | Back to comp.lang.php


Contents

  Query a Array Field in MySQL heitorfaria@gmail.com - 2012-11-26 09:04 -0800
    Re: Query a Array Field in MySQL Scott Johnson <noonehome@chalupasworld.com> - 2012-11-26 12:00 -0800
      Re: Query a Array Field in MySQL heitorfaria@gmail.com - 2012-11-27 06:40 -0800
      Re: Query a Array Field in MySQL heitorfaria@gmail.com - 2012-11-27 06:56 -0800
        Re: Query a Array Field in MySQL Scott Johnson <noonehome@chalupasworld.com> - 2012-11-27 21:10 -0800
    Re: Query a Array Field in MySQL Jerry Stuckle <jstucklex@attglobal.net> - 2012-11-26 15:10 -0500
    Re: Query a Array Field in MySQL Denis McMahon <denismfmcmahon@gmail.com> - 2012-11-27 00:28 +0000
    Re: Query a Array Field in MySQL Denis McMahon <denismfmcmahon@gmail.com> - 2012-11-27 00:28 +0000

#7645 — Query a Array Field in MySQL

Fromheitorfaria@gmail.com
Date2012-11-26 09:04 -0800
SubjectQuery a Array Field in MySQL
Message-ID<54d3cfad-b3c7-4ae2-b375-8897168810df@googlegroups.com>
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

[toc] | [next] | [standalone]


#7646

FromScott Johnson <noonehome@chalupasworld.com>
Date2012-11-26 12:00 -0800
Message-ID<k90hpr$1h2$1@dont-email.me>
In reply to#7645
On 11/26/2012 9:04 AM, heitorfaria@gmail.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.


[toc] | [prev] | [next] | [standalone]


#7650

Fromheitorfaria@gmail.com
Date2012-11-27 06:40 -0800
Message-ID<60fe6092-c69a-466c-97b1-867347350bf1@googlegroups.com>
In reply to#7646
Thank you very much. I love you! =)

[toc] | [prev] | [next] | [standalone]


#7651

Fromheitorfaria@gmail.com
Date2012-11-27 06:56 -0800
Message-ID<6e18affc-288d-4d0e-ae77-a61334f0d7a8@googlegroups.com>
In reply to#7646
> 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?', ... );  

[toc] | [prev] | [next] | [standalone]


#7654

FromScott Johnson <noonehome@chalupasworld.com>
Date2012-11-27 21:10 -0800
Message-ID<k946c3$esf$1@dont-email.me>
In reply to#7651
On 11/27/2012 6:56 AM, heitorfaria@gmail.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

[toc] | [prev] | [next] | [standalone]


#7647

FromJerry Stuckle <jstucklex@attglobal.net>
Date2012-11-26 15:10 -0500
Message-ID<k90iat$56d$1@dont-email.me>
In reply to#7645
On 11/26/2012 12:04 PM, heitorfaria@gmail.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@attglobal.net
==================

[toc] | [prev] | [next] | [standalone]


#7648

FromDenis McMahon <denismfmcmahon@gmail.com>
Date2012-11-27 00:28 +0000
Message-ID<k911g3$h4v$1@dont-email.me>
In reply to#7645
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

[toc] | [prev] | [next] | [standalone]


#7649

FromDenis McMahon <denismfmcmahon@gmail.com>
Date2012-11-27 00:28 +0000
Message-ID<k911f5$h51$2@dont-email.me>
In reply to#7645
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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.php


csiph-web