Connor
Connor

Reputation: 17

Comparing MySql variables

I am trying to make a addition to my website where admins can ban people, but my code isn't working. I have looked everywhere but i can't find a fix. Can anyone help me? Here is my code.

<?php

    $con = mysql_connect("localhost", "root", "");
    if (!$con) {
        die('Could not connect: ' . mysql_error());
    }
    mysql_select_db("users", $con);
    $objGet = mysql_query("SELECT `blocked`  FROM `users` WHERE `username` LIKE '$username'");

    if (mysql_num_rows($objGet) === "1") {
        echo '<script>alert("Account Suspended")</script>';
        header('Location: userAccount.php?logoutSubmit=1');
    } else {
        //Do Nothing
    }
    ?>

Error :

Warning: mysql_num_rows() expects parameter 1 to be resource, string given in C:\xampp\htdocs\index.php on line 50

Ok i got the code working with completely modified code but it works

Upvotes: 0

Views: 74

Answers (1)

Antti29
Antti29

Reputation: 3033

I think the problem is with this line:

if(mysql_num_rows($objGet) === "1") {

mysql_num_rows() returns an integer, but you're comparing it against a string with the identity check ===, which will always return false.

Maybe this would be a better comparison:

if(mysql_num_rows($objGet) > 0) {

Upvotes: 1

Related Questions