Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

accessing info from MySQL 1

Status
Not open for further replies.

kpetursson

Programmer
Jan 28, 2002
313
US
is there a way to retrieve a single peice of data from a record in a MySQL table if you know the column title but do not know its position in the table?

example
I want the phone number from col home_phone in table members but I did not create the table, and the table structure is not yet set in stone. so home_phone could end up being the 3 of 10 column in the table.

I know I could simply wait until the table structure is set, but this will slow development. for testing pruposes I have created the above mentioned table with the few columns I need at this stage in the PHP development, but the final structure of the table will likely look different then my little temporary test table.

Any help or suggestions is greatly appriciated.

Kevin Petursson
--
"Everyone says quotable things everyday, but their not famous... so nobody cares."... Some Person
 
If you use the PHP function mysql_fetch_assoc() or mysql_fetch_array(), the data from the recordset will be available in the form of an associative array. It won't matter in what position the column "home_phone" is in, as you can simply reference it by $resultset_row['home_phone'].




Want the best answers? Ask the best questions! TANSTAAFL!
 
You can issue a [blue]mysql_fetch_assoc()[/blue] on the result set variable, and then acces the column by name:

Code:
$qry="SELECT *FROM mytable";
$res=mysql_query($sql);

while($row=mysql_fetch_assoc($res)){

echo $row['mycolumn_name'];

}

[green]this will echo the contents of the column mycolumn_name[/green]

For moe info on mysql_fetch_assoc go to the PHP onlne manual:



----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Thank you sleipnir214

That works perfectly. seeing it like that make so much sense.

This will save me a lot of headaches.

Thanks.

Kevin Petursson
--
"Everyone says quotable things everyday, but their not famous... so nobody cares."... Some Person
 
I have two good reasons for only using associative array references to fetch data from a MySQL (or PostgreSQL or MSSQL) resultset.

One is that I learned the hard way that other programmers can make changes to a table, adding columns. MySQL, for one, allows users to add a column anywhere in a table, just just at the end, and a fellow programmer broke some of my code adding a column.

The other is that $result_set['home_phone'] is much a more helpful reference than $result_set[3] when I review the code after a 6-month hiatus.



Want the best answers? Ask the best questions! TANSTAAFL!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top