0

I am using the following code:

$db = JFactory::getDBO();
$query = $db->getQuery(true);
$query->select('name')
    ->from ('#__gruposlocales');
$query-> where('latitudX = ' . (int) $lat);
$resu = $db->loadObject();
$gro=$resu->name;
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. – mickmackusa Mar 23 '18 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. – mickmackusa May 27 '18 at 12:50
2

(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.

| improve this answer | |
  • Thank you Richard. Your suggestion did not work either but I found that this works: $query-> where('latitudX ='. $lat); – Pablo Castelo Feb 17 '18 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 '18 at 10:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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