0

I am trying to create a function that will check against the data from the database. If the results are empty, continue with the code. If else, abort.

Here is an example of my code.

function check_if_blocked(){
 global $wpdb;
 $user = '1';
 $results = $wpdb->get_results("SELECT * FROM mytable WHERE user = $user");
 if(empty($results)){
   $var = false;
  } else {
   $var = true;
  }
 return $var;
}

Function check_if_blocked is going to be used multiple through out the plugin.

Here is an example of how I plan on using check_if_blocked()..

function msg_form(){
if(check_if_blocked(){
  echo '<form action="" method="post">
  <input type="text" name="msg">
  <input type="submit" name="submit" value="send">';
  }
 }

Regardless on how I switch around the true, false, and even if(!check_if_blocked...

4
  • Problem #1: $user doesn't exist inside the scope of your function, consider passing it as an argument Commented May 20, 2015 at 19:33
  • Regradless of "Problem #1" The code doesn't work. Commented May 20, 2015 at 19:43
  • 1
    i don't know answer but all this code: if(empty($results)){ $var = false; } else { $var = true; } return $var; may be changed to return !empty($results); Commented May 20, 2015 at 19:45
  • Is wpdb from wordpress? I'm asking because the users table in wordpress usually doesn't have a column called "user". I doubt that's the cause but it might be worth checking. Commented May 20, 2015 at 19:56

2 Answers 2

1

You are trying to return the wrong value, get_results will return true if the query was successful, not if it found matches, so, it might return true even if there are no matching records found, try this instead:

function check_if_blocked()
 {
  global $wpdb;
  $user = '1';
  $results = $wpdb->get_results("SELECT * FROM mytable WHERE user = $user");
  if(!$results) {return false;}  // query failed, no way to know if there are matching records.
  $numrows = count($results); // query worked, now, check if there are matching records.
  if (is_int($numrows) && $numrows) {$var = true;} else {$var = false;}
  return $var; // return the result.
 }

Now, the function will return true only if there are one or more matching records.

Also, you have a syntax error here:

if(check_if_blocked(){

It should be:

if(check_if_blocked()){
Sign up to request clarification or add additional context in comments.

2 Comments

Warning: mysql_num_rows() expects parameter 1 to be resource, array given.
I re-edited my reply to match the database system you are using, try it now.
0

After everyone's advice. I finally got it to work. Here is my code.

function check_if_blocked($blocked){
global $wpdb;
$user = '1';
$user2 = '2';
$results = $wpdb->get_results("SELECT * FROM mytable WHERE blocked = $user AND blocker = $user2");
if(empty($results)){
$blocked = 'not_blocked';
} else {
$blocked = 'is_blocked';
}
}

function msg_form(){
if(check_if_blocked($blocked) == 'not_blocked){
echo '<form action="" method="post">
<input type="text" name="msg">
<input type="submit" name="submit" value="send"></form>';
}
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.