Select information from a column

Alles, was PHP betrifft, kann hier besprochen werden.

Select information from a column

Postby multipianista » 09. February 2014 16:49

Hi, I would like to know how can I separate each "String" form a column. For example, in my column "Colors" I have write: "Blue,Red,Green"
So, if I make a consult from the column "Colors", it will return me the whole information "Blue,Red,Gree,". How can I consult just for the blue, the red or the green for separate?

Thank you.
multipianista
 
Posts: 10
Joined: 24. January 2014 23:51
XAMPP Version: 5.5.19
Operating System: Windows 8

Re: Select information from a column

Postby JJ_Tagy » 09. February 2014 17:15

If they are definitely separated by commas, you can do a split function or a substr function.
JJ_Tagy
 
Posts: 544
Joined: 30. January 2012 13:44
XAMPP Version: 1.8.3
Operating System: Windows 7 x64

Re: Select information from a column

Postby Altrea » 09. February 2014 17:25

Hi,

explode() is the answer.

best wishes,
Altrea
We don't provide any support via personal channels like PM, email, Skype, TeamViewer!

It's like porn for programmers 8)
User avatar
Altrea
AF Moderator
 
Posts: 6685
Joined: 17. August 2009 13:05
XAMPP Version: 5.5.19
Operating System: W7Ux64

Re: Select information from a column

Postby multipianista » 09. February 2014 20:53

But it doesn't work for this code:

$query = $db->query("SELECT * FROM datos WHERE informacion LIKE '$queryString%' LIMIT 5");
if($query) {
while ($result = $query ->fetch_row()) {
echo '<li onClick="fill(\''.$result->informacion.'\');">'.$result->informacion.'</li>';}


Here it prints the column "informacion" but I just want it to select a part of that column. I mean, I want to search in each string (separate by ",") of the all "informacion" columns.
multipianista
 
Posts: 10
Joined: 24. January 2014 23:51
XAMPP Version: 5.5.19
Operating System: Windows 8

Re: Select information from a column

Postby Nobbie » 09. February 2014 21:31

multipianista wrote:Here it prints the column "informacion" but I just want it to select a part of that column. I mean, I want to search in each string (separate by ",") of the all "informacion" columns.


Therefore you cannot simply output $result->informacion directly, you have to split it into several chunks by using explode() or any suited function.

Anyway: we cannot teach you programming PHP, this forum is meant for technical problems. You should go for a good PHP programming book (there are really many out there) and learn programming step by step. As we all did. This forum cannot replace a good book and (of course) it will take a certain amount of time (months at least) to become a skilled PHP programmer.
Nobbie
 
Posts: 6775
Joined: 09. March 2008 13:04

Re: Select information from a column

Postby itnasasta » 14. February 2015 06:52

Here it prints the column "informacion" but I just want it to select a part of that column. I mean, I want to search in each string (separate by ",") of the all "informacion" columns.???
GuL
itnasasta
 
Posts: 1
Joined: 14. February 2015 06:19
XAMPP Version: 1.8.3-2
Operating System: windows

Re: Select information from a column

Postby Nobbie » 15. February 2015 21:04

itnasasta wrote:Here it prints the column "informacion" but I just want it to select a part of that column. I mean, I want to search in each string (separate by ",") of the all "informacion" columns.???


This is a pain and it definately means, that you have choosen the WRONG data model for your application. It is simply wrong, having comma separated values in your columns, instead of having a 1:n (or even n:n) relationship between some entities. I know that you dont like this answer and it does not make you happy, but its better to have a sudden death instead of a pain without end. I am sorry for the bad explanation, in german the idiom says "besser ein Ende mit Schrecken, als ein Schrecken ohne Ende". Maybe a translator can translate this into an appriopriate engllish idiom.

Anyway, you have to change your data design and your application, otherwise you will not ever have a good performing application.
Nobbie
 
Posts: 6775
Joined: 09. March 2008 13:04


Return to PHP

Who is online

Users browsing this forum: No registered users and 3 guests