mhopkins321
mhopkins321

Reputation: 3083

Save a 1 row, 1 field query directly into a variable instead of using while loop

If you have a query like

SELECT max(customerID) FROM customers

Is there a way to save that directly to a variable instead of using

$results = $dbLink->query("SELECT max(customerID) FROM customers");
while($row = $results->fetch_assoc()){
    $maxID = $row['customerID'];
}

The latter is just a lot of type for 1 value

Upvotes: 0

Views: 36

Answers (1)

zerkms
zerkms

Reputation: 255025

That's easy - just don't use loop:

$results = $dbLink->query("SELECT max(customerID) FROM customers");
$row = $results->fetch_assoc();
$maxID = $row['customerID'];

Upvotes: 3

Related Questions