1

I'm selecting something in mySQL via PHP and that command returns some array (which is right), but when I put that returning SELECT inside if condition and ask if it is returning null than PHP says it is returning null (which is not right, because it is returning array)

include '../db.php'; // my config

function select($command) {
  global $db;
  $sql = "".$command."";
  $sqlDone = $db -> prepare($sql);
  $sqlDone -> execute();
  $data = $sqlDone -> fetchAll();
  return $data;
}

$select = "SELECT likes.ID, likes.ID_user, likes.ID_post FROM likes WHERE likes.ID_user = '53' AND likes.ID_post = '2'"

if (select($select) == null) { // goes throw this
  print_r(select($select)); // returns array
} else {
    echo 'not null';
}

I tried to use !is_null and it doesn't work anyway. I tried to put that select command with same values directly inside phpmyadmin and it returns array, so I'm confused. Can you help me out?

Screenshot of phpmyadmin

12
  • Don't you want to print_r the data if not null instead of just printing "not null" ? Commented Aug 14, 2018 at 15:34
  • nah, that print_r is there just because of testing. It returns array same as in that if condition Commented Aug 14, 2018 at 15:36
  • You should use strict equality (===) instead of (==). An empty array is "falsy" in PHP Commented Aug 14, 2018 at 15:36
  • I don't really understand the purpose of your select() function, why do you need it? Seems needlessly complicated to me, and a few of the lines in there do nothing at all. Commented Aug 14, 2018 at 15:36
  • @Darragh Enright When I use this than it goes always for not null Commented Aug 14, 2018 at 15:38

1 Answer 1

1

PDO's fetchAll() returns an array, if there are no results, it returns an empty array (not NULL).

Just use empty()

$return = select($select); //put this into a variable, because if you don't, you'll query the database twice and may get different results.
if (empty($return)) { // goes throw this
  print_r($return); // returns array
} else {
    echo 'not null';
}

Side note, your function doesn't really do anything special. You could achieve the same thing with this:

$return = $db->prepare($select)->execute()->fetchAll();

If you used a PDO wrapper, it could be even shorter. For example, using my own wrapper GrumpyPDO, you would use

$return = $db->all($select);

then if you had variables to pass to the query, you would do

$select = "SELECT likes.ID, likes.ID_user, likes.ID_post FROM likes WHERE likes.ID_user = ? AND likes.ID_post = ?"
$return = $db->all($select, [$userid, $postid]);
Sign up to request clarification or add additional context in comments.

3 Comments

Didn't you mean just 'empty()' ? without exclamation mark? because '!empty()' is checking if it has something in it.
@SenTisso Yes, my bad. It confused me because print_r($return) will literally return nothing if that if statement is met.
So, I'm officially id*ot I was using ajax when clicking on button and I had some mess there and I was sending bad data and that PHP is working as it was, but anyways thank you for your time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.