Hello there,

I created a "users" table on MySQL and created a few columns namely "accountbalance", "ledgerbalance" and "dailybalance" (all three: BIGINTs). I created a test user and added some random figures. I am trying to retrieve the value of this columns (unique to the user) and them pasted separately into different DIVs like so:

<div><strong>Account Balance:</strong> <?php echo $accountbalance?></div>
<div><strong>Ledger Balance:</strong> <?php echo $ledgerbalance?></div>
<div><strong>Daily Balance:</strong> <?php echo $dailybalance?></div>

I have tried everything I know on PHP and none seem to be working. I will show you a couple approaches I used to try getting it right, please someone should help with out on where I am getting wrong, I'm stuck. Thanks.

Approach 1: Using SESSIONS

<?php
include('db-config.php');
require_once('login.php');
session_start();

$MyConn = mysqli_connect ("localhost", "redacted", "R", "R");
        if($_SESSION['name']) // Is the name unique? Shouldn't it be better to use id?
{
$SESSION = $_SESSION['name'];
$result = mysqli_query($MyConn, "select * from `users` where 
`name` = '$SESSION'"); // name here refers to the Username
$show=mysqli_fetch_assoc($result); // spaces

/*the below echos where entered into the divs I intented to have them pasted*/
echo " $show['accountbalance']";
echo " $show['ledgerbalance']";
echo " $show['dailybalance']";
}
?>

This one returned nothing whatsoever.

Approach 2: The conventional one

<?php
include('db-config.php');
session_start();
require_once ('login.php'); 
$hostname = "localhost";
$database = "";
$username = "";
$password = "";

$MyConn = mysqli_connect($hostname, $username, $password, $database);
?>  
<?php
$query = "SELECT accountbalance, ledgerbalance, dailybalance FROM users";
$result = mysqli_query($MyConn, $query);
$num_results = mysqli_num_rows($result);
?>

<?php
/*Loop through each row and display records */
for($i=0; $i < $num_results; $i++) {
$row = $result->fetch_row()[$i] ?? false;
?>

Daily Remaining Balance: <?php print $row['dbalance']; ?> 
<br />
Monthly Remaining Balance: <?php print $row['mbalance']; ?>
<br />
Available Balance: <?php print $row['balance']; ?><br />

<?php 
// end loop
} 
?>

The above kept returning no value, in fact null (0) value. It was the closest I came to actually seeing something appear in the div even though it turned out to be an eventual logical error. I even used var_dump and I didn't even under what I was seeing.

Note: I pretty much a beginner so I am really at a lost on what the problem could be.

The Last Approach: Prepared Statement

I got much of this from StackOverflow and the website refused to login entirely, let alone have anything pasted in the divs.

I'm not sure if I used the correct one.

IMPORTANT NOTICE I intend to run this code on the dashboard: where the user gets redirected to when they login.

Please I need help, it's a school project and I'm at loggerheads with a deadline.

if you can suggest any other way of doing it: whether Javascript or JQuery/Ajax, it would also suffice and I'd really appreciate if anyone could spare me a code that could run. Thanks

Thanks

It's a good strategy to use var_dup() to spit out the output of a variable, to make sure you're on the right track.

Right after line 14, $result = mysqli_query($MyConn, $query);, then do var_dump($result); to make sure that you're actually correctly querying the database and getting a resultset. What does it print out when you do that?

For approach 2: I assume the issue is that $database, $username and $password is an empty string.

I see you replied to me personally. So that everyone else following this thread is in the loop, you've got it to a point where:

I think it returned a positive, error-free var_dump because this was the result: object(mysqli_result)#3 (5) { ["current_field"]=> int(0) ["field_count"]=> int(3) ["lengths"]=> NULL ["num_rows"]=> int(1) ["type"]=> int(0) }

Now that you have that, you need to loop through the result set.

You can now loop through the results with a while loop, instead of a for loop. This way you don't need to count the number of results first:

// Loop through each row in the result set
while ($row = mysql_fetch_assoc($result)) {
    // For each row ...
    echo 'Daily Remaining Balance: ' . $row['dbalance'] . '<br>';
    echo 'Monthly Remaining Balance: ' . $row['mbalance'] . '<br>';
    echo 'Available Balance: ' . $row['balance'] . '<br>';
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.