Bonjour,
j'ai l'erreur suivante :
Warning: PDO::query(): SQLSTATE[3D000]: Invalid catalog name: 1046 No database selected in C:\xampp\htdocs\Sites\functions\home.func.php on line 7
Fatal error: Call to a member function fetchObject() on a non-object in C:\xampp\htdocs\Sites\functions\home.func.php on line 11
alors que j'ai suivi le tuto a la letter.
voici mon code :
home.func.php :
<?php
function get_posts(){
global $db;
$req = $db->query("
SELECT * FROM posts
");
$results = array();
while($rows = $req->fetchObject()){
$results[] = $rows;
}
return $results;
}
?>
main-functions.php :
<?php
$dbhost = 'localhost';
$dbname = 'blog';
$dbuser = 'root';
$dbpswd = '';
try{
$db = new PDO('mysql:host='.$dbhost.';$dbname='.$dbname,$dbuser,$dbpswd, array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8', PDO::ATTR_ERRMODE => PDO::ERRMODE_WARNING));
}catch(PDOException $e){
die("une erreur est survenue lors de la connexion a la base de donnees");
}
?>
NicwalleTu as mis un $ devant le dbname...
Le 07/02/2016 à 11:09
Il ne faut pas mettre ça:
mysql:host='.$dbhost.';$dbname='.$dbname,$dbuser,$dbpswd
Mais ça:
mysql:host='.$dbhost.';dbname='.$dbname,$dbuser,$dbpswd
Taklichd'accord merci
Le 09/02/2016 à 22:42