dongzhan3937
dongzhan3937
2014-01-22 11:33

短PHP代码不返回数据(return,true或false)

  • php
  • return

When I run this code, everything works:

<?php
$con = mysqli_connect ("", "", "", "") or die ($connect_error);

$username = 'tijmen';

$query = mysqli_query($con, "SELECT COUNT(*) FROM users WHERE username='$username'") or     die (mysqli_error());
$result = mysqli_fetch_row($query); 
echo $result[0];
?>

The username 'tijmen' exists in my database so the echo returns '1'.

Next thing I tried was:

$con = mysqli_connect ("", "", "", "") or die ($connect_error);

function user_exists($username) {
$query = mysqli_query($con, "SELECT COUNT(*) FROM users WHERE username='$username'") or die (mysqli_error());
$result = mysqli_fetch_row($query);  
return ($result[0] == 1) ? true : false;
}

if (user_exists('tijmen') === true) {
echo 'user exists';
}
else {
echo 'not found';
}

When I run this code nothing happens and I don't know why. Can anyone help me out on this one?

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

5条回答