This option does not work. var_dump($max) returns NULL.

 if ($result = $this->mysqli->prepare("SELECT MAX(`id`) FROM `table1` WHERE `testrow1`=? OR `testrow2`=? OR `testrow3`=? OR `testrow4`=?")) { $result->bind_param("iiii", $usid, $usid, $usid, $usid); $result->execute(); $result->bind_result($max); $result->close(); } 
  • The request is theoretically correct, check it better on the database, with real data, it is possible that the request does not return anything - Gorets

4 answers 4

Perhaps the error is that the question marks should be enclosed in brackets.

 "SELECT MAX(`id`) FROM `table1` WHERE `testrow1`='?' OR `testrow2`='?' OR `testrow3`='?' OR `testrow4`='?'" 

Run this query directly in the database ...

    To get data from a prepared expression in mysqli, you need to use the fetch() method

    And, of course, the query, in principle, must find at least one line that falls under the WHERE clause.

    By the way, instead of mysqli I recommend using PDO. Code goes two times less

     $stmt = $this->pdo->prepare("SELECT MAX(`id`) FROM `table1` WHERE `testrow1`=? OR `testrow2`=? OR `testrow3`=? OR `testrow4`=?")); $stmt->execute([$usid, $usid, $usid, $usid]); $max = $stmt->fetchColumn(); 

      If they are in ascending order, then you can: mysqli_insert_id();

         SELECT id FROM table_name ORDER BY id DESC LIMIT 1;