0

I've tried many different ways of formatting this and none of them have worked.

Mysql history shows this is what it tries to execute: SELECT * FROM operations WHERE id= ? ORDER BY exectime

Heres the script:

<?php
$sql = "SELECT * FROM operations WHERE id = ? ORDER BY exectime";
if($stmt = mysqli_prepare($link, $sql)){
  $stmt -> bind_param("i", $_SESSION["id"]);
  if($result = $stmt->execute()){
      if(mysqli_stmt_fetch($stmt) > 0) {
        while($row = mysqli_stmt_fetch($stmt)) {
          echo $row[0];
        }
      } else {
        echo "Empty";
      }
  } else{
      echo "Oops! Something went wrong. Please try again later.";
  }
  mysqli_stmt_close($stmt);
}
?>

The even weirder thing is I'm using bind_param on the login pages and works completely fine.

It doesn't even echo "Oops! Something went wrong. Please try again later.";

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • Most likely there is another error. Please stop checking manually for errors. Remove all these `if` statements checks and enable automatic error reporting. [How to get the error message in MySQLi?](https://stackoverflow.com/a/22662582/1839439) – Dharman Apr 04 '21 at 22:57
  • Yea its reporting a whole lotta nothin. –  Apr 04 '21 at 23:02
  • Yep, i got all the error reporting. Gives me nothing. –  Apr 04 '21 at 23:08
  • Oh, i have I've been trying to get this to work for 2 hours. It all executes up to here "if(mysqli_stmt_fetch($stmt) > 0) {" and i think thats just not working because its properly executing the query. Correction it does run that and it comes back true even tho it shouldnt. Im losing my mind here. –  Apr 04 '21 at 23:09
  • Ok ive went over a couple more times. And assuming the query runs fine. It stops here: ```while($row = mysqli_stmt_fetch($stmt)) {``` –  Apr 04 '21 at 23:22
  • That makes sense. It's not a valid while loop. Did you want to fetch a single row or multiple rows? – Dharman Apr 04 '21 at 23:24
  • Multiple rows.. –  Apr 04 '21 at 23:28

1 Answers1

0

You overcomplicated the whole thing. It's difficult to find exactly what went wrong because the code is too messy. Your while loop and the preceding if statement are definitely incorrect.

Just keep it simple. Enable mysqli error reporting and then use OOP style and get_result() method.

$stmt = $link->prepare("SELECT * FROM operations WHERE id = ? ORDER BY exectime");
$stmt->bind_param("i", $_SESSION["id"]);
$stmt->execute();
$result = $stmt->get_result();
$data = $result->fetch_all(MYSQLI_NUM);

if ($data) {
    foreach ($data as $row) {
        echo $row[0];
    }
} else {
    echo "Empty";
}
Dharman
  • 30,962
  • 25
  • 85
  • 135