Adapter une page PhP / MYSQL en PHP / PDO
Bonjour,
J'essaie laborieusement de passer à PDO, en regardant sur les forums je me suis fait une petite idée mais lorsque j'essaye, ce n'est pas concluant :
voici ma page de connexion :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| <?php
try
{
// Connexion à la base de données
$bdd = new PDO('mysql:host=localhost;dbname=xxx', 'adxxx', 'mdpxxx');
$bdd->exec("SET CHARACTER SET utf8");
}
catch (Exception $e)
{
die('Erreur : ' . $e->getMessage());
}
// Configuration facultative de la connexion
//$db->setAttribute(PDO::ATTR_CASE, PDO::CASE_LOWER); // les noms de champs seront en caractères minuscules
//$db->setAttribute(PDO::ATTR_ERRMODE , PDO::ERRMODE_EXCEPTION); // les erreurs lanceront des exceptions
?> |
La page avec PHP / MYSQL
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
| <?php
include("include/variables.inc.php");
include("menu.php");
$liendb = mysql_connect($bddserver, $bddlogin, $bddpassword);
mysql_select_db ($bdd);
mysql_set_charset( 'utf8' );
?>
<link rel="stylesheet" href="style.css" type="text/css">
<link rel="stylesheet" href="menu/dropdown_three.css" type="text/css">
<p align="left"> :: Instruments</p>
<center>
<tr>
<form action="instrument_ajoute.php" method="post">
<td><input type="submit" value="Ajouter un instrument" /></td>
</form>
</tr>
</center>
<br />
<table class="bicolor" width="40%" align="center" border="1">
<tr>
<th>Instrument</th>
<td class="intitule"> </td>
</tr>
<?php
$sql = "SELECT IDInstruments, Instrument FROM Instruments ORDER by Instrument";
$resultat = mysql_query ($sql);
while ($Instruments = mysql_fetch_array ($resultat))
{
$id = $Instruments['IDInstruments'];
$Instrument = $Instruments['Instrument'];
echo "<tr>";
echo "<td>$Instrument</td>";
echo "<td>";
echo "<a href=instrument_edite.php?id=$id>Editer</a>";
echo "</td>";
echo "</tr>";
}
echo "</table>";
?>
<?php
mysql_close($liendb);
?> |
et ce que j'ai tenté de faire avec PHP / PDO
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
| <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>
<body>
<link rel="stylesheet" href="style.css" type="text/css">
<link rel="stylesheet" href="menu/dropdown_three.css" type="text/css">
<p align="left"> :: Instruments</p>
<center>
<tr>
<form action="instrument_ajoute.php" method="post">
<td><input type="submit" value="Ajouter un instrument" /></td>
</form>
</tr>
</center>
<br />
<table class="bicolor" width="40%" align="center" border="1">
<tr>
<th>Instrument</th>
<td class="intitule"> </td>
</tr>
<?php
require 'include/sqlconnect.php';
$sql = $bdd->prepare("SELECT * FROM Instruments ORDER by Instrument";
$sql->execute();
$req = $bdd->query($sql);
while($row = $req->fetch()) {
echo "<tr>";
echo "<td>$row['$Instrument']</td>";
echo "<td>";
echo "<a href=instrument_edite.php?id=$id>Editer</a>";
echo "</td>";
echo "</tr>";
}
echo "</table>";
$req->closeCursor();
?> |
Cela ne fonctionne pas :?
Ce n'est pas la page de connexion qui est en cause, car j'ai testé avec cet exemple trouvé sur un forum, et cela fonctionne :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| <?php
require 'include/sqlconnect.php';
$ins = $bdd->prepare("SELECT * FROM Personnes
INNER JOIN Instruments
ON Personnes.IDInstruments = Instruments.IDInstruments
ORDER BY Nom_Personne, Prenom_Personne");
$ins->setFetchMode(PDO::FETCH_ASSOC);
$ins->execute();
// $tab = $ins->FETCH_ASSOC();
$tab = $ins->fetchAll();
for($i=0;$i<count($tab);$i++){
echo implode(" | ",$tab[$i])."<br />";
}
for($i=0;$i<count($tab);$i++){
echo $tab[$i]["IDPersonne"]." | ".
$tab[$i]["Nom_Personne"]." | ".
$tab[$i]["Prenom_Personne"]." | ".
$tab[$i]["Instrument"]." | ".
$tab[$i]["Paiement"]." | ".
$tab[$i]["Motet"]."<br />";
}
$count = $bdd->query("SELECT count(*) FROM Personnes")->fetchColumn();
/*
$sql = 'SELECT * FROM Personnes ORDER BY Nom_Personne, Prenom_Personne';
$req = $bdd->query($sql);
while ($row = $req->fetch()){
echo $row['Prenom_Personne'].' '.$row['Nom_Personne'].'<br>';
}
*/
//$req->closeCursor();
$ins->closeCursor();
?> |
Pouvez-vous maider s.v.p. j'ai plusieurs pages à afficher sous la former utilisée précédement avec PHP / MYSQL et si j'arrive à faire celle-ci, pour les autres cela ira tout seul :lol:
Merci d'avance