简体   繁体   English

MySQLI从数据库回显数据

[英]MySQLI echo data from database

I am creating a website and I am using mySqli to connect to the database. 我正在创建一个网站,并且正在使用mySqli连接到数据库。 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. 我问过其他人,他们认为这很奇怪(我们花了大约1个小时来修复它),因此任何帮助将不胜感激。

postimg.org/image/qqv4vmtf7 postimg.org/image/rp6hhz8ht 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(); ?>

You're not executing the SELECT query. 您没有执行SELECT查询。 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. 然后,您可以使用$userRow['userName']$userRow['userCoins']来显示有关用户的信息。

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. 执行SELECT查询并设置$userRow不应位于if(!$condition)块中,以便在提交表单之前查看页面时会看到用户信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM