Je me fait une class pour le connecter à MySql, afficher des données, ajouter, update etc...
J'ai une erreur dont je ne connais pas la source.
Voici mon code :
<?php
class connect {
public $connect;
public $query;
public function __construct($host, $user, $password, $database) {
$this->connect = mysql_connect($host, $user, $password);
mysql_select_db($database, $this->connect);
}
public function query($query, $i = '0') {
$this->query[$i] = mysql_query($query, $this->connect);
}
public function fetch($i = '0', $type = 'object') {
switch ($type)
{
case 'row':
$fetch = mysql_fetch_row($this->query[$i]);
break;
case 'array':
$fetch = mysql_fetch_array($this->query[$i]);
break;
case 'assoc':
$fetch = mysql_fetch_assoc($this->query[$i]);
break;
case 'object':
$fetch = mysql_fetch_object($this->query[$i]);
break;
}
return $fetch;
}
public function close() {
mysql_close($this->connect);
}
}
// CETTE PARTIE EST SIMPLEMENT LÀ POUR TESTER LA CLASSE
function nl2p($str,$addtag='') {
return str_replace('<p'.$addtag.'></p>', '', '<p'.$addtag.'>' . preg_replace('#\n|\r#', '</p>$0<p'.$addtag.'>', $str) . '</p>');
}
require_once("config.php");
$db = new connect( $host, $user, $password, $database );
$db->query("SELECT * from nouvelles ORDER BY 'id' DESC LIMIT 10");
$db->query("SELECT * from nouvelles ORDER BY 'id' DESC LIMIT 1", 1);
$db->query("UPDATE `nouvelles` SET `auteur` = 'endorphine!!!!' WHERE `id` = 31 LIMIT 1", 2);
$db->fetch(2, 'assoc');
$db->close();
while ($r = $db->fetch()) {
echo "<h2>" . $r->titre . "</h2>\n";
echo nl2p($r->nouvelle) . "\n\n";
}
echo "<br />";
echo "<hr />";
echo "<br />";
$r = $db->fetch(1);
echo "<h2>" . $r->titre . "</h2>";
echo "<p>" . nl2br($r->nouvelle) . "</p>";
?>
Voici l'erreur :
Merci d'avance pour votre aide!!Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in C:\wamp\www\connect.php on line 28