我的网站设置了一个存储用户数据的mysql数据库。数据库结构如下所示:
table = users
columns = Id : username : email : password : active
在active
列下,有效值为1,无效值为0
当用户为我的网站付款时,active
值会从0更改为1.
我想立即显示用户是否已在用户个人资料中付款。
如何查询数据库以根据active
获取username
列的值,并将其保存为变量以便稍后在脚本中使用?
答案 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
}
}