0

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(); ?>
12
  • 1
    You need to call $result->fetch_assoc() to get a row of results. Any tutorial on using mysqli should show this. Commented Nov 14, 2016 at 19:46
  • Where do you set $userRow? Commented Nov 14, 2016 at 19:47
  • Look edit, I still don't know how to echo the column to the page. Commented Nov 14, 2016 at 19:55
  • You need to assign it to something, like $userRow = $result->fetch_assoc(); Commented Nov 14, 2016 at 19:55
  • If you'd bothered to read a single tutorial, or the examples in the PHP documentation, you'd have seen this. Commented Nov 14, 2016 at 19:56

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

12 Comments

I have done everything on here but i am still receiving nothing.
That can't be. fetch_assoc() returns either an array or FALSE, it never returns NULL.
That's $userRow['userKey'], not var_dump($userRow).
No it isn't take that wasn't doing anything if you look
The image shows the line with <h3>Welcome, <?php echo $userRow['userName']; ?>. You Currently Have <span id="services"><?php echo $userRow['userKey']; ?></span> Service Coins</h3>, so NULL is where echo $userRow['userKey']; is.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.