swift
swift

Reputation: 53

how to call and echo a value from my database for a a certain user using username to identify the user

I have a table named users on my database which is suppose to hold balance in BTC and Cash in two of its rows I have a php file which i have used to echo the value for each user using the user's username to identify the particular user, but for some reason the echoed data contains the whole row for BTC and Cash value instead of identifying the user using the username and displaying only the value in the column for BTC and Cash where it suppose to display the balance on my website. So what i am basically looking for is the code to call the data from my database and using username to identify user, display the value for the user.

This is the html where it suppose to display the user's balance on my website

<!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>

hare is the php (new.php) which is suppose to get the values from my database and echo it

<?php

      // Server credentials 
$servername = "";
$username = "";
$password = "";
$dbname = "";

// 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();
?>

Upvotes: 0

Views: 658

Answers (2)

JLDN Admin
JLDN Admin

Reputation: 131

As stated by Kunal Raut, change your sql statement, but if you want to select the data based on the username, then use the following.

$sql = "SELECT bitcoin, cash FROM users WHERE username=$_SESSION[‘USERNAME’]”

Or use the user id.

$sql = "SELECT bitcoin, cast FROM users WHERE userid= $_SESSION[‘USERID’]

In both examples, you would need to change the field name to match what you used in the database to hold either the username or the user id.

Upvotes: 0

Kunal Raut
Kunal Raut

Reputation: 2584

Your sql query is wrong change it to

$sql = "SELECT * FROM users";

Or if you just want the fields like bitcoin and cash you may write a query as

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

And then echo the respective variables.

Upvotes: 1

Related Questions