1

Basically, I have been having some trouble with sending a request to a MySQL server and receiving the data back and checking if a user is an Admin or just a User.

Admin = 1 User = 0

<?php 
$checkAdminQuery = "SELECT * FROM `users` WHERE `admin`";
$checkAdmin = $checkAdminQuery
mysqli_query = $checkAdmin;

if ($checkAdmin == 1) {
echo '<h1>Working!</h1>';
}else {
echo '<h1>Not working!</h1>'; 
}

?>

Sorry that this may not be as much info needed, I am currently new to Stack Overflow.

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • 2
    The SQL is incomplete. You need to say what `admin` should be equal to. You also have at least one error in your code. Fix those two first. If you do not see any errors then [enable them](http://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display). – Sverri M. Olsen Mar 10 '15 at 12:42
  • Specify database field name for which 'admin' is stored as value ....! – Anand Solanki Mar 10 '15 at 12:43
  • SELECT * from users where 'admin' = 1; – RaMeSh Mar 10 '15 at 12:47

4 Answers4

1

You need to have something like user id if you want to check someone in database. For example if you have user id stored in session

<?php
// 1. start session
session_start();
// 2. connect to db
$link = mysqli_connect('host', 'user', 'pass', 'database');
// 3. get user
$checkAdminQuery = mysqli_query($link, "SELECT * FROM `users` WHERE `id_user` = " . $_SESSION['id_user'] );
// 4. fetch from result
$result = mysqli_fetch_assoc($checkAdminQuery);

// 5. if column in database is called admin test it like this
if ($result['admin'] == 1) {
  echo '<h1>Is admin!</h1>';
}else {
  echo '<h1>Not working!</h1>'; 
}
?>
MatejG
  • 1,393
  • 1
  • 17
  • 26
1

Firstly, your SQL query is wrong

SELECT * FROM `users` WHERE `admin`

It's missing the rest of the WHERE clause

SELECT * FROM `users` WHERE `admin` = 1

Then you're going to need fetch the result from the query results. You're not even running the query

$resultSet = mysqli_query($checkAdminQuery)

Then from there, you'll want to extract the value.

while($row = mysqli_fetch_assoc($resultSet))
{
    //do stuff
}

These are the initial problems I see, I'll continue to analyze and find more if needed.

See the documentation here

http://php.net/manual/en/book.mysqli.php

Zarathuztra
  • 3,215
  • 1
  • 20
  • 34
1

// get all admin users (assumes database already connected)

$rtn = array();
$checkAdminQuery = "SELECT * FROM `users` WHERE `admin`=1";
$result = mysqli_query($dbcon,$checkAdminQuery) or die(mysqli_error($dbconn));

while($row = mysqli_fetch_array($result)){
   $rtn[] = $row;
}
0
$checkAdminQuery = "SELECT * FROM `users` WHERE `admin`"; !!!!

where what ? you need to specify where job = 'admin' or where name ='admin' you need to specify the column name where you are adding the admin string