Pagina 1 di 2 1 2 ultimoultimo
Visualizzazione dei risultati da 1 a 10 su 14
  1. #1

    Ricerca in un database mysql

    Salve a tutti
    sto scrivendo un paio di pagine in php per gestire una biblioteca

    ho dei problemi per la ricerca....
    sono riuscito a fare una bella funzione per la ricerca avanzata (controllo in sequenza di tutti in campi in AND e poi via via in OR)

    ma non capisco quale possa essere un buon modo per una ricerca "semplice".... cioè..... come estrapolare i risultati da un'unica stringa di ricerca

    considerando che potrebbero venire cercate parole appartenenti a più campi ho usato explode, ma non funziona nulla, mi sapete aiutare?

    Codice PHP:
    if ($_POST['QSearch']!=""){        echo ("<fieldset><legend>Ricerca libera</legend>");        $stringhe=explode(" "$_POST['Search']);        foreach( $stringhe as $stringhecercate) {             echo $stringhecercate;            $Resmysql_query("SELECT * FROM Testi WHERE (LOWER(Testi.Posizione) LIKE '%".$stringhecercate."%' OR (LOWER(Testi.Autore) LIKE '%".$stringhecercate."%' OR (LOWER(Testi.Titolo) LIKE '%".$stringhecercate."%' OR (LOWER(Testi.Editore) LIKE '%".$stringhecercate."%' OR (LOWER(Testi.Anno) LIKE '%".$stringhecercate."%' OR (LOWER(Testi.ISBN) LIKE '%".$stringhecercate."%' ORDER BY Testi.Autore;"$MyConn);}        if (mysql_num_rows($Res) == 0)            echo ("Non è stato trovato niente");        else {            echo ("<table class=\"db\">");            while ($Entrymysql_fetch_array($Res))                echo ("<tr><td>".$Entry['ID']."</td><td>".$Entry['Posizione']."</td><td>".$Entry['Autore']."</td><td>".$Entry['Titolo']."</td><td>".$Entry['Editore']."</td><td>".$Entry['Anno']."</td><td>".$Entry['ISBN']."</td><td>".$Entry['Note']."</td><td>".$Entry['Prestato']."</td></tr>");            echo ("</table>");        }        echo ("</fieldset>");    } 

    l'errore che mi restituisce è

    Codice PHP:
    Warningmysql_num_rows() expects parameter 1 to be resourceboolean given in /home/mhd-01/www.nethanel.it/htdocs/biblioteca/cerca.php on line 62 
    la riga 62 è
    Codice PHP:
            if (mysql_num_rows($Res) == 0
    grazie!

  2. #2
    Moderatore di PHP L'avatar di Alhazred
    Registrato dal
    Oct 2003
    Messaggi
    12,505
    Riformatta il codice perché non si capisce niente.
    Quando posti del codice, prima di inviare il messaggio visualizza l'anteprima per assicurarti che sia ben formattato.

  3. #3
    m, si, scusate!


    Codice PHP:
    if ($_POST['QSearch']!=""){
            echo (
    "<fieldset><legend>Ricerca libera</legend>");
            
    $stringhe=explode(" "$_POST['Search']);
            foreach( 
    $stringhe as $stringhecercate) {
                 echo 
    $stringhecercate;
                
    $Resmysql_query("SELECT * FROM Testi WHERE (LOWER(Testi.Posizione) LIKE '%".$stringhecercate."%' OR (LOWER(Testi.Autore) LIKE '%".$stringhecercate."%' OR (LOWER(Testi.Titolo) LIKE '%".$stringhecercate."%' OR (LOWER(Testi.Editore) LIKE '%".$stringhecercate."%' OR (LOWER(Testi.Anno) LIKE '%".$stringhecercate."%' OR (LOWER(Testi.ISBN) LIKE '%".$stringhecercate."%' ORDER BY Testi.Autore;"$MyConn);}


            if (
    mysql_num_rows($Res) == 0)
                echo (
    "Non è stato trovato niente");
            else {
                echo (
    "<table class=\"db\">");
                while (
    $Entrymysql_fetch_array($Res))
                echo (
    "<tr><td>".$Entry['ID']."</td><td>".$Entry['Posizione']."</td><td>".$Entry['Autore']."</td><td>".$Entry['Titolo']."</td><td>".$Entry['Editore']."</td><td>".$Entry['Anno']."</td><td>".$Entry['ISBN']."</td><td>".$Entry['Note']."</td><td>".$Entry['Prestato']."</td></tr>");
                echo (
    "</table>");
            }        
    echo (
    "</fieldset>");
        } 
    un po' meglio.... poi il fatto è che è confuso di suo!

  4. #4
    Utente di HTML.it
    Registrato dal
    Feb 2014
    residenza
    Voltati
    Messaggi
    913
    L'errore non è questo, ma tu fai tante query e usi solo l'ultima
    Dovresti farlo così il foreach

    Codice PHP:
    $query "SELECT * FROM Test WHERE";
    foreach (
    $stringhe as $stringacercata) {
        if (
    strpos($query"OR") !== false) {        $query .= " OR";
        }
        
    $stringa strtolower ($stringacercata);
        
    $query .= " LOWER(Posizione) LIKE '%$stringa%' OR LOWER(Autore) LIKE '%$stringa%' OR LOWER(Titolo) LIKE '%$stringa%' OR LOWER(Editore) LIKE '%$stringa%' OR LOWER(Anno) LIKE '%$stringa%' OR LOWER(ISBN) LIKE '%$stringa%'"
    }
    $query .= " ORDER BY Autore";
    $Res mysql_query($query$MyConn); 
    No

  5. #5
    Codice PHP:
    if ($_POST['QSearch']!=""){
            echo (
    "<fieldset><legend>Ricerca libera</legend>");
            
    $stringhe=explode(" "$_POST['QSearch']);
            
    $query "SELECT * FROM Test WHERE";
      foreach (
    $stringhe as $stringacercata) {
            
    $stringa strtolower ($stringacercata);
            
    $query .= "LOWER(Posizione) LIKE '%".$stringa."%' OR LOWER(Autore) LIKE '%".$stringa."%' OR LOWER(Titolo) LIKE '%".$stringa."%' OR LOWER(Editore) LIKE '%".$stringa."%' OR LOWER(Anno) LIKE '%".$stringa."%' OR LOWER(ISBN) LIKE '%".$stringa."%'"
    }
    $query.=" ORDER BY Autore";
    $Res=mysql_query($query$MyConn); 
        if (
    mysql_num_rows($Res) == 0) echo {("Non è stato trovato niente");}
        else {
                echo (
    "<table class=\"db\">".$TabHead_LibriClienti);
                while (
    $Entrymysql_fetch_array($Res))
                    echo (
    "<tr><td>".$Entry['ID']."</td><td>".$Entry['Posizione']."</td><td>".$Entry['Autore']."</td><td>".$Entry['Titolo']."</td><td>".$Entry['Editore']."</td><td>".$Entry['Anno']."</td><td>".$Entry['ISBN']."</td><td>".$Entry['Note']."</td><td>".$Entry['Prestato']."</td></tr>");
                echo (
    "</table>");
            }
            echo (
    "</fieldset>");
        } 

    mi dà
    codice:
    Parse error:  syntax error, unexpected '}' in /home/mhd-01/www.nethanel.it/htdocs/biblioteca/cerca.php on line 83
    se la tolgo (è quella di chiusura del foreach)
    codice:
    Parse error:  syntax error, unexpected T_VARIABLE in /home/mhd-01/www.nethanel.it/htdocs/biblioteca/cerca.php on line 83

    ....?

  6. #6
    Moderatore di PHP L'avatar di Alhazred
    Registrato dal
    Oct 2003
    Messaggi
    12,505
    Manca il ; dopo
    LIKE '%".$stringa."%'"

  7. #7
    ok.... stupido io


    in ogni caso:

    seconda parte

    Codice PHP:
    $Res mysql_query($query$MyConn); 
        if (
    mysql_num_rows($Res) == 0)
            echo (
    "Non è stato trovato niente");
        else {
                echo (
    "<table class=\"db\">".$TabHead_LibriClienti);
                while (
    $Entrymysql_fetch_array($Res))
                    echo (
    "<tr><td>".$Entry['ID']."</td><td>".$Entry['Posizione']."</td><td>".$Entry['Autore']."</td><td>".$Entry['Titolo']."</td><td>".$Entry['Editore']."</td><td>".$Entry['Anno']."</td><td>".$Entry['ISBN']."</td><td>".$Entry['Note']."</td><td>".$Entry['Prestato']."</td></tr>");
                echo (
    "</table>");
            } 
    mi dà
    codice:
    Warning:  mysql_num_rows() expects parameter 1 to be resource, boolean given in /home/mhd-01/www.nethanel.it/htdocs/biblioteca/cerca.php on line 69
    Non è stato trovato niente

  8. #8
    Utente di HTML.it
    Registrato dal
    Feb 2014
    residenza
    Voltati
    Messaggi
    913
    fai un echo di $Res

    Non viene "Resurce #id" con un numero?
    No

  9. #9
    ho provato, ma non appare nulla e l'errore resta identico a prima

  10. #10
    Utente di HTML.it L'avatar di badaze
    Registrato dal
    Jun 2002
    residenza
    Lyon
    Messaggi
    5,372
    La documentazione di mysql_query dice che se la query è una SELECT il dato di ritorno è una resource altrimenti è un boolean.
    Ridatemi i miei 1000 posts persi !!!!
    Non serve a nulla ottimizzare qualcosa che non funziona.
    Cerco il manuale dell'Olivetti LOGOS 80B - www.emmella.fr

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.