Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Checking users boolean from MySQL

So the title may be confusing, I just did not find the correct words for this but here is my problem. I have a login and signup system of where the user has an is_admin column set to false or 0 by default. I am simply trying to check if the user is an admin to reveal curtain things. When the user logs in the only reak thing that happens is a session starting like this

$_SESSION['uid'];

It picks up the username for the session. What do I do to check to se if the boolean is true to then reveal the information?

1 Answer

Robert Kulagowski
Robert Kulagowski
4,954 Points

I don't think there's enough information, but I'll guess. If you want a PHP boolean (true / false) then you'll need to cast the response from MySQL.

Here's a snippet from a function that I have; the main code is invoking this to determine if they user is a superuser and if they can delete. In the MySQL table, superUser and allowDelete are

tinyint(1) default '0'

So MySQL is going to return an integer value. We then cast by using (boolean)

$stmt = $dbh->prepare("SELECT superUser, allowDelete FROM user WHERE userid=:userid");
$stmt->execute(array("userid" => $userid));
$result = $stmt->fetch(PDO::FETCH_ASSOC);
return (array((boolean)$result["superUser"], (boolean)$result["allowDelete"]));