Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

while loop is not working with max(eno) query

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;
}
like image 513
LOKESH Avatar asked Aug 02 '26 00:08

LOKESH


1 Answers

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

like image 96
Abhik Chakraborty Avatar answered Aug 04 '26 12:08

Abhik Chakraborty



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!