Visualizzazione dei risultati da 1 a 3 su 3

Discussione: problema query

  1. #1

    problema query

    salve...
    ho db del sito con tabella catalogo con campi autore, editore, paginaautore (il link verso la pg dell'autore con tutti i suoi libri presenti in catalogo), pageditore (il link verso pag dell'editore con tutti i libri in catalogo)....quando utente fa ricerca nel catalogo la pag di risposta oltre al libro cercato gli offre anche i link verso paginaautore e pageditore...recuperati con questa query:

    Codice PHP:
    <?php                         //recupero link paginaautore e link pageditore
     
      
    $db 'nome database'
      
    $sel mysql_select_db($db) or die (mysql_error());//selz il db
      
    $a $row_Recordset1['editore']; // il nome dell'editore 
      
    $b $row_Recordset1['autore'];  //il nome dell'autore cercato
      
     
    //query al db
     
    $query mysql_query("SELECT paginaautore,pageditore FROM catalogo WHERE autore LIKE '%$b%' AND editore LIKE '%$a%'"); 
     
    $row mysql_fetch_array($query);
      
     
    ?>
      <div id="link-pg-autore">
      <div class="autore"><?php echo " <a href=\"" .$row['paginaautore'];  ?>">tutti i libri dell'autore in catalogo</a></div> 
      <div id="link-pg-editore">
      <?php echo " <a href=\"" .$row['pageditore'];  ?>">catalogo casa editrice</a>
      </div>
      </div>
    </div>
    funziona...a meno che il nome dell'autore e/o dell'editore contenga un apostrofo....allora ho questo messaggio d'errore:

    Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in /home/...etc...etc...e la righa è questa: $row = mysql_fetch_array($query);

    ..è possibile fargli digerire in qualche modo l'apostrofo?

    grazie

  2. #2
    Utente di HTML.it
    Registrato dal
    May 2012
    Messaggi
    1,453
    mysql_real_escape_string(), o aggiungi gli slash manualmente prima degli apostrofi

  3. #3
    grazie...letto in giro...se capito bene mysql_real_escape_string() la uso quando metto dati nel database..ho usato dreamweaver per questo :

    Codice PHP:
    <?php
    if (!function_exists("GetSQLValueString")) {
    function 
    GetSQLValueString($theValue$theType$theDefinedValue ""$theNotDefinedValue ""
    {
      if (
    PHP_VERSION 6) {
        
    $theValue get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
      }

      
    $theValue function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

      switch (
    $theType) {
        case 
    "text":
          
    $theValue = ($theValue != "") ? "'" $theValue "'" "NULL";
          break;    
        case 
    "long":
        case 
    "int":
          
    $theValue = ($theValue != "") ? intval($theValue) : "NULL";
          break;
        case 
    "double":
          
    $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
          break;
        case 
    "date":
          
    $theValue = ($theValue != "") ? "'" $theValue "'" "NULL";
          break;
        case 
    "defined":
          
    $theValue = ($theValue != "") ? $theDefinedValue $theNotDefinedValue;
          break;
      }
      return 
    $theValue;
    }
    }

    $editFormAction $_SERVER['PHP_SELF'];
    if (isset(
    $_SERVER['QUERY_STRING'])) {
      
    $editFormAction .= "?" htmlentities($_SERVER['QUERY_STRING']);
    }

    if ((isset(
    $_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
      
    $insertSQL sprintf("INSERT INTO catalogo (immagine, autore, titolo, genere, editore, anno, euro, paginaautore, pageditore, recensione) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
                           
    GetSQLValueString($_POST['immagine'], "text"),
                           
    GetSQLValueString($_POST['autore'], "text"),
                           
    GetSQLValueString($_POST['titolo'], "text"),
                           
    GetSQLValueString($_POST['genere'], "text"),
                           
    GetSQLValueString($_POST['editore'], "text"),
                           
    GetSQLValueString($_POST['anno'], "text"),
                           
    GetSQLValueString($_POST['euro'], "text"),
                           
    GetSQLValueString($_POST['pgautore'], "text"),
                   
    GetSQLValueString($_POST['pgeditore'], "text"),
                           
    GetSQLValueString($_POST['recensione'], "text"));
      
    mysql_select_db($database_conexione$conexione);
      
    $Result1 mysql_query($insertSQL$conexione) or die(mysql_error());

      
    $insertGoTo "catalogo.php";
      if (isset(
    $_SERVER['QUERY_STRING'])) {
        
    $insertGoTo .= (strpos($insertGoTo'?')) ? "&" "?";
        
    $insertGoTo .= $_SERVER['QUERY_STRING'];
      }
      
    header(sprintf("Location: %s"$insertGoTo));
    }
    ?>
    poi nel body c'è il form dove inserisco i vari campi...da pivello mi sembra che mysql_real_escape_string() lo usi....se puoi darmi una dritta...grazie

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.