I am using the following code:

$db = JFactory::getDBO();
$query = $db->getQuery(true);
    ->from ('#__gruposlocales');
$query-> where('latitudX = ' . (int) $lat);
$resu = $db->loadObject();
echo "<p>Gruposs: $gro</p>";

where $lat is a variable from a previous code and its value is -0.180653. The code doesn't work; it shows nothing after "Gruposs".

If I use $query-> where('latitudX = -0.180653'); instead, the code works perfectly, showing a name. If in the upper code, I use echo "<p>Gruposs: $lat</p>"; it shows -0.180653, telling me that the $lat variable is being passed correctly.

I am using the same code syntax in a previous part of my code with a different table and a variable in the "where" statement and it works perfectly. Why doesn't my code work with the $lat variable in the "where" statement?

  • Please state, in your question, the Data Type of the latitudX column. Is it FLOAT, DOUBLE, DECIMAL, VARCHAR? This will perhaps lead to an acceptable answer and/or help a future researcher with their issue. Mar 23, 2018 at 11:00
  • I am quite surprised that Richard's solution didn't fix the issue -- this is what I would recommend as well. Perhaps the implementation of his advice is incorrect. Can you show how you implemented float casting (show us your updated snippet)? Also, can you call var_dump($lat); and tell us what that prints to screen? This will help us to isolate if you have any invisible characters or something wonky. Please continue to improve and clarify your question so that this page can be officially resolved. May 27, 2018 at 12:50

1 Answer 1


(int) creates an integer, so (int) $lat will remove the bit after the decimal point, ie it will equal 0.

Use (float) $lat instead if you want to convert a string to a floating point number.

  • Thank you Richard. Your suggestion did not work either but I found that this works: $query-> where('latitudX ='. $lat); Feb 17, 2018 at 20:32
  • @PabloCastelo - just make sure that you have validated and sanitized the $lat variable that you pass into your database, if it comes from user input.
    – FFrewin
    Feb 19, 2018 at 10:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.