travelkind Posted May 16, 2012 Share Posted May 16, 2012 I am getting the following error when I try to run my php script: Notice: Undefined index: MAX(Date) in D:\Pricing\EasyPHP-5.3.9\www\FT9 05-16-12.php on line 21 The most recent date is: The most recent or MAX Date should be returned after the echo statement "The most recent date is:" Here is my code: <?php $conn=odbc_connect("XXX","xxx|xxx","xxx"); if (!$conn) { exit("Connection Failed: " . $conn); } // Get a specific result from the "FT9_FUEL_TAX_PRICE_LINES" table $query =("SELECT ItemNumber,MAX(Date) FROM 'FT9_FUEL_TAX_PRICE_LINES' WHERE ItemNumber = 'E-10 NL GAS'AND TableCode = 'CITA' GROUP BY 'ItemNumber'"); $result = odbc_exec($conn, $query) or die(odbc_errormsg()); // keeps getting the next row until there are no more to get while($row=odbc_fetch_array($result)) { // Print out result echo "The most recent date is: " .$row['MAX(Date)']; echo "<br />"; } ?> Can anyone help me with this? Any help would be much appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/262631-notice-undefined-index-maxdate/ Share on other sites More sharing options...
mrMarcus Posted May 16, 2012 Share Posted May 16, 2012 That's because you're doing it wrong Add an alias to the MAX(Date) in your query like so: $query =("SELECT ItemNumber, MAX(`Date`) as `max_date` FROM 'FT9_FUEL_TAX_PRICE_LINES' WHERE ItemNumber = 'E-10 NL GAS'AND TableCode = 'CITA' GROUP BY 'ItemNumber'"); And change your echo result to reflect that alias: echo "The most recent date is: " .$row['max_date']; Get it? Same as if you wanted to print `ItemNumber`, you do: echo $row['ItemNumber']; Quote Link to comment https://forums.phpfreaks.com/topic/262631-notice-undefined-index-maxdate/#findComment-1346075 Share on other sites More sharing options...
Maq Posted May 16, 2012 Share Posted May 16, 2012 in the future, place OR tags around your code. Quote Link to comment https://forums.phpfreaks.com/topic/262631-notice-undefined-index-maxdate/#findComment-1346077 Share on other sites More sharing options...
mrMarcus Posted May 16, 2012 Share Posted May 16, 2012 Just noticed, too, that you have your table name encapsulated in single-quotes. That will not work. Same for 'ItemNumber' in your GROUP BY. Either leave without, or use ticks (better practice is to use ticks ` as MySQL haa reserved keywords that will most likely creep in at some point in time): $query ="SELECT ItemNumber, MAX(`Date`) as `max_date` FROM `FT9_FUEL_TAX_PRICE_LINES` WHERE ItemNumber = 'E-10 NL GAS' AND TableCode = 'CITA' GROUP BY `ItemNumber`"; Edit: table names, field/column names cannot be surrounding with quotes of any kind. Leave them alone, or as recommended, use ticks. Quote Link to comment https://forums.phpfreaks.com/topic/262631-notice-undefined-index-maxdate/#findComment-1346083 Share on other sites More sharing options...
travelkind Posted May 17, 2012 Author Share Posted May 17, 2012 Hey guys thanks for the input but now I am getting the following Warning: Warning: odbc_exec() [function.odbc-exec]: SQL error: [ProvideX][ODBC Driver][FILEIO]Table is not accessible, SQL state S0000 in SQLExecDirect in D:\Pricing\EasyPHP-5.3.9\www\FT9 05-16-12 Night Version.php on line 15 [ProvideX][ODBC Driver][FILEIO]Table is not accessible Here is my revised code I must have missed something! <?php $conn=odbc_connect("XXX","xxx|xxx","xxx"); if (!$conn) { exit("Connection Failed: " . $conn); } // Get a specific result from the "FT9_FUEL_TAX_PRICE_LINES" table $query ="SELECT ItemNumber, MAX(`Date`) as `max_date` FROM `FT9_FUEL_TAX_PRICE_LINES` WHERE ItemNumber = 'E-10 NL GAS' AND TableCode = 'CITA' GROUP BY `ItemNumber`"; $result = odbc_exec($conn, $query) or die(odbc_errormsg()); // keeps getting the next row until there are no more to get while($row=odbc_fetch_array($result)) { // Print out result echo "The most recent date is: " .$row['max_date']; echo "<br />"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/262631-notice-undefined-index-maxdate/#findComment-1346169 Share on other sites More sharing options...
mrMarcus Posted May 17, 2012 Share Posted May 17, 2012 Sounds like there's a permissions issue with accessing the table. No longer a PHP issue, but an Access issue. Quote Link to comment https://forums.phpfreaks.com/topic/262631-notice-undefined-index-maxdate/#findComment-1346299 Share on other sites More sharing options...
Maq Posted May 17, 2012 Share Posted May 17, 2012 travelkind, in the future, place OR tags around your code. Quote Link to comment https://forums.phpfreaks.com/topic/262631-notice-undefined-index-maxdate/#findComment-1346332 Share on other sites More sharing options...
travelkind Posted May 17, 2012 Author Share Posted May 17, 2012 There is an issue with the ODBC connection so I created a local Mysql database and the code ran perfectly! I will have to look into ODBC issue but I appreciate all the help you guys gave me on this! Thanks again! Quote Link to comment https://forums.phpfreaks.com/topic/262631-notice-undefined-index-maxdate/#findComment-1346355 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.