0

I made a custom function to select data from database using WP methods. It looks like this;

function CheckUser($name){
                /*DB connection */
                $result = $mydb->get_results("SELECT *FROM users WHERE name = ".$name.";");
                if(!empty($result)){
                    echo "OK!";
                }
        }

And here is a part in code:

/*This part is in foreach so thats why I am using $obj
$name_string = $obj->first_name." ".$obj->surname;
Prombutne($name_string);

But every time I not receive anything from DataBase, when I change !empty to empty I get OK so Select function always return's empty.

Mārcis
  • 79
  • 6

1 Answers1

0

Replace

$result = $mydb->get_results("SELECT *FROM users WHERE name = ".$name.";");

with

$result = $mydb->get_results("SELECT * FROM users WHERE name = '".$name."'");

You have to give space after '*' and before 'table_name' and in where condition you are using name field which is string so you need to pass it in quotes.

Nirali Biniwale
  • 627
  • 5
  • 16