Search code examples
phpsqlpdoxampp

why does my $type variable get saved as 'Admin''?


am trying to get the number of admins in my table it does work normally but now that i added inner join for two tables i get this error of an extra ' in the word admin and i don't know how to remove it so here is my function in userc.php :

    function admincountsearch($key)
    {
        $con = config::getConnexion();
        $sql = "SELECT  user.username,user.name,user.lastname,user.id, 
                    user.email, user.image, user.verified, usero.type, 
                    usero.description,usero.ban 
            FROM user 
                INNER JOIN usero ON user.id_o = usero.id 
            WHERE user.username LIKE :keyword 
                OR user.id LIKE :keyword 
                OR user.name LIKE :keyword 
            WHERE usero.type=:type ";
        $stmt = $con->prepare($sql);
        $type ="Admin" ;
        $stmt->bindValue(':keyword', '%' . $key . '%', PDO::PARAM_STR);
        $stmt->bindValue(':type',$type, PDO::PARAM_STR);
        $stmt->execute();
        $stmt->fetch();
        $results = $stmt->rowCount();
        return $results;
    }

and here is how i call it in find.php:

<span class="info-box-number"><?php echo $userc->admincountsearch($key); ?></span>

and here is the error that i got:

: Uncaught PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'WHERE usero.type='Admin'' at line 2 in C:\xampp\htdocs\Project\Integre\Controller\userc.php:458 Stack trace: #0 C:\xampp\htdocs\Project\Integre\Controller\userc.php(458): PDOStatement->execute() #1 C:\xampp\htdocs\Project\Integre\Views\find.php(200): userc->admincountsearch('g') #2 {main} thrown in


Solution

  • You have 2 WHERE clauses in your statement. You probably meant

    SELECT
      user.username,
      user.name,
      user.lastname,
      user.id,
      user.email,
      user.image,
      user.verified,
      usero.type,
      usero.description,
      usero.ban
    FROM
        user
    INNER JOIN usero ON
        user.id_o = usero.id
    WHERE (
      user.username LIKE :keyword OR
      user.id LIKE :keyword OR
      user.name LIKE :keyword
    ) AND
    usero.type=:type