<?php
class sql
{
public $query = '';
public function __construct ($host, $user, $passwd, $db)
{
mysql_connect ($host, $user, $passwd) or die ('erreur!');
mysql_select_db ($db) or die ('erreur!');
return 0;
}
public function query ($type, $table, $champs, $where='', $order='', $limit=array(), $desc=FALSE, $values=array(), $update=array(), $update_keys=array())
{
if ($table == '') exit;
switch ($type)
{
case 'select':
$this->query = 'SELECT '.$champs.' FROM '.$table;
if ($where != '')
$this->query .= ' WHERE '.$where;
if ($order != '')
$this->query .= ' ORDER BY '.$order;
if ($desc == TRUE)
$this->query .= ' DESC ';
if ($limit != '')
$this->query .= 'LIMIT ('.$limit[0].', '.$limit[1].')';
break;
case 'delete':
if ($where == '') $where = 1;
$this->query = 'DELETE FROM '.$table.' WHERE '.$where;
break;
case 'insert':
$this->query = 'INSERT INTO '.$table.' VALUES (""';
foreach ($values as $value)
{
$this->query .= ', "'.$value.'"';
}
$this->query .= ')';
break;
case 'update':
foreach ($update as $item)
{
foreach ($update_keys as $key)
{
$this->query = 'UPDATE '.$table.' SET '.$item.' = "'.$key.'"';
}
}
break;
default:
exit;
}
return mysql_query($this->query) //LIGNE 60
}
private function __destruct()
{
mysql_close();
}
};
Ensuite, dans mon fichier "index.php", j'exécute la requête suivante:<?php
include 'options.php';
$pagetitle = 'accueil';
$body = '
<table width="100%" bgcolor="#f1f1f1" cellspacing="0" cellpadding="0" align="center">';
while ($sql = mysql_fetch_assoc ($db->query ('select', 'news', 'contenu, date', '', 'id_news', $limit=array(0,5),TRUE))) /*c'est la ligne 9*/
{
$body .= '<tr><td width="100%" align="justify" bgcolor="#f1f1f1">'.$sql['contenu'].'</td></tr>
<tr><td width="100%" align="right" bgcolor="#f1f1f1">'.$sql['date'].'</td></tr>';
}
$body .= '</table>';
$tpl->output ($pagetitle, $body);
?>
(les classes sont instanciées dans le fichier "options.php")Et voici l'erreur retournée:
J'ai fait un test pour affiché ma requête à partir du fichier "index.php" et elle est normalement constituée. Pas de problème non plus du côté des paramètres de connexion. Avez vous une idée?Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in D:\web\test\index.php on line 9
EDIT: j'ai fais un affichage de la requête et mysql_error() sur la ligne 60 qui me signale:
SELECT contenu, date FROM news ORDER BY id DESC LIMIT (0, 5)
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(0, 5)' at line 1