pull data from one field based on another fields value

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
Azteroth
Forum Newbie
Posts: 3
Joined: Sat May 24, 2008 12:37 am

pull data from one field based on another fields value

Post by Azteroth »

Ok I didn't know exactly what to do here I have everything pulled but how do I pull data from one field based on another fields value in the same row...

for example say I have a field thats name is

user_info_field_name

and it has values such as

first name
last name
age
hair color

Then I have another field that say called

user_info_field_value

John
Smith
39
brown

how can I based on what user_info_field_name value is pull data on the same row out of user_info_field_value

thats pretty much my question... thanks ahead for the help
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: pull data from one field based on another fields value

Post by califdon »

I'm afraid that you need to learn the fundamentals of a database before going any further. Your example makes no sense at all. A field in a table must contain one and only one value in each row, and the values in different rows must all be values of the same domain. That is, if the field is named "hair color" (a bad example, because field names should never contain spaces), then the values in each row might be "black", "brown", "blond", etc. I suspect you are thinking in terms of a spreadsheet and its cells. If you are, you are starting out on the wrong foot. I have taught both spreadsheets and databases for many years in college, and I know from experience that students who have worked with spreadsheets are the hardest to teach database principles to, because they tend to continue thinking in terms of "cells". That just doesn't work with databases!

Each table represents a collection of some "entity" (a person, a place, an object, a document, an object, an event, a transaction, etc.) and each field in a table represents some common "attribute" of an entity (a characteristic or distinguishing quality of the entity).

I recommend that you study up on very basic database tutorials, such as:
http://www.htmlgoodies.com/primers/data ... hp/3478051
http://www.geekgirls.com/databasics_01.htm
Azteroth
Forum Newbie
Posts: 3
Joined: Sat May 24, 2008 12:37 am

Re: pull data from one field based on another fields value

Post by Azteroth »

No I am working on a project where who ever did this database was an idiot... I got it going though with multiple mysql_query and mysql_fetch_assoc... but thank you for the assumption that I am a complete newbie and have absolutely no clue what I am doing.
matthijs
DevNet Master
Posts: 3360
Joined: Thu Oct 06, 2005 3:57 pm

Re: pull data from one field based on another fields value

Post by matthijs »

but thank you for the assumption that I am a complete newbie and have absolutely no clue what I am doing.
You have to understand that reading your original question does make it seem like you had to do some learning of the db basics first. Don't blame it on califdon, who was so kind to take the time to write an answer and provide some links.
Azteroth
Forum Newbie
Posts: 3
Joined: Sat May 24, 2008 12:37 am

Re: pull data from one field based on another fields value

Post by Azteroth »

I apologize for my rudeness I was/am still on a tight time frame for a clients project and I was not looking for something that was going to help me with mysql structure because I am trying to work off of what was already there for the client... Again I am sorry for my disrespect.

I do have a question that is based on data pulled from these databases though and was wondering if I could get some help with that...

I am getting the data back and have one of the strings that comes back has an & in it how can I make sure if that is there that it gets replaced so I don't get an error about there being a white space after it. I have tried to use a regex and str_replace but I with regex it does not go into an if statement even if the string is there and str_replace gives me an error.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: pull data from one field based on another fields value

Post by califdon »

Azteroth wrote:I apologize for my rudeness I was/am still on a tight time frame for a clients project and I was not looking for something that was going to help me with mysql structure because I am trying to work off of what was already there for the client... Again I am sorry for my disrespect.

I do have a question that is based on data pulled from these databases though and was wondering if I could get some help with that...

I am getting the data back and have one of the strings that comes back has an & in it how can I make sure if that is there that it gets replaced so I don't get an error about there being a white space after it. I have tried to use a regex and str_replace but I with regex it does not go into an if statement even if the string is there and str_replace gives me an error.
Show us the regex and str_replace statements you've tried. And is this data going to be used in HTML or for some other purpose?
Post Reply