Reputation: 53
I want to get a row from db and show in my page. Following code doesn't work and my browser hangs. and i don't know how fix it. Please help me.
Class :
public function get_calculate($id)
{
if ($id != 0) {
$stmt_select_calculate = $this->connect()->prepare('SELECT analix_calculate.title,analix_calculate.basis FROM analix_calculate where analix_calculate.id=:calculate_id AND analix_calculate.is_active=1;');
$stmt_select_calculate->bindValue(':calculate_id', $id, PDO::PARAM_STR);
} else {
$stmt_select_calculate = $this->connect()->prepare('SELECT analix_calculate.title,analix_calculate.basis FROM analix_calculate where analix_calculate.is_active=1;');
}
return $stmt_select_calculate->execute();
}
index.php :
<?php
include("../db/Database.php");
$databse = new Database();
$menu = $databse->get_calculate(0);
while ($row_select_calculate = $menu) {
?>
<li>
<a href="#0">
<i class="fa fa-home" aria-hidden="true"></i><?php echo $row_select_calculate['title']?>
</a>
</li>
<?php
}
?>
Upvotes: 0
Views: 36
Reputation: 54841
First of all, $stmt_select_calculate->execute();
returns bool value. You cannot iterate over bool value, it's obvious. So, you should return statement object itself:
public function get_calculate($id)
{
if ($id != 0) {
$stmt_select_calculate = $this->connect()->prepare('SELECT analix_calculate.title,analix_calculate.basis FROM analix_calculate where analix_calculate.id=:calculate_id AND analix_calculate.is_active=1;');
$stmt_select_calculate->bindValue(':calculate_id', $id, PDO::PARAM_STR);
} else {
$stmt_select_calculate = $this->connect()->prepare('SELECT analix_calculate.title,analix_calculate.basis FROM analix_calculate where analix_calculate.is_active=1;');
}
$stmt_select_calculate->execute();
return $stmt_select_calculate;
}
In index.php
you should fetch
data:
$menu_stmt = $databse->get_calculate(0);
while ($row = $menu_stmt->fetch()) {
?>
<li>
<a href="#0">
<i class="fa fa-home" aria-hidden="true"></i><?php echo $row['title']?>
</a>
</li>
<?php
}
Upvotes: 1