Syntax error on return statement [closed]

南楼画角 提交于 2020-01-17 02:43:11

问题


I'm doing this simple website, and I have run into this error:

My function:

<?php 
function user_exists($username)
{
    $username = sanitize($username);
    $query = mysqli_query($connect, "SELECT COUNT(`user_id`) FROM `users` WHERE `username` = '$username'");
    return (mysqli_result($query, === 0) 1) ? true : false;
}
 ?>

My php error log:

PHP Parse error:  
syntax error, unexpected '===' (T_IS_IDENTICAL) in function on line 6

Line 6 is the return line.

I understand what a syntax error means, but I'm quite sure that the '===' is not the problem.


回答1:


Edit : I was only talking about the ternary condition and this answer is false because the mysqli_result() function doesn't exist.

I guess you are trying to do this :

return mysqli_result($query) === 0 ? false : true;

And as Marcel Korpel said, use prepared statements to avoid security flaws.




回答2:


You have a few problems here. First of all there is no mysqli_result(), it does not exist. Instead you can fetch the row like below. Also your $connect is out of scope. You need to pass it as an argument, and as the comments point out even if mysqli_result() did exist, it still wouldn't work because of the syntax error.

function user_exists($username, $connect)
{
    $output = false;
    $username = sanitize($username);
    $query = mysqli_query($connect, "SELECT COUNT(`user_id`) FROM `users` WHERE `username` = '$username'");

    if($query) // check the query was successful before trying to fetch
    {
        $row = mysqli_fetch_row($query);
        $output = $row[0] > 0;
    }

    return $output;
}

I assume your sanitize() is doing mysqli_real_escape_string(). For best security, switch to a Prepared Statement.



来源:https://stackoverflow.com/questions/17239090/syntax-error-on-return-statement

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!