0

In ordinary php I don't have that kind of problem I think but in WP this is a different story. I have query and I want return one value from "liczba_wodo" column.

global $wpdb;
if (isset($_POST['submit'])){
        $spr_liczbe_wodo = $wpdb->get_row ("select liczba_wodo from wp_ow_adres where adres='Street 12/6'");
        foreach ( $spr_liczbe_wodo as $print )   {
        echo $print->liczba_wodo;
        echo 'Echo test';
        }

    }
}

Of course 'echo test' works fine. I think that "$spr_liczbe_wodo" is null or something. Thanks for help.

4
  • This is a PHP issue, the DB query in WordPress is no different, turn on debugging and use prepare.
    – Wyck
    Commented Dec 22, 2013 at 17:50
  • @Wyck I still have get_row and I see this notice: Notice: Trying to get property of non-object in Commented Dec 22, 2013 at 18:02
  • This seems ok, what does var_dump() on result give you?
    – Rarst
    Commented Dec 22, 2013 at 18:18
  • object(stdClass)#2711 (1) { ["liczba_wodo"]=> string(1) "2" } Commented Dec 22, 2013 at 18:19

1 Answer 1

0

If you need to return one value then use get_var instead of get_row. get_var will return a string. get_row will return an array or object.

$spr_liczbe_wodo = $wpdb->get_var ("select liczba_wodo from wp_ow_adres where adres='Street 12/6'");
var_dump($spr_liczbe_wodo);

The problem with your code, by the way, is that $spr_liczbe_wodo is an object, but when you loop over it $print is not but you attempt to use it as if it were an object anyway. Through in a couple of var_dumps and you can easily see the problem.

0

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.