Good afternoon, I bring a column with numbers out of mysql, how can I turn now to line 2 of this column? To make some mathematical operations, for example, to add the first row of a column to the second .. $ row ['exp_cnt'] [1] - the second character from each row is displayed to me .. (((

while ($row = mysql_fetch_array($query)) { print_r($row['exp_cnt']); } 
  • it is necessary to add directly to the SQL query, and immediately get the finished result - Ipatyev
  • Well, here you have the same cycle. on the second pass of the cycle you have the second entry. it means that on the first one it is necessary somewhere to remember the value, and on the second one it should be added together. Although, of course, Ipatyev is right, data should be collected directly in SQL in the form in which it is needed, but in php it is not recommended. it's slower and often not convenient - Mike

2 answers 2

As an option, make an array where to write the results. And then count in that cycle, or right in this cycle

Well, in general - it is better to perform such operations directly in the sql request, sql works faster than php.

 $res = array(); while ($row = mysql_fetch_array($query)) { $res[] = $row['exp_cnt']; } $res - будет хранить все результаты выборки 
  • Good afternoon, help with the request please. There is a mysql table: id | name | num 1 | q | 1000 2 | w | 800 3 | e | 600 4 | r | 700 The task is to make an operation on num with a select to eventually get a column with values. 1000-800 = 200 800-600 = 200 600-700 = -100 - Valery
 $i = 1; while ($row = mysql_fetch_array($query)) { if ($i > 2) { break; } if ($i == 2) { $second_row = $row['exp_cnt']; echo $second_row; } $i++; }