Go4Expert

Go4Expert (http://www.go4expert.com/)
-   PHP (http://www.go4expert.com/forums/php/)
-   -   retrive defrent inserted data from mysql & compaer between it to show the graph (http://www.go4expert.com/forums/retrive-defrent-inserted-data-mysql-t28509/)

abosirage 3Jun2012 14:45

retrive defrent inserted data from mysql & compaer between it to show the graph
 
hi every body
I need your help friends
I am inserting about 40 item in my database four time weekly , I want to get today insert date & the last inserted data in my php page to compare between them & show the graph
I use select twice to get current and previous but the result first and last,
my code is like
Code:

<?php
  include ('config.php');
  $select = mysql_query("SELECT DISTINCT ...... FROM .... ORDER BY `id` desc") ;
while($row = mysql_fetch_array($select))
{
    $date[] = $row['.....'];   
 }
$ert= $date[1];
function isUnique($ert){
 
    return (array_unique($array) != $array);
 }
 echo $ert;
$result = mysql_query("SELECT * FROM ...... WHERE ....='.......' or ......='......' or ......='..........' or ORDER BY `id` desc LIMIT 4");
?> 
    <?php
  include ('config.php');
$result1 = mysql_query("SELECT * FROM ......... WHERE ......... ='......... ' or ......... ='......... ' or ......... ='......... ' or ORDER BY `id` asc LIMIT 4");

?>


pein87 11Jun2012 03:34

Re: retrive defrent inserted data from mysql & compaer between it to show the graph
 
Your query already selects none duplicates because your using the DISTINCT keyword. You will not get duplicates using that keyword nor would you if you used UNIQUE. you could just get the last item and the date of today and use php to check the date difference.

get last item using id column

Code: SQL

SELECT date FROM table_name ORDER BY id DESC LIMIT 1


or

Code: SQL

SELECT DAY(date) AS lDay, MONTH(date) AS lMonth, YEAR(date) AS lYEAR FROM table_name ORDER BY id DESC LIMIT 1;


the last method requires a proper date data type like DATETIME or TIMESTAMP and does not work on any VARCHAR types.

Your array would then have three values year, month, and day which you can use php's built in date function to get these for the current day and subtract the values to see the time elapsed.

PHP Code:

<?php
include("config.php");

$Q mysql_query("SELECT DAY(date) AS lDay, MONTH(date) AS lMonth, YEAR(date) AS lYEAR FROM table_name ORDER BY id DESC LIMIT 1");

$tDate = array(date("m"),date("d"),date("Y"));

$R mysql_fetch_array($Q);

if(
$R['lDay'] == $tDate[1] && $R['lMonth'] == $tDate[0] && $R['lYear'] == $tDate[2])
{

// some code here that says everything was done today

}
else
{

// subtract the values using $tDate as the big number ie $tDate[0] - $R['Month']

}

?>

That isn't the best example of course but you could compare which one is greater only if the years are different and calculate how many days, months, and years have passed since the last insert.

if you

PHP Code:

print_f($tDate); 

it shows

Code:

Array ( [0] => 06 [1] => 10 [2] => 2012 )
you can also get todays date using this SQL

Code: SQL

SELECT MONTH(NOW()) AS thisMonth, DAY(NOW()) AS thisDay, YEAR(NOW()) AS thisYEAR


just giving you options to get todays date with code and compare your last entries date.


All times are GMT +5.5. The time now is 18:35.