0

I am trying to do a query in PHP PDO where it will grab a simple result. So like in my query I need it to find the row where the column group is 'Admin' and show what ever is in the group column. I know that we already know what it should be [Should be admin] but just need to get the query to work. Its only grabbing 1 row from my table, so will I need forsearch?

If I change WHERE group = 'Admin' to WHERE id = '1' it works fine. But I need it so it can be where group = 'admin'

$sql2 = "SELECT * FROM groups WHERE group = 'Admin'";
$stm2 = $dbh->prepare($sql2);
$stm2->execute();
$users2 = $stm2->fetchAll();

foreach ($users2 as $row2) {
    print ' '. $row2["group"] .' ';
}

Thanks

2

2 Answers 2

1

group is a reserved word in MySQL, that's why it's not working. In general it's a bad idea to use reserved words for your column and table names. Try using backticks around group in your query to get around this, so:

$sql2 = "SELECT * FROM groups WHERE `group` = 'Admin'";

Also you should really use placeholders for values, because you're already using prepared statement it's a small change.

Edit: just to clarify my last remark about the placeholders. I mean something like this:

$sql2 = "SELECT * FROM groups WHERE `group` = ?";
$stm2->execute(array('Admin'));
Sign up to request clarification or add additional context in comments.

Comments

0

try to use wildcard in your WHERE Clause:

$sql2 = "SELECT * FROM groups WHERE group LIKE '%Admin%'";

Since the value in your table is not really Admin but Administrator then using LIKE and wildcard would search the records which contains admin.

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.