[SOLVED] PHP SQL QUERY: SYNTAX ERROR

kaito

Member
I have an element field to display some chained info in a QR, that will be stored in database afab_entrenamiento. In PHP validation I need to retrieve last "id" from database, so I make the query but it replies with a SQL Maria DB syntax error:

"1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'FROM afab_entrenamiento' at line 2"

My query code into php validation frame is:
PHP:
//para conocer el identrenamiento con el que voy a grabar esta información, tengo que consultar cuál es el último id de la tabla afab_entrenamientos y sumarle una unidad
$mydb = JFactory::getDbo();
$myQuery = $mydb->getQuery(true);
$myQuery
      ->select (MAX('id'))
      ->from('afab_entrenamiento');
$mydb->setQuery($myQuery);
$lastid = $mydb->loadResult();
$identrenamiento = $lastid + 1;

I have maden lots of combinations in order yo overcome this error but I haven´t found the correct one. Anyone can help me? Thanks in advance

 
You have to include the sql function in quotes
Code:
//para conocer el identrenamiento con el que voy a grabar esta información, tengo que consultar cuál es el último id de la tabla afab_entrenamientos y sumarle una unidad
$mydb = JFactory::getDbo();
$myQuery = $mydb->getQuery(true);
$myQuery
      ->select ("MAX(".$mydb->quote('id').")")
      ->from('afab_entrenamiento');
$mydb->setQuery($myQuery);
$lastid = $mydb->loadResult();
$identrenamiento = $lastid + 1;
 
Hi Icollong,
using your query syntax solves syntax error, thanks !!

PHP:
//para conocer el identrenamiento con el que voy a grabar esta información, tengo que consultar cuál es el último id de la tabla afab_entrenamientos y sumarle una unidad
$mydb = JFactory::getDbo();
$myQuery = $mydb->getQuery(true);
$myQuery
      ->select ('MAX('.$mydb->quote('id').')')
      ->from($mydb->quoteName('afab_entrenamiento'));
$mydb->setQuery($myQuery);
$lastid = $mydb->loadResult();
$identrenamiento = $lastid + 1;

But now, result from query, $lastid returns zero, when las id in database is 51, so I don´t know what I am missing. I have tried to look for another field in database, id_user instead of id to check if query is working, and the result $lastid is also zero, so something is not pointing to the database into the query, but I can not see what it is...
 
Hi Icollong,
I have found where was the problem: instead "quote" is "quoteName". Correct query finally is :
PHP:
//para conocer el identrenamiento con el que voy a grabar esta información, tengo que consultar cuál es el último id de la tabla afab_entrenamientos y sumarle una unidad
$mydb = JFactory::getDbo();
$myQuery = $mydb->getQuery(true);
$myQuery
      ->select ('MAX('.$mydb->quoteName('id').')')
      ->from($mydb->quoteName('afab_entrenamiento'));
$mydb->setQuery($myQuery);
$lastid = $mydb->loadResult();
$identrenamiento = $lastid + 1;

Many thanks for your help !!!
 
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top