Page 1 sur 1

Erreur mysql -> mysql_fetch_assoc() et mysql_free_result(

Posté : 01 avr. 2009, 23:07
par djtec
Bonsoir,

Voici les deux erreurs qui s'affiche constamment et qui fait que rien ne s'affiche:
Warning: mysql_fetch_assoc(): 17 is not a valid MySQL result resource in C:\wamp\www\v4\includes\db\mysql.php on line 44

Warning: mysql_free_result(): 17 is not a valid MySQL result resource in C:\wamp\www\v4\includes\db\mysql.php on line 76


Et voici mon code de mysql.php:
class Mysql {
	
	private $sqlserver;
	private $sqluser;
	private $sqlpassword;
	private $database;
	
	public function __construct($sqlserver, $sqluser, $sqlpassword, $database){
		$this->user = $sqluser;
		$this->server = $sqlserver;
		$this->dbname = $database;
		
		$this->db_connect_id = mysql_connect($this->server, $this->user, $sqlpassword);

		mysql_select_db($this->dbname, $this->db_connect_id);
		
		return $this->db_connect_id;
	}
	
	public function __destruct() {
		mysql_close($this->db_connect_id);
	}
	
	public function sql_prepare($string) {
		return mysql_query($string);
	}
	
	public function sql_execute($type, $string) {
		switch($type) {
			case "fetch_assoc":
				$result = mysql_fetch_assoc($string); // Ligne 44
				$this->sql_memory($string);
				return $result;
			break;
		
			case "fetch_array":
				$result = mysql_fetch_array($string);
				$this->sql_memory($string);
				return $result;
			break;
			
			case "fetch_row":
				$result = mysql_fetch_row($string);
				$this->sql_memory($string);
				return $result;
			break;
			
			case "fetch_object":
				$result = mysql_fetch_object($string);
				$this->sql_memory($string);
				return $result;
			break;
			
			case "num_rows":
				$result = mysql_num_rows($string);
				$this->sql_memory($string);
				return $result;
			break;
	}
	
	public function sql_memory($string) {
		return mysql_free_result($string); // Ligne 76
	}
}
Et voici comment je m'en sert:
// Include Files	
include("db/config.inc.php");

// MySql
$db = new Mysql($sqlserver, $sqluser, $sqlpassword, $database);

//-------------------------------------------------
$sql = "select cat_id, cat_nom, f.forum_id, forum_name, forum_desc, forum_post, forum_topic, auth_view, t.topic_id, t.topic_post, post_id, post_time, post_createur, user_id, user_name ";
$sql .= "from ".TABLE_FFCATEGORY." c ";
$sql .= "left join ".TABLE_FFORUM." f on c.cat_id = f.forum_cat_id ";
$sql .= "left join ".TABLE_FPOST." p on p.post_id = f.forum_last_post_id ";
$sql .= "left join ".TABLE_FTOPIC." t on t.topic_id = p.topic_id ";
$sql .= "left join ".TABLE_USERS." u on u.user_id = p.post_createur ";
$sql .= "where auth_view < ".$level." "; 
$sql .= "order by cat_ordre, forum_ordre desc";

if(!$req = $db->sql_prepare($sql)) {
	$db->sql_error(__FILE__, __LINE__, mysql_error());
}

if($db->sql_execute("num_rows", $req) > 0) {
	while($row = $db->sql_execute("fetch_assoc", $req)) {
		if($category != $row["cat_id"]) {
			$category = $row["cat_id"];
			
			$tpl->assign_block_vars('headc', array(
				'L_CATEGORY' => $row["cat_name"],
				'L_TOPIC' => $lang["Forum"]["Topic"],
				'L_POST' => $lang["Forum"]["Post"],
				'L_LAST_POST' => $lang["Forum"]["Last_post"]
			));
		}
		
		$tpl->assign_block_vars('headc.headf', array(
			'F_ID' => $row["forum_id"],
			'F_NAME' => $row["forum_name"],
			'F_DESC' => $row["forum_desc"],
			'F_TOPIC' => $row["forum_topic"],
			'F_POST' => $row["forum_post"],
			'F_LAST_POST' => sprintf($lang["Forum"]["Last_post2"], $row["user_id"], $row["user_name"], date("d M Y", $row["post_time"]), date("H:i", $row["post_time"]))
		));
	}
}
Et voici la structure des tables:

Code : Tout sélectionner

CREATE TABLE IF NOT EXISTS `forum_categorie` ( `cat_id` int(11) NOT NULL AUTO_INCREMENT, `cat_nom` varchar(30) CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL, `cat_ordre` int(11) NOT NULL, PRIMARY KEY (`cat_id`), UNIQUE KEY `cat_ordre` (`cat_ordre`) ) ENGINE=MyISAM; CREATE TABLE IF NOT EXISTS `forum_forum` ( `forum_id` int(11) NOT NULL AUTO_INCREMENT, `forum_cat_id` mediumint(8) NOT NULL, `forum_name` varchar(30) CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL, `forum_desc` text CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL, `forum_ordre` mediumint(8) NOT NULL, `forum_last_post_id` int(11) NOT NULL, `forum_topic` mediumint(8) NOT NULL, `forum_post` mediumint(8) NOT NULL, `auth_view` tinyint(4) NOT NULL, `auth_post` tinyint(4) NOT NULL, `auth_topic` tinyint(4) NOT NULL, `auth_annonce` tinyint(4) NOT NULL, `auth_modo` tinyint(4) NOT NULL, PRIMARY KEY (`forum_id`) ) ENGINE=MyISAM; CREATE TABLE IF NOT EXISTS `forum_post` ( `post_id` int(11) NOT NULL AUTO_INCREMENT, `post_createur` int(11) NOT NULL, `post_texte` text CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL, `post_time` int(11) NOT NULL, `topic_id` int(11) NOT NULL, `post_forum_id` int(11) NOT NULL, PRIMARY KEY (`post_id`) ) ENGINE=MyISAM; CREATE TABLE IF NOT EXISTS `forum_topic` ( `topic_id` int(11) NOT NULL AUTO_INCREMENT, `forum_id` int(11) NOT NULL, `topic_titre` char(60) CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL, `topic_createur` int(11) NOT NULL, `topic_vu` mediumint(8) NOT NULL, `topic_time` int(11) NOT NULL, `topic_genre` varchar(30) CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL, `topic_last_post` int(11) NOT NULL, `topic_first_post` int(11) NOT NULL, `topic_post` mediumint(8) NOT NULL, PRIMARY KEY (`topic_id`), UNIQUE KEY `topic_last_post` (`topic_last_post`) ) ENGINE=MyISAM; CREATE TABLE IF NOT EXISTS `users` ( `user_id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT, `user_active` tinyint(1) DEFAULT '1', `user_name` varchar(25) NOT NULL, `user_password` varchar(32) NOT NULL, `user_session_time` int(11) NOT NULL DEFAULT '0', `user_session_page` smallint(5) NOT NULL DEFAULT '0', `user_lastvisit` int(11) NOT NULL DEFAULT '0', `user_level` tinyint(4) DEFAULT '0', `user_timezone` decimal(5,2) NOT NULL DEFAULT '0.00', `user_style` tinyint(4) DEFAULT NULL, `user_lang` varchar(255) DEFAULT NULL, `user_dateformat` varchar(14) NOT NULL DEFAULT 'd M Y H:i', `user_viewemail` tinyint(1) DEFAULT NULL, `user_notify_tuto` tinyint(1) NOT NULL DEFAULT '0', `user_notify_gal` tinyint(1) NOT NULL DEFAULT '0', `user_notify_pm` tinyint(1) NOT NULL DEFAULT '0', `user_rank` int(11) DEFAULT '0', `user_avatar` varchar(100) DEFAULT NULL, `user_avatar_type` tinyint(4) NOT NULL DEFAULT '0', `user_email` varchar(255) DEFAULT NULL, `user_icq` varchar(15) DEFAULT NULL, `user_website` varchar(100) DEFAULT NULL, `user_from` varchar(100) DEFAULT NULL, `user_sig` text, `user_sig_bbcode_uid` char(10) DEFAULT NULL, `user_aim` varchar(255) DEFAULT NULL, `user_yim` varchar(255) DEFAULT NULL, `user_msnm` varchar(255) DEFAULT NULL, `user_interests` varchar(255) DEFAULT NULL, `user_actkey` varchar(32) DEFAULT NULL, `user_newpasswd` varchar(32) DEFAULT NULL, PRIMARY KEY (`user_id`) ) ENGINE=MyISAM;
J'ai cherché sur Google mais je ne vois toujours pas ce qui se passe quand je fais un echo $sql juste après la requête et que je la colle dans phpmyadmin la requête fonctionne.

Même si je remplace mysql_fetch_assoc() par mysql_fetch_array() ou mysql_fetch_row() ca change rien les deux erreurs sont toujours là.

Je tourne sous WampServer avec Php5 et MySql 5.0.51a

Merci d'avance...

Posté : 02 avr. 2009, 00:25
par Grummfy
hello,
alors,
1° erreur : il manque une accolade à la fin du switch.
2° : pas de méthode sql_error (je suppose que tu l'as supprimé pour l'exemple mais je préfère le signalé) et je suppose idem pour les constante TABLE_*
et level est indéfini ...
Donc :
define('TABLE_FFCATEGORY', 'forum_categorie');
define('TABLE_FFORUM', 'forum_forum');
define('TABLE_FPOST', 'forum_post');
define('TABLE_FTOPIC', 'forum_topic');
define('TABLE_USERS', 'users');

$level = 0;

//et ceci dans la class
	public function sql_error($f, $l, $e)
	{
		die('fichier : ' . $f . '<br />ligne n°' . $l . '<br />erreur : ' . $e);
	}
3° : sisql_error est appelé, le script doit s'arrêter là (enfin ne pas entrer dans le block) car le résultat sql ne sera pas valable

j'ai tester et je n'ai plus d'erreur mais n'ayant pas de contenu dans les tables ...

bref pour moi cela me semble bon

Posté : 02 avr. 2009, 18:38
par djtec
Bonjour

L'accolade à la fin du switch c'est une erreur quand j'ai recopié car elle y est dans mon fichier.

Voici ma fonction sql_error():
	public function sql_error($file, $line, $error) {
		global $tpl, $ExHtml, $ExPhp, $lang;

		$page_title = sprintf($lang["Title"]["Index"], "et oui");
		include("includes/page_header".$ExPhp);

		$tpl->set_filenames(array(
			'body' => 'fileset/error'.$ExHtml
		));

		$tpl->assign_vars(array(
			'MESSAGE_TITLE' => 'Erreur Générale',
			'MESSAGE_TEXT' => 'Fine: '.$file.'<br />Line: '.$line.'<br /><br />Error: '.$error
		));

		$tpl->pparse('body');

		include("includes/page_footer".$ExPhp);
		
		exit;
	}
Et mon level est déclaré comme ceci:
$level = (isset($_SESSION['level'])) ? intval($_SESSION['level']) : 1;
Et les constantes comme ceci:
define('TABLE_FFCATEGORY', 'forum_categorie');
define('TABLE_FFORUM', 'forum_forum');
define('TABLE_FPOST', 'forum_post');
define('TABLE_FTOPIC', 'forum_topic');
define('TABLE_USERS', 'users');
Je n'ai pas compris ta 3eme explication.

Pour du contenu en voici c'est tiré du script du siteduzero.com.

Code : Tout sélectionner

INSERT INTO `forum_categorie` (`cat_id`, `cat_nom`, `cat_ordre`) VALUES (1, 'Général', 30); INSERT INTO `forum_categorie` (`cat_id`, `cat_nom`, `cat_ordre`) VALUES (2, 'Jeux-Vidéos', 20); INSERT INTO `forum_categorie` (`cat_id`, `cat_nom`, `cat_ordre`) VALUES (3, 'Autre', 10); INSERT INTO `forum_forum` (`forum_id`, `forum_cat_id`, `forum_name`, `forum_desc`, `forum_ordre`, `forum_last_post_id`, `forum_topic`, `forum_post`, `auth_view`, `auth_post`, `auth_topic`, auth_annonce, auth_modo) VALUES (1, 1, 'Présentation', 'Nouveau sur le forum? Venez vous présenter ici !', 60, 0, 0, 0, 0, 0, 0, 0, 0); INSERT INTO `forum_forum` (`forum_id`, `forum_cat_id`, `forum_name`, `forum_desc`, `forum_ordre`, `forum_last_post_id`, `forum_topic`, `forum_post`, `auth_view`, `auth_post`, `auth_topic`, auth_annonce, auth_modo) VALUES (2, 1, 'Les News', 'Les news du site sont ici', 50, 0, 0, 0, 0, 0, 0, 0, 0); INSERT INTO `forum_forum` (`forum_id`, `forum_cat_id`, `forum_name`, `forum_desc`, `forum_ordre`, `forum_last_post_id`, `forum_topic`, `forum_post`, `auth_view`, `auth_post`, `auth_topic`, auth_annonce, auth_modo) VALUES (3, 1, 'Discussions générales', 'Ici on peut parler de tout sur tous les sujets', 40, 0, 0, 0, 0, 0, 0, 0, 0); INSERT INTO `forum_forum` (`forum_id`, `forum_cat_id`, `forum_name`, `forum_desc`, `forum_ordre`, `forum_last_post_id`, `forum_topic`, `forum_post`, `auth_view`, `auth_post`, `auth_topic`, auth_annonce, auth_modo) VALUES (4, 2, 'MMORPG', 'Parlez ici des MMORPG', 60, 0, 0, 0, 0, 0, 0, 0, 0); INSERT INTO `forum_forum` (`forum_id`, `forum_cat_id`, `forum_name`, `forum_desc`, `forum_ordre`, `forum_last_post_id`, `forum_topic`, `forum_post`, `auth_view`, `auth_post`, `auth_topic`, auth_annonce, auth_modo) VALUES (5, 2, 'Autres jeux', 'Forum sur les autres jeux', 50, 0, 0, 0, 0, 0, 0, 0, 0); INSERT INTO `forum_forum` (`forum_id`, `forum_cat_id`, `forum_name`, `forum_desc`, `forum_ordre`, `forum_last_post_id`, `forum_topic`, `forum_post`, `auth_view`, `auth_post`, `auth_topic`, auth_annonce, auth_modo) VALUES (6, 3, 'Loisir', 'Vos loisirs', 60, 0, 0, 0, 0, 0, 0, 0, 0); INSERT INTO `forum_forum` (`forum_id`, `forum_cat_id`, `forum_name`, `forum_desc`, `forum_ordre`, `forum_last_post_id`, `forum_topic`, `forum_post`, `auth_view`, `auth_post`, `auth_topic`, auth_annonce, auth_modo) VALUE

Posté : 28 avr. 2009, 11:49
par GiorgioLino
Bien que le post date un peu, j'apporte une suggestion au cas où le problème n'aurait pas encore été résolu.

Je penses que tout est dit dans le message d'erreur is not a valid MySQL result resource et à partir de là, je vois 2 possibilités :

1) MySQL n'aime pas que tu utilises "string" comme nom de variable --> Tester avec un autre nom.

2) MySQL n'aime pas que tu utilise la fonction mysql_fetch_assoc() sur un argument qui n'est pas explicitement de type 'result'.
Concrètement, cette fonction prendrait en argument le résultat de ta fonction sql_prepare().

P.S.
C'est assez étrange de voir définir une classe mysql, à moins que tu sois dans une environnement multi-serveurs (?).
Parce que dans le cas contraire php fournit une classe simplifiant l'accès aux données via l'extension PDO.
Tu pourrais t'en servir directement ou même faire hériter ta classe customisée de PDO.

http://fr.php.net/pdo