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