仅当count为0时才返回false

I am trying to make sure that my function return false only if count for cc is 0. I keep getting false back from my function even when count is more than 0.

My function:

function db_cc_exists($link, $cc) {
  $sql = "SELECT count(*) as count FROM visitors WHERE cc = '$cc' LIMIT 1";
  $result = mysqli_query($link, $sql);
  $row = mysqli_fetch_array($result);
  $ret = $row[0];

  mysqli_free_result($result);
  return (intval($ret) == 0 ? FALSE : TRUE);
}

$link is the connection of my database.

$ret ($row[0]) is an array. Use $ret['count']

return (intval($ret['count']) == 0 ? FALSE : TRUE);

UPDATE

I believe my original answer is actually incorrect.

mysqli_fetch_array takes 2 parameters - the second is the type of array to return. The default is MYSQLI_BOTH which creates an array with both numeric indexes (in your case with element '0') and associative indexes (in your case 'count').

$row[0]; should have returned the count.

The options for the 2nd parameter

/* numeric array */
$row = mysqli_fetch_array($result, MYSQLI_NUM);

/* associative array */
$row = mysqli_fetch_array($result, MYSQLI_ASSOC);

/* associative and numeric array */
$row = mysqli_fetch_array($result, MYSQLI_BOTH);

If you followed my original answer and used intval($ret['count'], you should have received the error "Illegal string offset 'count'".

A simple test is to put a var_dump($row) statement after it is set to see what is being returned.

You should get something like:

array(2) { [0]=> string(5) "123" ["count"]=> string(5) "123" } 

Your query returns either any value from 0 to number of records. So try condition like this.

Please try to check like this.

function db_cc_exists($link, $cc) {
  $sql = "SELECT count(*) as count FROM visitors WHERE cc = '$cc' LIMIT 1";
  $result = mysqli_query($link, $sql);
  $row = mysqli_fetch_array($result);    
  mysqli_free_result($result);
  return ($row[0] ? TRUE : FALSE);
}