Lina
Lina

Reputation: 627

“Trying to get properties on a non-object” in wordpress

I'm creating wordpress theme. I get this error inside a textarea: Trying to get properties on a non-object.

function add_map_url($post){

        global $wpdb;
        $info = $wpdb->get_var("SELECT * FROM $wpdb->contacts WHERE ID= $post->ID  LIMIT 1");
        $val = !empty($info) ? htmlspecialchars($info->link) : '';
        echo '<textarea name="link" rows="6" cols="30">'.$val.'</textarea>';
    }

It seems the problem causing the error is: $info->link. If I am changing the code:

function add_map_url($post){

            global $wpdb;
            $info = $wpdb->get_var("SELECT * FROM $wpdb->contacts WHERE ID= $post->ID  LIMIT 1");
            //$val = !empty($info) ? htmlspecialchars($info->link) : '';
            echo '<textarea name="link" rows="6" cols="30">'.$info.'</textarea>';
        }   

It's working and gives contacts ID.

The table contacts looks like:

contacts
ID     link

I can't figure out what I'm doing wrong. Thanks for any advice!

Upvotes: 0

Views: 985

Answers (1)

Mike Purcell
Mike Purcell

Reputation: 19999

The query probably isn't returning any rows matching the criteria given, in other words the row doesn't exist in the table. You can run the query from the mysql command line and see if it returns anything. You may want to change the empty() call to is_object():

$val = is_object($info) ? htmlspecialchars($info->link) : '';

Upvotes: 1

Related Questions