<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[[PHP] Script esporta solo prima riga!]]></title><description><![CDATA[<p dir="auto">Buongiorno a tutti voi gentili colleghi.<br />
Ho bisogno del vostro aiuto per uno script che deve esportare il contenuto di alcune tabelle di un database MySQL.<br />
Premetto che l'estrazione dei dati, la relativa formattazione e la stampa a video avviene come mi aspetto.<br />
Sto lavorando su uno script realizzato da altri e modificato secondo le necessità del mio cliente per esportare i dati in file CSV dove per separatore dei vari campi è stato usato "|" (pipe) e i dati all'interno dei campi i dati sono separati da virgola e nel caso degli URL delle immagini sono raggruopati fra singole virgolette se gli stessi sono 2 o più.</p>
<p dir="auto">Codice PHP di exporta_2.php</p>
<pre><code class="language-php">&lt;?php
require_once('config.php');

$pro = $_GET['pro'];
$id_lang = 6;

global $pro, $id_lang, $id_prod;

$link = mysql_connect(DB_HOSTNAME,DB_USERNAME,DB_PASSWORD);
mysql_select_db(DB_NAME,$link);

unlink('nome.csv');

$output = "CODICE|MACROCATEGORIA,CATEGORIA,SUBCATEGORIA|NOME|PREZZO|PESO|QUANTITA|DESCRIZIONE BREVE|DESCRIZIONE|IMMAGINI|URLPRODOTTO|EAN13\n";

$res = mysql_query("SELECT p.id_product, p.id_category_default, p.supplier_reference, p.price, p.weight, p.quantity, p.ean13, pl.name, pl.description, pl.description_short FROM ".PS_TABLE_PREFIX."product AS p INNER JOIN ".PS_TABLE_PREFIX."product_lang AS pl ON p.id_product = pl.id_product WHERE pl.id_lang = '".$id_lang."' AND p.id_manufacturer = '".$pro."'");
while ($row = mysql_fetch_assoc($res))
{   
    $id_prod = $row['id_product'];
    $id_cat_def = $row['id_category_default'];
    $sup_ref = $row['supplier_reference'];
    $price = $row['price'];
    $weight = $row['weight'];
    $quantity = $row['quantity'];
    $ean = $row['ean13'];
    $name = $row['name'];
    $desc_breve = strip_tags($row['description_short']);
    $descrizione = strip_tags($row['description']);

    /*
    echo $id_prod;
    echo "|".$id_cat_def;
    echo "|".$sup_ref;
    echo "|".$price;
    echo "|".$weight;
    echo "|".$quantity;
    echo "|".$ean;
    echo "|".$name;
    echo "|".$desc_breve;
    echo "|".$descrizione;
    echo "&lt;br /&gt;";
    */
    
    $output = $id_prod;
    
    // estrazione della Macrocategoria
    
    $output .= "|--Macrocategoria--|".$name."|".$price."|".$weight."|".$quantity."|".$desc_breve."|".$descrizione;
    
    // estrazione delle immagini
    // estrazione delle immagini -&gt; Sviluppo
    $conta_imm = "SELECT COUNT('id_image') FROM ".PS_TABLE_PREFIX."image WHERE id_product = '".$id_prod."'";
    $result = mysql_query($conta_imm ) or die (mysql_error());
    $totale = mysql_result($result,0,0);
    global $totale;
    //echo $totale;

    if ($totale == 8)
    {
     $resImg1 = mysql_query("SELECT id_image FROM ".PS_TABLE_PREFIX."image WHERE id_product = '".$id_prod."' and position='1'");
     while ($row = mysql_fetch_array($resImg1))
     {
      $id_img1 = $row['id_image'];
      global $id_img1;
     }

     $resImg2 = mysql_query("SELECT id_image FROM ".PS_TABLE_PREFIX."image WHERE id_product = '".$id_prod."' and position='2'");
     while ($row = mysql_fetch_array($resImg2))
     {
      $id_img2 = $row['id_image'];
      global $id_img2;
     }

     $resImg3 = mysql_query("SELECT id_image FROM ".PS_TABLE_PREFIX."image WHERE id_product = '".$id_prod."' and position='3'");
     while ($row = mysql_fetch_array($resImg3))
     {
      $id_img3 = $row['id_image'];
      global $id_img3;
     }

     $resImg4 = mysql_query("SELECT id_image FROM ".PS_TABLE_PREFIX."image WHERE id_product = '".$id_prod."' and position='4'");
     while ($row = mysql_fetch_array($resImg4))
     {
      $id_img4 = $row['id_image'];
      global $id_img4;
     }

     $resImg5 = mysql_query("SELECT id_image FROM ".PS_TABLE_PREFIX."image WHERE id_product = '".$id_prod."' and position='5'");
     while ($row = mysql_fetch_array($resImg5))
     {
      $id_img5 = $row['id_image'];
      global $id_img5;
     }

     $resImg6 = mysql_query("SELECT id_image FROM ".PS_TABLE_PREFIX."image WHERE id_product = '".$id_prod."' and position='6'");
     while ($row = mysql_fetch_array($resImg6))
     {
      $id_img6 = $row['id_image'];
      global $id_img6;
     }

     $resImg7 = mysql_query("SELECT id_image FROM ".PS_TABLE_PREFIX."image WHERE id_product = '".$id_prod."' and position='7'");
     while ($row = mysql_fetch_array($resImg7))
     {
      $id_img7 = $row['id_image'];
      global $id_img7;
     }

     $resImg8 = mysql_query("SELECT id_image FROM ".PS_TABLE_PREFIX."image WHERE id_product = '".$id_prod."' and position='8'");
     while ($row = mysql_fetch_array($resImg8))
     {
      $id_img8 = $row['id_image'];
      global $id_img8;
     }
     $image = "'".PATH."img/p/".$id_prod."-".$id_img1.".jpg,".PATH."img/p/".$id_prod."-".$id_img2.".jpg,";
     $image .= PATH."img/p/".$id_prod."-".$id_img3.".jpg,".PATH."img/p/".$id_prod."-".$id_img4.".jpg,";
     $image .= PATH."img/p/".$id_prod."-".$id_img5.".jpg,".PATH."img/p/".$id_prod."-".$id_img6.".jpg,";
     $image .= PATH."img/p/".$id_prod."-".$id_img7.".jpg,".PATH."img/p/".$id_prod."-".$id_img8.".jpg'|";
     global $image;
     //echo $image;
    }

    if ($totale == 7)
    {
     $resImg1 = mysql_query("SELECT id_image FROM ".PS_TABLE_PREFIX."image WHERE id_product = '".$id_prod."' and position='1'");
     while ($row = mysql_fetch_array($resImg1))
     {
      $id_img1 = $row['id_image'];
      global $id_img1;
     }

     $resImg2 = mysql_query("SELECT id_image FROM ".PS_TABLE_PREFIX."image WHERE id_product = '".$id_prod."' and position='2'");
     while ($row = mysql_fetch_array($resImg2))
     {
      $id_img2 = $row['id_image'];
      global $id_img2;
     }

     $resImg3 = mysql_query("SELECT id_image FROM ".PS_TABLE_PREFIX."image WHERE id_product = '".$id_prod."' and position='3'");
     while ($row = mysql_fetch_array($resImg3))
     {
      $id_img3 = $row['id_image'];
      global $id_img3;
     }

     $resImg4 = mysql_query("SELECT id_image FROM ".PS_TABLE_PREFIX."image WHERE id_product = '".$id_prod."' and position='4'");
     while ($row = mysql_fetch_array($resImg4))
     {
      $id_img4 = $row['id_image'];
      global $id_img4;
     }

     $resImg5 = mysql_query("SELECT id_image FROM ".PS_TABLE_PREFIX."image WHERE id_product = '".$id_prod."' and position='5'");
     while ($row = mysql_fetch_array($resImg5))
     {
      $id_img5 = $row['id_image'];
      global $id_img5;
     }

     $resImg6 = mysql_query("SELECT id_image FROM ".PS_TABLE_PREFIX."image WHERE id_product = '".$id_prod."' and position='6'");
     while ($row = mysql_fetch_array($resImg6))
     {
      $id_img6 = $row['id_image'];
      global $id_img6;
     }

     $resImg7 = mysql_query("SELECT id_image FROM ".PS_TABLE_PREFIX."image WHERE id_product = '".$id_prod."' and position='7'");
     while ($row = mysql_fetch_array($resImg7))
     {
      $id_img7 = $row['id_image'];
      global $id_img7;
     }
     
     $image = "'".PATH."img/p/".$id_prod."-".$id_img1.".jpg,".PATH."img/p/".$id_prod."-".$id_img2.".jpg,";
     $image .= PATH."img/p/".$id_prod."-".$id_img3.".jpg,".PATH."img/p/".$id_prod."-".$id_img4.".jpg,";
     $image .= PATH."img/p/".$id_prod."-".$id_img5.".jpg,".PATH."img/p/".$id_prod."-".$id_img6.".jpg,";
     $image .= PATH."img/p/".$id_prod."-".$id_img7.".jpg'|";
     global $image;
     //echo $image;
    }

    if ($totale == 6)
    {
     $resImg1 = mysql_query("SELECT id_image FROM ".PS_TABLE_PREFIX."image WHERE id_product = '".$id_prod."' and position='1'");
     while ($row = mysql_fetch_array($resImg1))
     {
      $id_img1 = $row['id_image'];
      global $id_img1;
     }

     $resImg2 = mysql_query("SELECT id_image FROM ".PS_TABLE_PREFIX."image WHERE id_product = '".$id_prod."' and position='2'");
     while ($row = mysql_fetch_array($resImg2))
     {
      $id_img2 = $row['id_image'];
      global $id_img2;
     }

     $resImg3 = mysql_query("SELECT id_image FROM ".PS_TABLE_PREFIX."image WHERE id_product = '".$id_prod."' and position='3'");
     while ($row = mysql_fetch_array($resImg3))
     {
      $id_img3 = $row['id_image'];
      global $id_img3;
     }

     $resImg4 = mysql_query("SELECT id_image FROM ".PS_TABLE_PREFIX."image WHERE id_product = '".$id_prod."' and position='4'");
     while ($row = mysql_fetch_array($resImg4))
     {
      $id_img4 = $row['id_image'];
      global $id_img4;
     }

     $resImg5 = mysql_query("SELECT id_image FROM ".PS_TABLE_PREFIX."image WHERE id_product = '".$id_prod."' and position='5'");
     while ($row = mysql_fetch_array($resImg5))
     {
      $id_img5 = $row['id_image'];
      global $id_img5;
     }

     $resImg6 = mysql_query("SELECT id_image FROM ".PS_TABLE_PREFIX."image WHERE id_product = '".$id_prod."' and position='6'");
     while ($row = mysql_fetch_array($resImg6))
     {
      $id_img6 = $row['id_image'];
      global $id_img6;
     }

     $image = "'".PATH."img/p/".$id_prod."-".$id_img1.".jpg,".PATH."img/p/".$id_prod."-".$id_img2.".jpg,";
     $image .= PATH."img/p/".$id_prod."-".$id_img3.".jpg,".PATH."img/p/".$id_prod."-".$id_img4.".jpg,";
     $image .= PATH."img/p/".$id_prod."-".$id_img5.".jpg,".PATH."img/p/".$id_prod."-".$id_img6.".jpg'|";
     global $image;
     //echo $image;
    }

    if ($totale == 5)
    {
     $resImg1 = mysql_query("SELECT id_image FROM ".PS_TABLE_PREFIX."image WHERE id_product = '".$id_prod."' and position='1'");
     while ($row = mysql_fetch_array($resImg1))
     {
      $id_img1 = $row['id_image'];
      global $id_img1;
     }

     $resImg2 = mysql_query("SELECT id_image FROM ".PS_TABLE_PREFIX."image WHERE id_product = '".$id_prod."' and position='2'");
     while ($row = mysql_fetch_array($resImg2))
     {
      $id_img2 = $row['id_image'];
      global $id_img2;
     }

     $resImg3 = mysql_query("SELECT id_image FROM ".PS_TABLE_PREFIX."image WHERE id_product = '".$id_prod."' and position='3'");
     while ($row = mysql_fetch_array($resImg3))
     {
      $id_img3 = $row['id_image'];
      global $id_img3;
     }

     $resImg4 = mysql_query("SELECT id_image FROM ".PS_TABLE_PREFIX."image WHERE id_product = '".$id_prod."' and position='4'");
     while ($row = mysql_fetch_array($resImg4))
     {
      $id_img4 = $row['id_image'];
      global $id_img4;
     }

     $resImg5 = mysql_query("SELECT id_image FROM ".PS_TABLE_PREFIX."image WHERE id_product = '".$id_prod."' and position='5'");
     while ($row = mysql_fetch_array($resImg5))
     {
      $id_img5 = $row['id_image'];
      global $id_img5;
     }

     $image = "'".PATH."img/p/".$id_prod."-".$id_img1.".jpg,".PATH."img/p/".$id_prod."-".$id_img2.".jpg'";
     $image .= PATH."img/p/".$id_prod."-".$id_img3.".jpg,".PATH."img/p/".$id_prod."-".$id_img4.".jpg,";
     $image .= PATH."img/p/".$id_prod."-".$id_img5.".jpg'|";
     global $image;
     //echo $image;
    }

    if ($totale == 4)
    {
     $resImg1 = mysql_query("SELECT id_image FROM ".PS_TABLE_PREFIX."image WHERE id_product = '".$id_prod."' and position='1'");
     while ($row = mysql_fetch_array($resImg1))
     {
      $id_img1 = $row['id_image'];
      global $id_img1;
     }

     $resImg2 = mysql_query("SELECT id_image FROM ".PS_TABLE_PREFIX."image WHERE id_product = '".$id_prod."' and position='2'");
     while ($row = mysql_fetch_array($resImg2))
     {
      $id_img2 = $row['id_image'];
      global $id_img2;
     }

     $resImg3 = mysql_query("SELECT id_image FROM ".PS_TABLE_PREFIX."image WHERE id_product = '".$id_prod."' and position='3'");
     while ($row = mysql_fetch_array($resImg3))
     {
      $id_img3 = $row['id_image'];
      global $id_img3;
     }

     $resImg4 = mysql_query("SELECT id_image FROM ".PS_TABLE_PREFIX."image WHERE id_product = '".$id_prod."' and position='4'");
     while ($row = mysql_fetch_array($resImg4))
     {
      $id_img4 = $row['id_image'];
      global $id_img4;
     }

     $image = "'".PATH."img/p/".$id_prod."-".$id_img1.".jpg,".PATH."img/p/".$id_prod."-".$id_img2.".jpg,";
     $image .= PATH."img/p/".$id_prod."-".$id_img3.".jpg,".PATH."img/p/".$id_prod."-".$id_img4.".jpg'|";
     global $image;
     //echo $image;
    }

    if ($totale == 3)
    {
     $resImg1 = mysql_query("SELECT id_image FROM ".PS_TABLE_PREFIX."image WHERE id_product = '".$id_prod."' and position='1'");
     while ($row = mysql_fetch_array($resImg1))
     {
      $id_img1 = $row['id_image'];
      global $id_img1;
     }

     $resImg2 = mysql_query("SELECT id_image FROM ".PS_TABLE_PREFIX."image WHERE id_product = '".$id_prod."' and position='2'");
     while ($row = mysql_fetch_array($resImg2))
     {
      $id_img2 = $row['id_image'];
      global $id_img2;
     }

     $resImg3 = mysql_query("SELECT id_image FROM ".PS_TABLE_PREFIX."image WHERE id_product = '".$id_prod."' and position='3'");
     while ($row = mysql_fetch_array($resImg3))
     {
      $id_img3 = $row['id_image'];
      global $id_img3;
     }

     $image = "'".PATH."img/p/".$id_prod."-".$id_img1.".jpg,".PATH."img/p/".$id_prod."-".$id_img2.".jpg,";
     $image .= PATH."img/p/".$id_prod."-".$id_img3.".jpg'|";
     global $image;
     //echo $image;
    }

    if ($totale == 2)
    {
     $resImg1 = mysql_query("SELECT id_image FROM ".PS_TABLE_PREFIX."image WHERE id_product = '".$id_prod."' and position='1'");
     while ($row = mysql_fetch_array($resImg1))
     {
      $id_img1 = $row['id_image'];
      global $id_img1;
     }

     $resImg2 = mysql_query("SELECT id_image FROM ".PS_TABLE_PREFIX."image WHERE id_product = '".$id_prod."' and position='2'");
     while ($row = mysql_fetch_array($resImg2))
     {
      $id_img2 = $row['id_image'];
      global $id_img2;
     }

     $image = "'".PATH."img/p/".$id_prod."-".$id_img1.".jpg,".PATH."img/p/".$id_prod."-".$id_img2.".jpg'|";
     global $image;
     //echo $image;
    }

    if ($totale == 1)
    {
     $resImg1 = mysql_query("SELECT id_image FROM ".PS_TABLE_PREFIX."image WHERE id_product = '".$id_prod."' and position='1'");
     while ($row = mysql_fetch_array($resImg1))
     {
      $id_img1 = $row['id_image'];
      global $id_img1;
     }

     $image = PATH."img/p/".$id_prod."-".$id_img1.".jpg|";
     global $image;
     //echo $image;
    }

    $output .= "|".$image;
    
    // composizione URL prodotti
    $urlprod = PATH."product.php?id_product=".$id_prod;
    global $urlprod;
    //echo $urlprod;
    
    $output .= "|".$urlprod."|".$ean."\n";

    $output = str_replace(",Home page","",$output);
    $output = str_replace(",","",$output);

}

$handle = fopen('nome.csv','w');
fwrite($handle,$output);
fclose($handle);

?&gt;
</code></pre>
<p dir="auto">Purtroppo nel file generato, che come si può notare si chiama nome.csv, appare solo la riga di intestazione quella che inizia per "CODICE|MACROCATEGORIA...".<br />
Inoltre se cambio il nome del file, ad esempio export_apple.csv, mi appare sempre una sola riga ed esattamente quella relativo all'ultimo prodotto estratto.</p>
<p dir="auto">Il database su cui si deve lavorare è quello di PrestaShop versione 1.5.3</p>
<p dir="auto">Il file config.php</p>
<pre><code class="language-php">&lt;?php
/* 
Settaggi.
*/
define('DB_USERNAME','');
define('DB_PASSWORD','');
define('DB_HOSTNAME','localhost');
define('DB_NAME','');
define('PS_TABLE_PREFIX','ps_');
define('PATH','&lt;indirizzo web assoluto del sito&gt;');
?&gt;
</code></pre>
<p dir="auto">Dipende tutto da $output?<br />
Resto in attesa di vostri consigli per estrarre tutti i prodotti di un produttore, in questo caso io sto lavorando con i dati relativi ai prodotti APPLE che sono stati installati come dati di esempio di Prestashop 1.5.3.<br />
A vostra disposizione per ulteriori informazioni.</p>
]]></description><link>https://dev.connect.gt/topic/184545/php-script-esporta-solo-prima-riga</link><generator>RSS for Node</generator><lastBuildDate>Thu, 26 Nov 2026 17:11:55 GMT</lastBuildDate><atom:link href="https://dev.connect.gt/topic/184545.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 12 Feb 2013 13:44:13 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to [PHP] Script esporta solo prima riga! on Thu, 14 Feb 2013 09:38:25 GMT]]></title><description><![CDATA[<p dir="auto">Sono riuscito a realizzare una nuova versione del file export_2.php che ho nominato export_21.php ma stavolta riesco a inserire nel file CSV risultante solo 2 righe la prima riga che funziona da instazione delle rispettive colonne mentre la seconda colonna corrisponde all'ultimo id di prodotto.<br />
Dovrebbero essere presenti 4 righe relative ad altrettanti prodotti ma non riesco a trovare una soluzione.<br />
La query eseguita in MySQL Query Browser estrare tutte le righe relative all'id del produttore passato.<br />
Se eseguo la stampa dei dati estratti senza cambiare le funzioni mysql_* visualizzo tutto.<br />
Nel nuovo file utilizzo la funzione <strong>fputcsv</strong> settata con i rispettivi parametri e le righe devono essere passate con array.<br />
L'unica versione funzionante della funzione <strong>fputcsv</strong> è quella tipica del manuale di PHP che crea il file CSV nella stessa cartella dove è presente lo script PHP ma per l'appunto vi sono presenti solo le due righe invece di un totale di 5.<br />
Ho provato anche ad impostare mysqli_fetch_all ma con i settaggi estratti dal manuale di PHP ottengo solo messaggi di errore.<br />
La mia versione di PHP è la 5.4.0 mentre quella di PrestaShop 1.5.3 è 5.3.21 e quindi ambedue posso eseguire le funzioni mysqli_<em>.<br />
Vi ringrazio per il vostro aiuto che è ancora necessario e accolgo suggerimenti e consigli.<br />
Intanto mi informo di opportuni settaggi di php.ini per usare al meglio le funzioni mysqli_</em>.<br />
Grazie a tutti.<br />
Qualcuno mi può aiutare</p>
]]></description><link>https://dev.connect.gt/post/1110000</link><guid isPermaLink="true">https://dev.connect.gt/post/1110000</guid><dc:creator><![CDATA[antoniog2]]></dc:creator><pubDate>Thu, 14 Feb 2013 09:38:25 GMT</pubDate></item></channel></rss>