Visualizzazione dei risultati da 1 a 10 su 10

Discussione: if... else

  1. #1

    if... else

    Ciao a tutti,
    non riesco a fare un "if". In pratica, all'interno del file setto una variabile (nome) e le associo un valore, poi vado a leggere una tabella (che contiene 6 righe) del db per verificare se il "nome" è presente nella tabella.
    Se non metto la condizione "if" e scrivo i risultati della query ottengo i 6 valori della tabella. Quello che vorrei fare é verificare se il valore recuperato dalla tabella è uguale a "nome". Per fare un esempio con il codice qui sotto vorrei che mi apparisse "bad ok bad bad bad bad", ma invece il risultato che ottengo è "ok ok ok ok ok ok".
    Come posso fare?

    codice:
    $nome = "Citta´ visitate";
    echo "<table cellpading=0 cellspacing=4><tr>";
    $query10=mysql_query("select * from menu order by id");
    while ($row10=mysql_fetch_assoc($query10))
    
    {
    $cit = $row10['pagina'];
    if ($nome = $cit) {
    echo "ok";
    }
    else {
    echo "bad";
    }
    
    
    }
    Grazie a tutti

  2. #2
    if ($nome = $cit) {

    con un solo uguale e' un assegnazione di valore e non una comparazione.

    Il silenzio è spesso la cosa migliore. Pensa ... è gratis.

  3. #3

  4. #4
    Altra domanda:
    faccio una query, se non ottengo risultati scrivo "ko", se ottengo qualche risultato scrivo "ok". Come si fa?

    codice:
    $query3=mysql_query("select distinct luogo from foto where luogo='$cit1' order by citta asc");
    while ($row2=mysql_fetch_assoc($query2))
    {
    echo "ok";
    }
    come faccio a mettere la condizione "ko"?

    Grazie

  5. #5
    Codice PHP:
    $query3=mysql_query("select distinct luogo from foto where luogo='$cit1' order by citta asc");

    if(
    mysql_num_rows($query3) == || !$query3) echo "ko";
    else echo 
    "ok"
    Questa e' la mia firma! Lo so, e' una mezza schifezza.
    Un sito
    - skype non è per consulenze online -

  6. #6

  7. #7
    E' passato un po' di tempo, ma non ho avuto tempo per provare prima. Ancora non mi funziona
    Il mio codice e':
    Codice PHP:
    $query1=mysql_query("select distinct cat from citta order by citta asc");
    while (
    $row1=mysql_fetch_assoc($query1))

    {
    echo 
    "<a name=";
    echo 
    $row1['cat'];
    echo 
    "></a><h2>";
    echo 
    $row1['cat'];
    echo 
    "</h2>";

    $query2=mysql_query("select * from citta where cat='$row1[cat]' order by citta asc");
    while (
    $row2=mysql_fetch_assoc($query2))
    {
    $cit1 $row2['citta'];
    $query3=mysql_query("select distinct luogo from foto where luogo='$cit1' order by citta asc");

    if(
    mysql_num_rows($query3) == || !$query3) echo "ko";
    else echo 
    "ok";





    }

    e l'errore è
    Codice PHP:
    Warningmysql_num_rows(): supplied argument is not a valid MySQL result resource in /www/10gbfreehost.com/d/y/d/dydale/htdocs/citta2.php on line 48
    ko
    Warning
    mysql_num_rows(): supplied argument is not a valid MySQL result resource in /www/10gbfreehost.com/d/y/d/dydale/htdocs/citta2.php on line 48
    ko
    Warning
    mysql_num_rows(): supplied argument is not a valid MySQL result resource in /www/10gbfreehost.com/d/y/d/dydale/htdocs/citta2.php on line 48
    ko
    Warning
    mysql_num_rows(): supplied argument is not a valid MySQL result resource in /www/10gbfreehost.com/d/y/d/dydale/htdocs/citta2.php on line 48
    ko
    Warning
    mysql_num_rows(): supplied argument is not a valid MySQL result resource in /www/10gbfreehost.com/d/y/d/dydale/htdocs/citta2.php on line 48
    ko
    Warning
    mysql_num_rows(): supplied argument is not a valid MySQL result resource in /www/10gbfreehost.com/d/y/d/dydale/htdocs/citta2.php on line 48
    ko
    Warning
    mysql_num_rows(): supplied argument is not a valid MySQL result resource in /www/10gbfreehost.com/d/y/d/dydale/htdocs/citta2.php on line 48
    ko
    Warning
    mysql_num_rows(): supplied argument is not a valid MySQL result resource in /www/10gbfreehost.com/d/y/d/dydale/htdocs/citta2.php on line 48
    ko
    Warning
    mysql_num_rows(): supplied argument is not a valid MySQL result resource in /www/10gbfreehost.com/d/y/d/dydale/htdocs/citta2.php on line 48 
    Sapete aiutarmi?

    Grazie

  8. #8
    metti la segnalazione di errore mysql_error() dopo tutte le query.

    Il silenzio è spesso la cosa migliore. Pensa ... è gratis.

  9. #9
    Dato che non conosco php, probabilmente ho sbagliato:

    Codice PHP:
    $query1=mysql_query("select distinct cat from citta order by citta asc") or die(mysql_error());
    while (
    $row1=mysql_fetch_assoc($query1))

    {
    echo 
    "<a name=";
    echo 
    $row1['cat'];
    echo 
    "></a><h2>";
    echo 
    $row1['cat'];
    echo 
    "</h2>";

    $query2=mysql_query("select * from citta where cat='$row1[cat]' order by citta asc") or die(mysql_error());
    while (
    $row2=mysql_fetch_assoc($query2))
    {
    $cit1 $row2['citta'];
    $query3=mysql_query("select distinct luogo from foto where luogo='$cit1' order by citta asc") or die(mysql_error());

    if(
    mysql_num_rows($query3) == || !$query3) echo "ko";
    else echo 
    "ok";





    }

    Ho modificato così il codice, ma l'errore è sempre uguale

    Codice PHP:
    Warningmysql_num_rows(): supplied argument is not a valid MySQL result resource in /www/10gbfreehost.com/d/y/d/dydale/htdocs/citta2.php on line 48 
    (ripetuto tante volte)

    Senza le ultime due righe "if...else..." non dà errore.

  10. #10
    personalmente le query annidate nel where mi suonano come campane rotte.

    L'ultima if controlla se hai delle righe nel result set oppure hai ricevuto un false dalla query.

    ma la query e' condizionata dal while precedente, e non stampi nulla nel frattempo.

    stampa un po' di righe che ti servano da debug.

    esempio sull'ultima query:

    Codice PHP:
    while ($row2=mysql_fetch_assoc($query2))
    {
    echo 
    $cit1 $row2['citta'];
    $query3 mysql_query("select distinct luogo 
                            from foto 
                            where luogo='
    $cit1' order by citta asc") or die(mysql_error());

    if(
    mysql_num_rows($query3) == || !$query3) echo "ko - "mysql_num_rows($query3);
    else echo 
    "ok"

    Il silenzio è spesso la cosa migliore. Pensa ... è gratis.

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.