Undefined index for database field

The code below shows the undefined index warning for my database field name "zatitle", the spelling is correct in both the code and the database. I can not find out about the error, please help.

include('connect.php'); dbConnect(); $myquery=mysql_query("SELECT zaid,zatext, MAX(zatitle) FROM announcements") or die(mysql_error()); if (mysql_num_rows($myquery) == '1') { $asession=mysql_fetch_array($myquery); $ses = $asession['zatitle']; if($ses=='1'){ $one='2013-2014'; }elseif($ses=='2'){ $one='2014-2015'; }elseif($ses=='3'){ $one='2015-2016'; }elseif($ses=='4'){ $one='2016-2017'; } 

For this code, the warning is below

Note: undefined index: zatitle in C: \ xampp \ htdocs \ home \ home.php on line 9

+6
source share
2 answers

Use alias for a column with MAX .

 $myquery=mysql_query("SELECT zaid,zatext, MAX(zatitle) as zatitle FROM announcements") or die(mysql_error()); ^^^^^^^^^^ 
+3
source

I think you need such a request

 $myquery=mysql_query("SELECT zaid,zatext, MAX(zatitle) as zatitle FROM announcements") 

because if you select Max (zatitle), then the result will also be displayed as Max (zatitle)

+3
source

Source: https://habr.com/ru/post/986206/


All Articles