-1

I'm having trouble where I wanted to check if the user_name is empty then it will echo YES. But now I din get any of the echo results. I received:

Error: Commands out of sync; you can't run this command now {"success":1}

if ($result = $mysqli->query("SELECT user_name FROM user WHERE user_id = '33'", MYSQLI_USE_RESULT)){

        while ($row = mysql_fetch_array($result)) {
            if($row['user_name']===' '){
                echo "YES";
            }
        }

            /* Note, that we can't execute any functions which interact with the
               server until result set was closed. All calls will return an
               'out of sync' error */
            if (!$mysqli->query("SET @a:='this will not work'")) {
                printf("Error: %s\n", $mysqli->error);
            }

            $result->close();
            $json['success']    = 1;

}else{
    $json['success'] = 7;
}

So, what my problem and how to solve? I had tried finding the answers and solutions on web but none of it works. Thanks for guidance and advice.

Haresh Chhelana
  • 24,720
  • 5
  • 57
  • 67
Savage Leo
  • 21
  • 12

4 Answers4

0
while ($row = mysql_fetch_array($result)) {
            if($row['user_name']===''){
                echo "YES";
            }
        }
PHP Geek
  • 3,949
  • 1
  • 16
  • 32
  • I got the error that I mentioned: Error: Commands out of sync; you can't run this command now {"success":1} – Savage Leo Aug 11 '16 at 04:40
0

try this:

if($row['user_name']===' ') to if(empty($row['user_name']));
Gynteniuxas
  • 7,035
  • 18
  • 38
  • 54
0

Use empty() or strcmp() if empty ... otherwise is_null() if null is stored in database

Dhiraj Sharma
  • 4,364
  • 24
  • 25
0

from PHP .net you can see: If you use MYSQLI_USE_RESULT all subsequent calls will return error Commands out of sync unless you call mysqli_free_result()

So, either you use

1) MYSQLI_STORE_RESULT and mysqli_free_result()

2) mysql_query // traditional

3) established the connection again(reconnect to db)

Riad
  • 3,822
  • 5
  • 28
  • 39