0
<?php
require_once  'login.php';

$conn = new mysqli($hn, $un, $pw, $db); //these three lines connect to the db with the login file
if($conn->connect_error) die($conn->connect_error); //all the lines above here will be in all your codes

$query = "SELECT * FROM orders"; //the query
$result = $conn->query($query); //run the query and get the result
if(!$result) die($conn->error);

$rows = $result->num_rows;
$query = "SELECT max(orderid) from orders";
$result = mysqli_query($conn, $query);

while($row = mysqli_fetch_assoc($result)){
    echo "{$row(orderid)'}";
}
?>

I keep getting an error

( ! ) Notice: Undefined index: MAX(orderid) in C:\wamp64\www\groupproject\test1.php on line 23

Whats causing this?

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Why don't you fetch the results from the first query? – Barmar Nov 30 '20 at 03:41
  • I don't think you could be getting that error from the code you posted. – Barmar Nov 30 '20 at 03:41
  • The second query just returns 1 row, why are you using a `while` loop to fetch the results? – Barmar Nov 30 '20 at 03:41
  • That error would come from a line that contains `$row['MAX(orderid)']`, which doesn't appear anywhere in your code. – Barmar Nov 30 '20 at 03:42
  • I think you made a mistake in your echo statement. You used $row() instead of $row[]. Also, what is that single quote for? – CharlesEF Nov 30 '20 at 03:53
  • It is a very bad idea to use `die(mysqli_error($$conn));` in your code, because it could potentially leak sensitive information. See this post for more explanation: [mysqli or die, does it have to die?](https://stackoverflow.com/a/15320411/1839439) – Dharman Nov 30 '20 at 10:44
  • If you are only starting to learn PHP then you should learn PDO instead of mysqli. PDO is much easier and more suitable for beginners. Start here https://phpdelusions.net/pdo – Dharman Nov 30 '20 at 10:45

1 Answers1

0

There's no need to execute two queries, you can get both results in a single query.

$query = "SELECT COUNT(*) as rows, MAX(orderid) AS last FROM orders";
$result = $conn->query($query);
$row = $result->fetch_assoc();
$rows = $row['rows'];
$last_orderid = $row['last'];
Barmar
  • 741,623
  • 53
  • 500
  • 612