We help IT Professionals succeed at work.

Grab single value from mysql database with php

Russelauto
Russelauto asked
on
I'm using the following code to grab and store a value from a table in a mysql database.

  $result = mysql_query(" SELECT email FROM users WHERE username = '$username' ");
  $row = mysql_fetch_array($result);
  $email = $row["email"];
 
It works fine but my question is why do I have to fetch it from an array? Shouldn't it be a single non-array value? Shouldn't the value of $result be the email address?
Comment
Watch Question

the array refers to the fact that there are several different columns for each row.

for example,

$row =
"email": "person@example.com",
"name": "Joe",
"id": 234,

etc, etc.

Author

Commented:
So then why use SELECT email versus SELECT * ?

It seems like you might as well use the asterisk in all cases...
Fixer of Problems
Most Valuable Expert 2014
Commented:
If you echo '$result', you'll see that it is a 'Resource' pointer, not the data.  If you have just a few fields in your rows, you can use * all the time.  Some people have many fields in their rows and being specific reduces the amount of data that needs to be moved over  the network and speeds up the transaction.

Author

Commented:
Thank you for quick the responses.