There are two rows given in mysql_query and I want each row put in a variables $date1 and $date2. My sample code is below.
while($row=mysql_fetch_array($sql)) {
$date1 = $row['TheDate'];
$date2 = $row['TheDate'];
}
echo $date1;
echo $date2;
There are two rows given in mysql_query and I want each row put in a variables $date1 and $date2. My sample code is below.
while($row=mysql_fetch_array($sql)) {
$date1 = $row['TheDate'];
$date2 = $row['TheDate'];
}
echo $date1;
echo $date2;
While I have no idea why you want to have $date1
and $date2
instead of $date
as an array of two elements, here's a way to do it:
$i = 1;
while($row=mysql_fetch_assoc($sql)){
${"date".$i} = $row['TheDate'];
$i++;
}
echo $date1;
echo $date2;
A proper way would be to assign them into an array and access the data like this:
while($row=mysql_fetch_assoc($sql)){
$date[] = $row['TheDate'];
}
echo $date[0];
echo $date[1];
To compare the two dates, you can do the followings:
$difference = $date2- $date1;
or in the array approach:
$difference = $date[1] - $date[0];
I think this is what you need.
$dates = array();
$allData = array();
while($row=mysql_fetch_array($sql)){ // switch to mysqli or PDO
$dates[] = $row['TheDate']; // $dates will have all the dates
$allData[] = $row; // $allData will have all the data in the query
}
print_r($dates);
PS:
Please consider switching your code to MySQLi or PDO - MySQL is deprecated since PHP 5.5, and no longer maintained.
while($row=mysql_fetch_array($sql)){
$results[] = $row['TheDate1']
}
echo $results[0];
echo $results[1];
What you want to do is create an array:
$dates = array();
while($row=mysql_fetch_array($sql)) {
$dates[] = $row['TheDate'];
}
var_dump($dates);
// Now if you *really* insist in having it in separate variables you could do:
list($date1, $date2) = $dates;
Also:
Please, don't use mysql_*
functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO, or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.