Moteur de recherche en php et concaténation requêt

Salut à tous , je suis un vrais debutant en php

J'essaie de faire un moteur de recherche pour mon site mais j'ai une erreur dans mon code que je n'ai pas su résoudre :


<?php

include("connexion.php");

$info=$conn->query('SELECT client,axe,cylindre,sphere,addition,axe1,cylindre1,sphere1,addition1,date FROM info ')or die(print_r($conn->erroInfo()));

if (isset($_GET['query']) AND !empty($_GET['query']))

{   

  $query=htmlspecialchars($_GET['query']);


$info=$conn->query=('SELECT client ,axe ,cylindre,sphere,addition,axe1,cylindre1,sphere1,addition1,date FROM info WHERE

 client,axe,cylindre,sphere,addition,axe1,cylindre1,sphere1,addition1,date LIKE "%'.$query.'%"' );

} 



?>

<!DOCTYPE html>
<html  >
<head>
  <title>Moteur de recherche</title>
</head>
<body  >

<form method="GET" >
   
 
  <h2 align="left" > 

  Entrer votre recherche: <input type="search" name="query" maxlength="80" size="80"  >


 <input type="submit" value="rechercher">


<ul>

 <?php while ( $a = $info->fetch()) { ?> 

<li>
  <?=$a['client']?> <?=$a['axe']?> <?=$a['cylindre']?> <?=$a['sphere']?> <?=$a['addition']?> <?=$a['axe1']?> <?=$a['cylindre1']?> <?=$a['sphere1']?> 

  <?=$a['addition1']?> <?=$a['date']?>
</li> 

<?php } ?> 


</ul>

 </h2>

</form>
 
</body>
</html>



voici le message d'erreur

Fatal error: Call to a member function fetch() on a non-object in C:\wamp\www\test\form_recherche.php on line 47

1 réponse

  1. Modérateur
    Bonjour,

    Le message d'erreur vient du fait que ta requête ne retourne rien.... car elle est fausse
    'SELECT client ,axe ,cylindre,sphere,addition,axe1,cylindre1,sphere1,addition1,date 
    FROM info 
    WHERE
     client,axe,cylindre,sphere,addition,axe1,cylindre1,sphere1,addition1,date LIKE "%'.$query.'%"'
    


    Regarde donc à quoi sert la fonction CONCAT
    https://www.w3schools.com/sql/func_mysql_concat.asp

    NB: Teste TOUJOURS tes requêtes DIRECTEMENT dans ta BDD (via PHPMyadmin par exemple) AVANT de vouloir les mettre dans ton code PHP.
    ça te permettra de savoir si elles fonctionnent ou non.
    1
    1. Modérateur
      De plus, je pense que tu cherches à faire :
      SELECT client ,axe ,cylindre,sphere,addition,axe1,cylindre1,sphere1,addition1,date 
      FROM info 
      WHERE
       client LIKE "%'.$query.'%"' 
      OR axe LIKE "%'.$query.'%"'
      OR cylindre LIKE "%'.$query.'%"'
       ...etc...
      
      0
    2. merci je s'essaye pour voir
      0
    3. j'ai arrangé le code un peu mais rien ne s'affiche quand je lance la recherche

      voici le code

      <?php
      
      include("connexion.php");
      
      $info=$conn->query('SELECT client FROM info ')or die(print_r($conn->erroInfo()));
      
      if (isset($_GET['q']) AND !empty($_GET['q']))
      
      {   
      
        $q= htmlspecialchars($_GET['q']);
      
      
      $info =$conn->query('SELECT client  FROM info WHERE
      
       client LIKE "%.$q.%"' );
        
      } 
      
      
      ?>
      
      <!DOCTYPE html>
      <html  >
      <head>
        <title>Moteur de recherche</title>
      </head>
      <body  >
      
      <form method="GET" >
         
       
        <h2 align="left" > 
      
        Entrer votre recherche: <input type="search" name="q" maxlength="80" size="80"  >
      
      
       <input type="submit" value="rechercher">
      
      <ul>
      
       <?php while ( $a = $info->fetch()) { ?> 
      
      <li>
        <?=$a['client']?> 
      </li> 
      
      <?php } ?> 
      
      
      </ul>
      
       </h2>
      
      </form>
       
      </body>
      </html>
      


      EDIT : Correction des BALISES DE CODE ( encore )
      0
    4. Modérateur
      $info =$conn->query('SELECT client  FROM info WHERE  client LIKE "%'.$q.'%"') or die(print_r($conn->erroInfo()));;
      
      0
    5. merci jordane45 mais je suis toujours dans la galère
      0