获取php变量的mysql值

时间:2014-10-25 11:40:34

标签: php mysql usersession

我的网站设置了一个存储用户数据的mysql数据库。数据库结构如下所示:

table = users

columns = Id : username : email : password : active 

active列下,有效值为1,无效值为0

当用户为我的网站付款时,active值会从0更改为1.

我想立即显示用户是否已在用户个人资料中付款。

如何查询数据库以根据active获取username列的值,并将其保存为变量以便稍后在脚本中使用?

1 个答案:

答案 0 :(得分:2)

你可以这样做:

<?php
            $sql = new mysqli ("host","user","pass" ,"db"); //EDIT with your parameters for DB
            $sql -> set_charset ( 'utf8' );
            if ($sql->connect_errno) {
            printf("Connect failed: %s\n", $sql->connect_error);
            exit();}


$user = $_SESSION['username']; // for example, you need to know what you are looking for

$q = "SELECT active FROM users WHERE username = '$user'";
if ($result = $sql->query($q)) {
while ($row = $result->fetch_assoc()) {
$active = $row['active']; 
}}

你知道$值为1或0,并且可以随时回复它

修改

如果还没有,你需要更改你的connect.php。您可以在没有凭据的情况下发布它,最好使用新代码编辑您的问题

session_start();
require('connect.php');

if (isset($_POST['username']) and isset($_POST['password'])) {
    $username = mysqli_real_escape_string($sql, $_POST['username']); //for security
    $password = mysqli_real_escape_string($sql, $_POST['password']); //for security
    $query = "SELECT * FROM user WHERE username='$username' and password='$password'";


if ($result = $sql->query($query)) {
while ($row = $result->fetch_assoc()) {
$active = $row['active']; }
        $count = $result->num_rows; //and this, too
    }
}