0

I'm a total newbie when it comes to php and mysql/mysqli. I have this code and I get a PHP Parse error: syntax error, unexpected T_OBJECT_OPERATOR in /home/byeroman/public_html/register.php on line 17. Here's the code:

$stmt = mysqli->prepare("SELECT COUNT(*) FROM users WHERE username=? LIMIT 1" or die($db->error()));
        $stmt->bind_param("s", $username);
    $stmt->execute();
    $stmt->store_result();
        $count=$stmt->num_rows;
        $stmt->close();
        if($count>0) exit();

what's wrong? thanks guys

1 Answer 1

1

That's silly syntax issue, you just forgot closing brace.
To make such things less possible and also to make your code readable, divide your statements into separate lines:

$sql  = "SELECT COUNT(*) FROM users WHERE username=? LIMIT 1";
$stmt = $mysqli->prepare($sql) or trigger_error($mysqli->error()));

You also need to make your mind which variable you're using ($mysqli or $db)
Also, num_rows() is wrong function to use, you need regular fetch instead.

By the way, consider to use some database abstraction library. It can make your life a lot easier and code - shorter, like this (it replaces ALL your code, mind you):

$num = $db->getOne("SELECT COUNT(*) FROM users WHERE username=?",$username);
if($num) { ... 
Sign up to request clarification or add additional context in comments.

Comments

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.