|
|
|
| Web Hosting Guide |
![]() ![]() |
Extracting Mysql Maths Using Php |
Sep 6 2007, 09:55 PM
Post
#1
|
|
|
Newbie [ Level 2 ] Group: Members Posts: 24 Joined: 9-August 07 Member No.: 23,954 |
Right, this is a really simple thing and it has me completely stumped. I'm working on this mini maths function and for some reason i cannot seem to do some simple math process using mysql. This is the code: (php btw), now assume that $date is actually a defined mysql date variable already successfully extracted.
[codebox]$sql = mysql_query("SELECT TO_DAYS('CURDATE()') - TO_DAYS('$date')"); while ($row = mysql_fetch_array($sql)){ $diff = $row["TO_DAYS('CURDATE()') - TO_DAYS('$date')"]; } [/codebox] Can anyone spot what im doing wrong becuase im just thrown by it. |
|
|
|
Sep 7 2007, 07:12 AM
Post
#2
|
|
|
Premium Member Group: [HOSTED] Posts: 286 Joined: 17-June 07 Member No.: 22,702 |
Aha, I think I have the answer to that - you need to use a so-called "column alias".
The SQL ANSI standard allows you to do things like: SELECT table.column AS alias FROM ... Aliases are mostly optional, but not in your particular case. Here you absolutely do need an alias because you are referencing the result of a formula. Therefore your piece of SQL would look like: SELECT current_date_to_days - some_other_date_to_days AS diff_in_days after which you would need to use: $diff = $row["diff_in_days"]; in the subsequent PHP code. This post has been edited by dserban: Sep 7 2007, 07:18 AM |
|
|
|
Sep 7 2007, 07:22 AM
Post
#3
|
|
|
Super Member Group: [HOSTED] Posts: 876 Joined: 8-April 06 From: Lima - Peru Member No.: 12,579 myCENTs:13.21 |
QUOTE(Supa Comix @ Sep 6 2007, 04:55 PM) [snapback]110565[/snapback] [codebox]$sql = mysql_query("SELECT TO_DAYS('CURDATE()') - TO_DAYS('$date')"); while ($row = mysql_fetch_array($sql)){ $diff = $row["TO_DAYS('CURDATE()') - TO_DAYS('$date')"]; }[/codebox] I view that your problem is with your sql query, change your $sql variable to: CODE $sql = mysql_query("SELECT TO_DAYS(CURDATE())-TO_DAYS('$date')"); // or $sql1 = mysql_query("SELECT TO_DAYS(CURDATE())-TO_DAYS('$date') as diff"); But now, why do you want this and then loop through your recordset???, the result of your query will return only one row with one column, so, instead simply use the mysql_fetch_array directly without the while. CODE $row = mysql_fetch_array($sql); $diff = $row["SELECT TO_DAYS(CURDATE())-TO_DAYS('$date')"]; // or $row1 = mysql_fetch_array($sql1); $diff1 = $row["diff"]; Another way to get the difference between two dates is by using the DATEDIFF MySql Function. This function returns the number of days between start and end date, notice that the TO_DAYS function returns a daynumber (the number of days since year 0). So, i think that you can also use the following to get the same result: CODE $sql = mysql_query("SELECT DATEDIFF( CURDATE( ) , '$date') as diff"); Best regards, This post has been edited by TavoxPeru: Sep 17 2007, 05:35 PM |
|
|
|
![]() ![]() |
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:
Similar Topics
| Topic Title | Replies | Topic Starter | Views | Last Action | |||
|---|---|---|---|---|---|---|---|
![]() |
4 | HannahI | 147 | 1st March 2010 - 12:34 AM Last post by: magiccode9 |
|||
![]() |
2 | 8ennett | 152 | 25th February 2010 - 11:42 AM Last post by: 8ennett |
|||
![]() |
7 | Jonnyabc | 174 | 17th February 2010 - 08:31 AM Last post by: mastercomputers |
|||
![]() |
3 | clovis | 1,451 | 15th February 2010 - 12:17 PM Last post by: iG-zet |
|||
![]() |
12 | Houdini | 2,655 | 14th February 2010 - 09:44 AM Last post by: mastercomputers |
|||
![]() |
7 | kenjvalip | 2,725 | 13th February 2010 - 02:28 PM Last post by: magiccode9 |
|||
![]() |
10 | nakulgupta | 2,891 | 12th February 2010 - 06:25 AM Last post by: iG-Nancy Johnson |
|||
![]() |
4 | Manu Dhanda | 1,186 | 6th January 2010 - 12:39 PM Last post by: iG-medhatalbashaa |
|||
![]() |
10 | jedipi | 6,132 | 27th December 2009 - 11:44 AM Last post by: iG-marc |
|||
![]() |
19 | lonebyrd | 4,221 | 1st November 2009 - 03:49 PM Last post by: iGuest |
|||
![]() |
27 | nightfox | 7,345 | 14th October 2009 - 07:15 AM Last post by: iG-Mark |
|||
![]() |
9 | miCRoSCoPiC^eaRthLinG | 2,368 | 13th September 2009 - 05:45 AM Last post by: iG-Jatin Sharma |
|||
![]() |
3 | darkken | 2,855 | 11th September 2009 - 08:26 PM Last post by: iGuest |
|||
![]() |
6 | ruben | 1,754 | 27th August 2009 - 09:07 AM Last post by: iG-J vincent |
|||
![]() |
4 | miCRoSCoPiC^eaRthLinG | 7,077 | 21st August 2009 - 01:59 PM Last post by: iG-J vincent |
|||
|
Lo-Fi Version | Time is now: 16th March 2010 - 08:06 AM |
© 2010 AstaHost: Free Web Hosting & Technical Discussion, Free Web Hosting. a member of xisto.
Powered by Invision Board. Skin: IPB Forum Skins
Expand / Collapse Navigation



Sep 6 2007, 09:55 PM






