0

OK so I have a table like this

Col1      Col2

10        30
20        40
50        60

I am querying the data like so

Query = ("SELECT * FROM TableName");

while ( $row= Mysql_fetch_array($query)) {

  // code will go here 
 Code needs to get the prev row and the current row for every row in the table like:

 This row['col1'] - prev row['col1'] = $wahatever

  echo $whatever



}

I dont know whow to reference the prev row in php so as it looks through the while statment I need to say col1 - prev col1 and col2 - prev col2

Can Anyonw tell me how to do this. Its actually for working out complex mapping distcances which I can do, just cant work out how to call the prev rows data in the loop.

Any help would be greatfully apprceiated

Thanks for all your help but I dont think I asled the question properly.

What I am looking for is for a way to get each row and then minus col1 from col1 on the prev row and run through a loop untill the end.

4

5 回答 5

2

You can try this completly in SQL solution

SELECT col1, col2, 
       col1 - @prev1, 
       col2 - @prev2,
       @prev1 := col1, @prev2 := col2
FROM TableName, (select @prev1 := 0, @prev2 := 0) r

SQLFiddle demo

于 2013-10-17T14:22:08.457 回答
2
 $prevRow = null;
 while ( $row= Mysql_fetch_array($query)) {
      if($prevRow != null){
           //comparison here
      }
      $prevRow = $row;
 }
于 2013-10-17T14:23:47.877 回答
1

Or, you could store your values first in an array like

$myArr = array(); $i=0;
while ( $row= Mysql_fetch_array($query)) {

  // code will go here 
   $myArr[$i] = $row['col1']; 
   $myArr[$i] = $row['col2'];

   $i++;
}

Then you would know that $i-1 is the previous row from db.

foreach($myArr as $i => $row) {
   if( isset($myArr[$i-1]) {
       $prevRow = $row;
   }
   // blah blah blah
}
于 2013-10-17T14:25:51.533 回答
1

You can use mysql_data_seek for changing pointer to current row and then fetch it, but I recommend to store previous value in each step

于 2013-10-17T14:26:02.307 回答
0

if you will compare 2 different rows most probably they will not be equal if they have id which is primary and auto incrementing but if you will just compare the specific data's in the row best way is to store them or you can query all column but not including the id

query the id-1 of the current query just make sure to make the id primary and auto increment for example

<?php
$que = "SELECT * FROM table ...'";
res = mysql_query($que, $con);
if(mysql_num_rows($res)>0){
while($row = mysql_fetch_array($res)){ 
$counter=1;//apply this so it will not compare the first row in the data to the previous 
//one because there is nothing to compare
if($counter > 1){//determine if it is the 2nd row therfore do the comparison
$currentrow=$row['id']; //this is the id of the current row which is row 2
$previousrowid=$row['id']-1; //this is the id of the row 1 which you will need to query 
//the datas on it to compare it 

 $que2 = mysql_query("SELECT value FROM table where id= '$previousrowid' ");//query of the previous row
 $r  = mysql_fetch_array($que2);
 $previousdata=$r['data'];
 $currentdata=$row['data'];
 $counter++;
 if($previousdata != $currentdata)
 {
 echo "They Are Not Equal";
 }else{
 echo "They Are Equal!";
 }
 }//ending of the counter


}}//ending of loop
?>

hopes this help

于 2013-10-17T14:25:10.657 回答