Probléme avec mysqli

Résolu
cs_youma85 Messages postés 82 Date d'inscription dimanche 18 février 2007 Statut Membre Dernière intervention 28 avril 2011 - 25 juil. 2008 à 16:48
cs_youma85 Messages postés 82 Date d'inscription dimanche 18 février 2007 Statut Membre Dernière intervention 28 avril 2011 - 26 juil. 2008 à 22:35
salut tous le monde j'ai un probléme avec la fonction mysqli, la connexion marche avec mysqli_connect(.....) mais le probléme se pose mysqli_free_result et  mysqli_num_rows voila les erreurs:

Warning: mysqli_affected_rows() expects parameter 1 to be mysqli, boolean given in C:\WebPHP\site1\results.php on line 35

Warning: mysqli_free_result() expects parameter 1 to be mysqli_result, boolean given in C:\WebPHP\site1\results.php on line 48

et voila le code :

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Search result</title>
</head>

<center> Book-O-Rama search result
</center>
<?php
    $searchtype=$_POST['searchtype'];
    $searchterm=$_POST['searchterm'];
   
    $searchterm=trim($searchterm);
    if(!$searchterm||!$searchtype)
    {   
        echo 'You have not enetered details, Please go back and try again.';
        exit;
    }
    if(!get_magic_quotes_gpc())
    {   
        $searchterm=addslashes($searchterm);
        $searchtype=addslashes($searchtype);
    }
    @ $db=mysqli_connect('localhost','youma85','dragon','bookorama');
   
    if(mysqli_connect_errno())
    {
        echo 'error: could not connect to database. Please try again later';
        exit;
    }
   
    $result=mysqli_query($db,"select * from books where ".$searchtype."like %".$searchterm."%");
   
    $num_result=mysqli_num_rows($result);
   
   
    echo ' number of books found:'.$num_result.'

';
   
    for($i=0;$i<$num_result;$i++)
    {
        $row=$result->fetch_assoc();
        echo ''.($i+1).'.Title: '.htmlspecialchars(stripslaches($row['title']));
        echo '
Author:'.stripslaches($row['author']).'
ISBN: ';
        echo stripslaches($row['isbn']).'
Price: '.stripslaches($row['price']).'

</html>

merci.
A voir également:

3 réponses

codefalse Messages postés 1123 Date d'inscription mardi 8 janvier 2002 Statut Modérateur Dernière intervention 21 avril 2009 1
26 juil. 2008 à 21:53
Déjà, au lieu de t'embetter à ajouter une variable inutile, remplace
    $num_result=$result->num_rows;
   
   
    echo ' number of books found:'.$num_result.'

';

par
    echo ' number of books found:'.$result->num_rows.'

';

Ensuite, essaye ca dans ton query :
'select * from books where '.$searchtype.' like "%'.$searchterm.'%"'

Es-tu sur que $searchtype à bien un espace à la fin, car dans ta requete, ca fait $searchtype.'like' ce qui aurait donnée valeur_de_search_typelike, ca va pas ! :p
Du coup c'est peut-être pour ca que tu as ces erreurs ensuite, la requete est invalide, donc $result n'est pas un objet, donc $result->num_rows te retourne cette erreur.

http://www.ReFlectiv.Net
3
cs_youma85 Messages postés 82 Date d'inscription dimanche 18 février 2007 Statut Membre Dernière intervention 28 avril 2011 1
25 juil. 2008 à 17:03
j'ai modifier mon code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Search result</title>
</head>

<center> Book-O-Rama search result
</center>
<?php
    $searchtype=$_POST['searchtype'];
    $searchterm=$_POST['searchterm'];
   
    $searchterm=trim($searchterm);
    if(!$searchterm||!$searchtype)
    {   
        echo 'You have not enetered details, Please go back and try again.';
        exit;
    }
    if(!get_magic_quotes_gpc())
    {   
        $searchterm=addslashes($searchterm);
        $searchtype=addslashes($searchtype);
    }
    @ $db=new mysqli('localhost','youma85','','bookorama');
   
    if(mysqli_connect_errno())
    {
        echo 'error: could not connect to database. Please try again later';
        exit;
    }
   
    $result=$db->query('select * from books where '.$searchtype.'like \'%'.$searchterm.'%\'');
   
    $num_result=$result->num_rows;
   
   
    echo ' number of books found:'.$num_result.'

';
   
    for($i=0;$i<$num_result;$i++)
    {
        $row=$result->fetch_assoc();
        echo ''.($i+1).'.Title: '.htmlspecialchars(stripslashes($row['title']));
        echo '
Author:'.stripslashes($row['author']).'
ISBN: ';
        echo stripslashes($row['isbn']).'
Price: '.stripslashes($row['price']).'

free();
    mysqli_close($db);
?>

</html>

j'ai travaillé en poo les erreurs précédents ne se pose plus mais il 'y a d'autre.:

Notice: Trying to get property of non-object in C:\WebPHP\site1\a.php on line 35
number of books found:

Fatal error: Call to a member function free() on a non-object in C:\WebPHP\site1\a.php on line 48

 

MAIS SI DANS LA REQUETE J'ENLEVE  LA PARTIE where '.$searchtype.'like \'%'.$searchterm.'%\' IL N'Y AURA PLUS D'ERREUR MAIS IL AFFICHE TOUS LES ENREGISTREMENTS DE LA TABLE MERCI
0
cs_youma85 Messages postés 82 Date d'inscription dimanche 18 février 2007 Statut Membre Dernière intervention 28 avril 2011 1
26 juil. 2008 à 22:35
merci codefalse, je vient de trouver l'erreur dans la table j'ai écrit books je devais écrir BOOKS et en plus ce que tu m'a dit
0
Rejoignez-nous