Morgan Clarke Morgan Clarke - 21 days ago 5
PHP Question

MySQLI echo data from database

I am creating a website and I am using mySqli to connect to the database. For some reason when I try to echo the information from the database I am not getting anything returning. I need it to take a column from the table and display it on the page, this isn't happening. I have asked someone else and they thought it was strange (we spent about 1 hour trying to fix it) so any help would greatly appreciated.

postimg.org/image/qqv4vmtf7
postimg.org/image/rp6hhz8ht

<?php
ob_start();
session_start();
include_once 'dbconnect.php';

if(!isset($_SESSION['user'])) {
header("Location: index.php");
exit;
}

$condition = empty($_POST['sender']) || empty($_POST['reciever']);
if (!$condition) {
$name = $_POST['sender'];
$reciever = $_POST['reciever'];

$query = "UPDATE users SET userCoins = userCoins + 1 WHERE userName='Morgan'";
$res = $mysqli->query($query);
if ($res) {
$error = "Success!";
} else {
$error = "Something Went Wrong!";
echo "Error: ".$mysqli->error; // here you can check your errors
}
$sql= "SELECT * FROM users WHERE userId=".$_SESSION['user'];
$result = $mysqli->query($sql);
$userRow = $result->fetch_assoc();
if (!$userRow) {
die("userID {$_SESSION['user']} not found!");
}
}
?>
<!DOCTYPE html>
<html>
<?php header("Access-Control-Allow-Origin: http://www.py69.esy.es"); ?>
<head>
<title>ServiceCoin</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="assets/css/bootstrap.min.css" type="text/css" />
<link rel="stylesheet" href="scripts/home/index.css" />
</head>
<body>
<ul>
<li><a href="#" class="a">ServiceCoin.com(image)</a></li>
<li><a href="logout.php?logout" class="a">Sign Out</a></li>
<li><a href="#" class="a">Contact</a></li>
<li><a href="#" class="a">Get Service Coins</a></li>
<li><a href="#" class="a">News</a></li>
<li><a href="settings.php" class="a">Settings</a></li>
<li><a href="#" class="a">Referrals</a></li>
<li><a href="service.php" class="a">Services</a></li>
<li><a href="home.php" class="a">Home</a></li>
</ul>
<br /><br />
<center>
<h3>Welcome, <?php echo $userRow['userName']; ?>. You Currently Have <br /><br /><br /><br /><br /><span id="services"><?php var_dump($userRow) ?></span> Service Coins</h3>
<form method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" autocomplete="off">
<div class="form-group">
<div class="input-group">
<span class="input-group-addon"><span class="glyphicons glyphicons-lock"></span></span>
<input type="text" name="sender" class="form-control" placeholder="Enter Your Wallet Key" maxlength="15" />
<span class="text-danger"><?php echo $error; ?></span>
</div>
<div class="input-group">
<span class="input-group-addon"><span class="glyphicons glyphicons-lock"></span></span>
<input type="text" name="reciever" class="form-control" placeholder="Enter The Recievers Wallet Key" maxlength="15" />
<span class="text-danger"><?php echo $error; ?></span>
</div>

</div>
<div class="form-group">
<button type="submit" class="btn btn-block btn-primary" name="send">Sign Up</button>
</div>
</form>
</center>
</body>
</html>
<?php ob_end_flush(); ?>

Answer

You're not executing the SELECT query. You need:

$sql= "SELECT * FROM users WHERE userId=".$_SESSION['user']; 
$result = $mysqli->query($sql);

Then change:

$result->fetch_assoc();

to

$userRow = $result->fetch_assoc();

Then you can use things like $userRow['userName'] and $userRow['userCoins'] to show information about the user.

You should also check if the query found anything, like:

if (!$userRow) {
    die("userID {$_SESSION['user']} not found!");
}

The code that performs the SELECT query and sets $userRow should not be in the if(!$condition) block, so that you'll see the user information when you view the page before submitting the form.

Comments