Reputation: 9
$select=$conn->query("SELECT `id` FROM `order` where `customer`='$id'");
while ($result=$select->fetch_assoc()) {
echo $result['id'];
}
I got the two values.
How to print the large number ? php max function is not working for me
Upvotes: 0
Views: 95
Reputation: 2011
You can directly do in query check below query for the same.
$select=$conn->query("SELECT id FROM order where customer='$id' order by id desc limit 1");
Upvotes: 0
Reputation: 31
In your way
$select=$conn->query("SELECT `id` FROM `order` where `customer`='$id'");
$maxVal = 0;
while ($result=$select->fetch_assoc()) {
if($maxVal<$result['id']){
$maxVal=$result['id'];
}
}
echo $maxVal;
But better is
$select=$conn->query("SELECT max(id) FROM order where customer='$id'");
$result=$select->fetch_assoc();
echo $result['id'];
Or if your id is auto-increment then you can use
$select=$conn->query("SELECT id FROM order where customer='$id' order by id desc Limit 1");
$result=$select->fetch_assoc();
echo $result['id'];
Upvotes: 0
Reputation: 6006
you can also take the max of id using sql as
SELECT max(id) FROM order where customer='$id'
Upvotes: 1
Reputation:
faster to do it in the query:
SELECT id FROM order where customer='$id' order by id desc Limit 1
Upvotes: 3