Reno04 Posted May 24, 2009 Share Posted May 24, 2009 Hi I'm new to using Mysql and php together and was wondering if someone could explain how i would display a derived column result. As in year - death_age. see below $query = "select name from prime_minister where YEAR(CURRENT_DATE()) - birth_yr "; $result = mysql_query($query); if (mysql_affected_rows() >=1) { echo '<h3>'.'The results of'.'<p>'.'<code>'. $query . '</code>' .'</p><p>are as follows:'.'</p>'.'</h3>'; } else { echo 'Check your SQL statement as there has been no data retrieved!'; } while($row = mysql_fetch_array($result, MYSQL_ASSOC)) { echo "<br /> Name : {$row['name']} <br>". echo "<br /> Current Age : {$row['??????']} <br>"; This part i dont understand what to write } mysql_close(); // Quote Link to comment https://forums.phpfreaks.com/topic/159421-displaying-derived-results/ Share on other sites More sharing options...
fantomel Posted May 24, 2009 Share Posted May 24, 2009 please use php /php between [] Quote Link to comment https://forums.phpfreaks.com/topic/159421-displaying-derived-results/#findComment-840955 Share on other sites More sharing options...
RussellReal Posted May 24, 2009 Share Posted May 24, 2009 you are only selecting 'name' try this: SELECT name, (EXTRACT(YEAR FROM NOW()) - EXTRACT(YEAR FROM birth_yr)) As age FROM prime_minister Quote Link to comment https://forums.phpfreaks.com/topic/159421-displaying-derived-results/#findComment-840959 Share on other sites More sharing options...
Reno04 Posted May 25, 2009 Author Share Posted May 25, 2009 Sorry my mistake the query was typed wrong it it should've been Select name, Year(current_date())-birth_yr As age from prime_minister the real problem i have is with php displaying the "as age" column as its derived and i don't know how to refer to it in my while loop see below while($row = mysql_fetch_array($result, MYSQL_ASSOC)) { echo "<br /> Name : {$row['name']} <br>". echo "<br /> Age : {$row['??????']} <br>"; // This part i dont understand what to write } mysql_close(); Quote Link to comment https://forums.phpfreaks.com/topic/159421-displaying-derived-results/#findComment-841560 Share on other sites More sharing options...
BK87 Posted May 25, 2009 Share Posted May 25, 2009 type the word age $row['age'] Quote Link to comment https://forums.phpfreaks.com/topic/159421-displaying-derived-results/#findComment-841561 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.