简体   繁体   中英

How do I pull the value of something from an SQL database [PHP]?

I have a column in my database called "Admin" that I want to be able to check the value of (whether it's 1 or NULL). And if it's 1 I want it to do something.

How do I write up the query and validate that the entry is = to 1?

Right now I just have the following set up

<?php
    include 'db_connect.php';

    if ($_SESSION['username'] == "admin")
    {
        echo '<a href="watWk8.php"> Admin Panel </a>';
        echo '';
    }
    else
    {
        echo "Please log in as admin (user: admin password: admin) to use the admin control panel";
    }
?>

Meaning if the user name is admin it will continue running the loop but I want any user to be able to tick a box on registering and have administrative rights. (I know this is destructive but it's just for a school project and a proof of concept)

Thanks!

EDIT:

I tried the following

 <?php include 'db_connect.php'; $query = "SELECT * from member WHERE Admin = '1'"; $result = mysqli_query($connection, $query); if ($_SESSION['username'] == "admin") { echo '<a href="watWk8.php"> Admin Panel </a>'; echo ''; echo $result; } else { echo "Please log in as admin (user: admin password: admin) to use the admin control panel"; } ?> 

And got the following error after trying to echo it https://i.imgur.com/4cBzHog.png

u are trying something like this maybe..

function admin_check( $email, $password ){
    $db = connect();
    if( $db ){
        $admin=1;
        $stmt = $db->prepare("SELECT * FROM users WHERE email = :email and password = :password and admin =:admin ");
        $stmt->bindParam(":email", $email);
        $stmt->bindParam(":password", $password);
        $stmt->bindParam(":admin", $admin);
        $stmt->execute();
        $row = $stmt->fetchAll();
        $resultCount = count($row);
        return $resultCount;
    }
}

Please, for the right way to connect and get data from DataBase read this manual: http://php.net/manual/en/pdo.connections.php

In the most cases using PDO over mysql or mysqli drivers are much more useful.

You just need to know about the return type of mysqli_query(); which return a mixed type variable means it could be an array or binary (True or False) sometimes depends on the query type. The following result may be output as false for error or array for data return so use condition for checking the $result simply by:

if($result) // or if(count($result) > 0 && $result)

If data exists then use a loop for per row data as per your need and for print complete $result you need to use a var_dump() or printr() instead of echo like :

 var_dump($result);

For details about mysqli_query() follow this link: http://php.net/manual/en/function.mysql-query.php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM