Here in the below mentioned program, I have written an SQL query, which displays the amount remaining. I have checked the query, it is working correctly. I think there's an issue with php program written, I am unable to find the problem.
Thanks for reading my problem.
<?php
$sql2 = "SELECT AMOUNT_REMAINAING from MAIN_TABLE ORDER BY AMOUNT_REMAINAING DESC LIMIT 1";
$query2 = $dbh -> prepare($sql2);
$query2->execute();
$results2=$query2->fetchAll(PDO::FETCH_OBJ);
?>
<div class="stat-panel-number h1 "><?php echo htmlentities($results2[0]); ?></div>
<div class="stat-panel-title text-uppercase">Budget Remaining</div>
<a href="manage-vehicles.php" class="block-anchor panel-footer text-center">Full Detail <i class="fa fa-arrow-right"></i></a>
EDIT 1:
EDIT 2: After solution by Tim Biegeleisen, the result is same
With Paul Coldrey's solution this is the result
EDIT 3: Thats what I am getting by print_r(results2);
The fetchAll
function will return an array of all results for the single AMOUNT_REAMINING
column which you selected. Since you also added LIMIT 1
, this array should only have (at most) one item. So, just access that first item:
$results2[0]
Your updated HTML code:
<div class="stat-panel-number h1 "><?php echo htmlentities($results2[0]); ?></div>
<div class="stat-panel-title text-uppercase">Budget Remaining</div>
Since fetchAll returns an array I would expect you need a [0] to get the first row. Then you will need another [0] to get the first column. Also the FETCH_OBJ will give you back an object but you really just want the value. Hence I think you want:
<?php
$sql2 = "SELECT AMOUNT_REMAINAING from MAIN_TABLE ORDER BY AMOUNT_REMAINAING DESC LIMIT 1 ";
$query2 = $dbh -> prepare($sql2);
$query2->execute();
$results2=$query2->fetchAll()[0][0];
?>
you can also explicitly select only the first column like this:
$query2->fetchAll(PDO::FETCH_COLUMN, 0);
And then you would need to choose the first row with a [0], so the line would be
$results2=$query2->fetchAll(PDO::FETCH_COLUMN, 0)[0];
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.