I have a site set up with a mysql DB that stores user data. The database structure looks like this:
table = users
columns = Id : username : email : password : active
Under the column active
the value is 1 for active and 0 for deactive
When a user pays for my site, the active
value changes from 0 to 1.
I want to show now if a user has paid or not at the user profile.
How can I query the database to get the value of the active
column based on the username
and save it as a variable to use later in the script?
You can just do:
<?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'];
}}
You know have $active with value 1 or 0 and can echo it whereever you want
EDIT
You will need to change your connect.php, in case you haven't yet. Can you please post it without your credentials, it's best to edit your question with the new code
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
}
}