Jump to content

Unable to get value from table


RedInjection

Recommended Posts

Hello
 
<?php
$sql = "SELECT role FROM users";
$result = $mysqli->query($sql);
 
$row = $result->fetch_assoc();
$checkrole = $row['role'];
if (logged_in() == true AND $checkrole == 'admin' ) { ?> <b>I am admin</b> <?php ; } <?php if (logged_in() == true AND $checkrole == 'user' ) { ?> <b>I am user</b> <?php ; } ?>
 
My table has 1 admin and 1 user but when I run this script the user is saying admin when it should be saying user?

 

I had this working and now its broke, I dropped my table and recreated but didn't fix it...

Edited by RedInjection
Link to comment
Share on other sites

Does admin have the admin role, is it also the first entry to have such a role, if so that will be your first result.

In cases like this I test for a single result otherwise display an error msg (in debug anyway, else log it).

 

Here I'm guessing that multiple users can have the same role, however each user has a unique username, id, etc...

 

The easiest way to debug this is to loop the results and print them all out...

 

 

 

while ($row = $result->fetch_assoc()) {
    echo ...
}

 

 

 

 

Scratch all that, you're asking it for users with admin role! So that's what you're getting!?

 

I changed to this


$sql = "SELECT * FROM users where role = 'admin'";
It's still returning admin for when a user is logged in user role

 

Edited by 0x00
Link to comment
Share on other sites

Tried to clear my cache but same result I am afraid

 

"SELECT role FROM users WHERE role='admin'

mysqli_result Object ( [current_field] => 0 [field_count] => 1 [lengths] => Array ( [0] => 5 ) [num_rows] => 1 [type] => 0 )

 

SELECT role FROM users WHERE role='user'

mysqli_result Object ( [current_field] => 0 [field_count] => 1 [lengths] => Array ( [0] => 4 ) [num_rows] => 1 [type] => 0 )

 

SELECT role FROM users WHERE role='user' or role='admin'

mysqli_result Object ( [current_field] => 0 [field_count] => 1 [lengths] => Array ( [0] => 5 ) [num_rows] => 2 [type] => 0 )

 

Appreciate your help to all!

Link to comment
Share on other sites

Fixed it be reinstalling PHP, without any modifications my original script post works :)

<?php
$sql = "SELECT role FROM users";
$result = $mysqli->query($sql);

$row = $result->fetch_assoc();
$checkrole = $row['role'];
if (logged_in() == true AND $checkrole == 'admin' ) { ?> <b>I am admin</b> <?php ; } <?php if (logged_in() == true AND $checkrole == 'user' ) { ?> <b>I am user</b> <?php ; } ?>
Link to comment
Share on other sites

I have absolutely no idea how this could possibly “work”.

 

You're randomly selecting the role of a user, and if that user happens to be an admin, then you consider the current user an admin as well? So anybody can be an admin on your site if they're lucky?

 

Shouldn't you be selecting the actual user that's currently logged in?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.