I used the query:
$sql = mysql_query("SELECT MAX(eno) FROM employee");
Which retrieve last record from database, but it is not working with while loop. If I used:
$sql = mysql_query("SELECT * FROM employee");
This query then the while loop runs properly. Why does this happen?
$sql = mysql_query("SELECT MAX(eno) FROM employee");
while ($row = mysql_fetch_assoc($sql)) {
$asd = $row['eno'];
echo "eno".$asd;
}
You need to provide an alias something as
SELECT MAX(eno) as eno FROM employee
Also using max() will give you one row so no need to fetch through loop you may use some other function and most importantly you must start using prepared statement.
Check the below thread
Fetch only one row in PHP/MySQL
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With