Fatal error: in C:\xampp\htdocs\libs\Database.php on line 7

Fermé
permet Messages postés 41 Date d'inscription jeudi 6 janvier 2011 Statut Membre Dernière intervention 10 juin 2015 - Modifié par jordane45 le 10/06/2015 à 16:21
jordane45 Messages postés 38145 Date d'inscription mercredi 22 octobre 2003 Statut Modérateur Dernière intervention 25 avril 2024 - 10 juin 2015 à 16:27
Bonjour,

J'ai une erreur qui s'affiche aléatoirement sur ma page internet et je ne vois pas d'ou vient l'erreur.

Voici l'erreur: Fatal error: in C:\xampp\htdocs\libs\Database.php on line 7

en image:


voici le code de Database.php:

<?php

class Database extends PDO
{
 public function __construct($DB_TYPE, $DB_HOST, $DB_NAME, $DB_USER, $DB_PASS)
 {
  parent::__construct($DB_TYPE.':host='.$DB_HOST.';dbname='.$DB_NAME, $DB_USER, $DB_PASS);

  // parent::setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTIONS);
 }

 /**


* select  * @param  string $sql An SQL string  * @param  array $array Parameters to bind  * @param  constant $fetchMode A PDO Fetch mode  * @return mixed  */ 
 public function select($sql, $array = array(), $fetchMode = PDO::FETCH_ASSOC) { 
 $sth = $this->prepare($sql);  foreach ($array as $key => $value)   {   
$sth->bindValue(":$key", $value);  
} 
 $sth->execute();  
return $sth->fetchAll($fetchMode); 
} 

/**  * get_value of a row  * @param  string $sql       Sql string  * @param  array  $array     parameters to bind  * @param  constant $fetchMode A PDO fetch mode  * @return mixed            Return a value  */ 
public function get_value($sql, $array = array(), $fetchMode = PDO::FETCH_ASSOC) { 
 $sth = $this->prepare($sql); 
 foreach ($array as $key => $value)   {   
$sth->bindValue(":$key", $value);  
}  $sth->execute(); 
 return $sth->fetchColumn(0); 
}

  /**  * insert  * @param  string $table A name of table to insert into  * @param  string $data An associative Array  *   */
 public function insert($table, $data) { 
 ksort($data);  
$fieldNames = implode('`, `', array_keys($data));  
$fieldValues = ':' . implode(', :', array_keys($data)); 
 $this->exec("set names utf8");  
$sth = $this->prepare("INSERT INTO $table (`$fieldNames`) VALUES ($fieldValues)"); 
 foreach ($data as $key => $value)   {  
 $sth->bindValue(":$key", $value); 
 } 
 $sth->execute(); } 

/**  * update  * @param  string $table A name of table to insert into  * @param  string $data An associative Array  * @param  string $where the WHERE query part  *   */
 public function update($table, $data, $where) {  
ksort($data);  
$fieldDetails = null;  
foreach ($data as $key => $value)   {   
$fieldDetails .= "`$key` = :$key, "; 
 }  
$fieldDetails = rtrim($fieldDetails, ', '); 
 $sth = $this->prepare("UPDATE $table SET $fieldDetails WHERE $where");  
foreach ($data as $key => $value)   { 
  $sth->bindValue(":$key", $value); 
 }  $sth->execute(); } 

/**  * delete  * @param  string  $table  * @param  string  $where  * @param  integer $limit  * @return integer Affected Rows  */ 
 public function delete($table, $where, $limit = 1) {
  return $this->exec("DELETE FROM $table WHERE $where LIMIT $limit"); }
}
?>


Petite idée ?

Cordialement

EDIT : Ajout du langage dans les balises de code
EDIT2 : Remise en forme du code

2 réponses

permet Messages postés 41 Date d'inscription jeudi 6 janvier 2011 Statut Membre Dernière intervention 10 juin 2015
10 juin 2015 à 16:18
petit up svp merci
0
jordane45 Messages postés 38145 Date d'inscription mercredi 22 octobre 2003 Statut Modérateur Dernière intervention 25 avril 2024 4 650
10 juin 2015 à 16:27
Bonjour,

La ligne indiquée dans le message d'erreur est :
 parent::__construct($DB_TYPE.':host='.$DB_HOST.';dbname='.$DB_NAME, $DB_USER, $DB_PASS);


... première chose à vérifier .. c'est ... est-ce que tes variables contiennent bien les bonnes informations ?
Peux tu en faire des ECHO histoire de vérifier ??

Ensuite... pour gérer correctement les erreurs... ajoutes donc un bloc TRY / CATCH

 public function __construct($DB_TYPE, $DB_HOST, $DB_NAME, $DB_USER, $DB_PASS) {
 try{
  parent::__construct($DB_TYPE.':host='.$DB_HOST.';dbname='.$DB_NAME, $DB_USER, $DB_PASS);
   // parent::setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTIONS);
} catch (PDOException $e) {
    echo 'Connexion échouée : ' . $e->getMessage();
}


0