1

i'm trying to make a check to show content or not:

$stmt = $mysqli->prepare("SELECT *
        FROM friends
        WHERE friendID = ?
        AND userID = ?");

$stmt->bind_param('ii', $id, $connectedUserID);
$stmt->execute();

if (login_check($mysqli) == true && $stmt->num_rows > 0) {
    //do something
} else echo 'you are not allowed to be here';

I want to check if there's any row with those two ids.

But it's not working, i tried many different ways like using === or !== false.

Any help?

Ty!

2
  • as said, the bind_param method doesn't runs the uery, you have to use the execute method to run your builded query Commented Sep 30, 2014 at 8:16
  • Thanks, is that the correct way? Commented Oct 1, 2014 at 4:34

2 Answers 2

1

Most likely login check does not return true, the following should be enough

if(isset($id, $connectedUserID)){
    $stmt = $mysqli->prepare("SELECT *
            FROM friends
            WHERE friendID = ?
            AND userID = ?");

    $stmt->bind_param('ii', $id, $connectedUserID);
    $stmt->execute();

    $stmt->execute();
    if ($stmt->num_rows > 0) {
        //do something
    } else{
        echo 'you are not allowed to be here';
    }
}else{
    //redirect to login
}

If the user need to be logged in then this is not the place to check for that, it should be prior to this query.

Sign up to request clarification or add additional context in comments.

8 Comments

Yes, the user needs to be logged in, but if $connectedUserID = $_SESSION['user_id']; is empty he won't be able to pass the if. So is that enough, right?
yes exactly, this logic has a flaw, because you need to be connected to get the value for this params, so that should be checked before running the query
ok added a if with login_check function... I don't know if calling too much times this function can broke it or something :P
You just need to make sure the session are set before the query. let me know if you need more help
I have it this way, "if login_check == true" and inside "if $stmt->num_rows > 0". The problem is that I don't know if it works because I'm logged in and the script sends me to the else... i think it's because I'm already using login_check by comparing it to false... I mean, i need to use it many time and not always true.
|
1

Try using a simple '=' or declare a boolean variable and set it 'true' and then compare

Also read:

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.