0

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;
kovpas
  • 9,553
  • 6
  • 40
  • 44
  • what is the problem? show your current output and your desired ouptut. One thing here at least is that you put the same information in both vars, and overwrite them for all results – Nanne Feb 01 '13 at 13:50
  • I can't properly understand the question, but wouldn't the use of an array be also good? I think the OP wants to put 1st date in $date1 and the second into $date2. So I think it would be better to use an array, and $dates[0] and [1]... But I'm not sure, if that is what the OP wants. – ppeterka Feb 01 '13 at 13:50
  • Use an array to store the result, see http://stackoverflow.com/questions/6741425/populate-php-array-from-while-loop – Jochen Feb 01 '13 at 13:51
  • I want that each row,the date will put into variable every time it will loop – Bill Hunter Feb 01 '13 at 13:52
  • in the same variable? why would you want that? – Nanne Feb 01 '13 at 13:54
  • [**Please, don't use `mysql_*` functions in new code**](http://bit.ly/phpmsql). They are no longer maintained [and are officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the [**red box**](http://j.mp/Te9zIL)? Learn about [*prepared statements*](http://j.mp/T9hLWi) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://j.mp/QEx8IB) will help you decide which. If you choose PDO, [here is a good tutorial](http://j.mp/PoWehJ). – tereško Feb 01 '13 at 13:54

4 Answers4

0

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];
Antony
  • 14,900
  • 10
  • 46
  • 74
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.

Vlad Preda
  • 9,780
  • 7
  • 36
  • 63
0
while($row=mysql_fetch_array($sql)){
     $results[] = $row['TheDate1']
}
echo $results[0];
echo $results[1];
Paul van Schayck
  • 517
  • 7
  • 10
0

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.

Zoe
  • 27,060
  • 21
  • 118
  • 148
PeeHaa
  • 71,436
  • 58
  • 190
  • 262