Fatal error: Call to a member function fetch() on boolean in C:\wamp64\www\Jeux\index.php on line 54
Voici mon code entier :
Code à la connexion de la base de donnée :
<?php
$username = "root";
$password = "";
$hostname = "localhost";
$namebase = "jeux";
// on tente la connexion à la base de donnée
try
{
$db = new PDO('mysql:host='.$hostname.';dbname='.$namebase.'', $username, $password);
}
catch (Exception $e)
{
// En cas d'erreur, on affiche un message et on arrête tout
die('Erreur : ' . $e->getMessage());
}
// Si tout va bien, on peut continuer
echo "la connexion a reussie.";
?>
Mon script :
<?php
if(isset($_GET['pp']) && !empty($_GET['pp']) && ctype_digit($_GET['pp']) == 1){
$page = $_GET['pp'];
}else{
$page = 5;
}
$req = $db->query("SELECT COUNT(*) AS total FROM items_hippique");
$resultat = $req -> fetch();
$total = $resultat['total'];
$nbPage = ceil($total/$page);
echo $nbPage;
if (isset($_GET['p']) && !empty($_GET['p']) && ctype_digit($_GET['p']) == 1){
if($_GET['p'] > $nbPage){
$current=$nbPage;
}else{
$current=$_GET['p'];
}
}else{
$current=1;
}
$page1 = ($current-1)*$page;
$reqItem = $db->query("SELECT * FROM items_hippique ORDER BY id ASC LIMIT $page1, $page");
var_dump($reqItem);
?>
Et voici mon code index.php où j'ai l'erreur à la ligne 54 ainsi j'ai aussi d'autre erreur $reqItem et $current me dit que c'est des variables qui ne sont pas définis alors que j'inclus mes fichiers php.
<?php
include ("connection.php");
include ("script.php");
?>
<meta charset="utf-8">
<!DOCTYPE html>
<html lang="fr">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<form method="get">
<label>Nombre d'item par page:</label>
<select name="pp">
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
</select>
<input type="hidden" name="p" value="<?php echo $current ?>">
<button class ="btn btn-primary btn-xs" type="submit">Appliquer</button>
</form>
<div class="container">
<h2>Bordered Table</h2>
<p>The .table-bordered class adds borders to a table:</p>
<table class="table table-bordered">
<thead>
<tr>
<th>#</th>
<th>Article</th>
<th>Prix</th>
</tr>
</thead>
<tbody>
<?php
while ($items = $reqItem->fetch()){
?>
<tr>
<td> <?php echo $items['nom']; ?></td>
<td> <?php echo $items['type']; ?></td>
<td> <?php echo $items['prix']; ?></td>
</tr>
<?php
}
?>
</tbody>
</table>
</div>
<ul class="pagination">
<li> class="<?php if($current == '1'){ echo "disabled"; } ?>" <a href="?p=<?php if($current != '1'){ echo $current-1; }else{ echo $current; }?>&&pp=<?php echo $_GET['pp']; ?>">«</a> </li>
<?php
for ($i=1; $i<=$nbPage; $i++){
if ($i == $current){
?>
<li class="active"><a href="?p=<?php echo $i?>&&pp=<?php echo $_GET['pp'];?>"<?php echo $i ?></a></li>
<?php
} else {
?>
<li><a href="?p=<?php echo $i ?>&&pp=<?php echo $_GET['pp'];?>"<?php echo $i ?></a></li>
<?php
}
}
?>
<li> class="<?php if($current == $nbPage){ echo "disabled"; } ?>" <a href="?p=<?php if($current != $nbPage){ echo $current+1; }else{ echo $current; }?>&&pp=<?php echo $_GET['pp'];?>">»</a> </li>
</ul>
</body>
</html>
Merci beaucoup de votre aide !!
Code : Tout sélectionner