M
M
Mikhail Smirnov2021-02-07 13:04:10
PHP
Mikhail Smirnov, 2021-02-07 13:04:10

Doesn't output last row from sql table?

There is a stat table with delivery and cost columns (default = 0), I get values ​​from it row by row with a condition (if delivery = 'Mail' and cost=0), after output I make changes to the cost column, and the next time I access the table, it displays the next line that matches the condition.

//Получаю данные заказа для обработки почты
function getOne(){
    $conn = connect();
    $result = mysqli_query($conn,"SELECT * FROM `stat`  WHERE `delivery`='Почта' AND `cost`='0'");
    if (mysqli_num_rows($result)>0){
        $row= mysqli_fetch_row($result);
        $id= $row[0];
        $result2 = mysqli_query($conn,"SELECT * FROM `order`  WHERE `id`='$id'");
        $row = mysqli_fetch_assoc($result) + mysqli_fetch_assoc($result2);
        echo json_encode($row);
    }
    else {
        echo "Ошибка в getOne";
    }
    mysqli_close($conn);
}

The question is the following, everything works, but does not display any last line that meets the condition

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Slava Rozhnev, 2021-02-07
@rozhnev

You should use one query with join:

<?php
function getOne($conn) {
    $result = mysqli_query($conn,"
    SELECT * 
    FROM `stat` 
    JOIN `order` ON `order`.`id` = `stat`.`order_id` 
    WHERE `delivery`='Почта' AND `cost`='0'"
  );
  
    if (mysqli_num_rows($result)>0) {
      while ($row = mysqli_fetch_assoc($result)) {
          echo json_encode($row);
      }
    }
    else {
        echo "Ошибка в getOne";
    }
    mysqli_close($conn);
}

share php code

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question