Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

: 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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading