如何使用我的用户名来标识特定用户,并从我的数据库中为某个用户调用和回显值

问题描述 投票:0回答:1

我在数据库中有一个名为users的表,该表应该在其中的两行中保持BTC和Cash余额我有一个php文件,我使用该文件使用用户名来回显每个用户的值,以标识特定用户,但是由于某些原因,回显的数据包含BTC和Cash值的整个行,而不是使用用户名来标识用户并仅在BTC和现金栏中显示要在我的网站上显示余额的值。因此,我基本上想要的是从我的数据库中调用数据并使用用户名标识用户并显示用户值的代码。

这是html,用于在我的网站上显示用户的余额

<!doctype html>
<html> 
<div class="logged-user-w text-center avatar-inline">
        <div class="logged-user-info-w">
    <div class="logged-user-name">
        <a class="text-primary" href="profile.php"><?php echo $_SESSION['username']; ?></a>

    </div>
//BTC balance//
    <div class="logged-user-role">
            <a class="text-grey" href="wallet.php"><?php include('new.php')?></a>
    </div>
//Cash balance//
    <div class="logged-user-role">
            <a class="text-grey" href="cashwallet.php"><?php include('new.php')?></a>
     </div>

</div>
</html>

这里是php(new.php),应该从我的数据库中获取值并回显它

<?php

      // Server credentials 
$servername = "localhost";
$username = "coinrksv_user";
$password = "Open1mind";
$dbname = "coinrksv_coin";

// Creating mysql connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Checking mysql connection
if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
}

// Writing a mysql query to retrieve data 
$sql = "SELECT * FROM users bitcoin, cash";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
  // Show each data returned by mysql
  while($row = $result->fetch_assoc()) {
?>

    <!-- USING HTML HERE : Here I am using php within html tags -->
    <p> <?php echo $row["bitcoin"]; ?></p>
    <p> <?php echo $row["cash"]; ?></p>
<?php
  }
} else {
  echo "0 results";
}

// Closing mysql connection
$conn->close();
?>
javascript php html
1个回答
1
投票

您的SQL查询错误,将其更改为

$sql = "SELECT * FROM users";

或者,如果您只想要比特币和现金之类的字段,则可以将查询写为:

$sql = "SELECT bitcoin,cash FROM users";

然后回显相应的变量。]​​>

© www.soinside.com 2019 - 2024. All rights reserved.