Jump to content

select "field name' AS question


phppup

Recommended Posts

Can anybody clarify the usefulness of "AS" and elaborate on how it operates.

 

If I select a 'bananas' AS 'yeelow fruit' will it change the TITLE in the table?  Or the table that is being viewed?

If I dump a list of 'FRUITS' into and HTML table, will using AS rename each column for me, or is that handled by MY coing of the HTML table?

Link to comment
https://forums.phpfreaks.com/topic/262880-select-field-name-as-question/
Share on other sites

If you SELECT a field from a table and use any MySQL functions while doing so, using an alias makes it easier to reference the field in an associative array when outputting the results.

 

if you were to do this:

$query = "SELECT DATE_FORMAT(date_field, '%M-%D-%Y') FROM table";
$result = mysql_query($query);

 

You would need to access that field in an associative array like this:

$array = mysql_fetch_assoc($result);
echo $array['DATE_FORMAT(date_field, '%M-%D-%Y')'];

 

With an alias, it becomes more straightforward:

$query = "SELECT DATE_FORMAT(date_field, '%M-%D-%Y') AS mydate FROM table";
$result = mysql_query($query);
$array = mysql_fetch_assoc($result);
echo $array['mydate'];

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.