<?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[Classe PHP per iterazioni mysql]]></title><description><![CDATA[<p dir="auto">Ciao a tutti,<br />
sto lavorando ad alcune pagine php per interagire con i dati contenuti in un db MySql e per comodità sto cercando una classe da includere e che mi faciliti un po' il lavoro oltre a renderlo più leggibile. Tra le diverse che ho trovato,quella che mi è sembrata un po' più interessante è quella di Sebastien Laout,non so se qualcuno ha provato a lavorarci...<br />
Il problema che mi si presenta è il seguente,scarico la classe, la estraggo nella cartella dove sono presenti le pagine su cui lavoro e provo a includerla in una pagina di prova con:<br />
include "db.class.php";<br />
Poi creo un oggetto DB come specificato anche nelle note dell'autore ma appena provo ad aprire la pagina mi dice che non è possibile includere la classe e che non esiste nessun oggetto di tipo DB......<br />
Avete qualche dritta da darmi?</p>
]]></description><link>https://dev.connect.gt/topic/210997/classe-php-per-iterazioni-mysql</link><generator>RSS for Node</generator><lastBuildDate>Thu, 26 Nov 2026 20:38:06 GMT</lastBuildDate><atom:link href="https://dev.connect.gt/topic/210997.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 11 Feb 2015 20:47:30 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Classe PHP per iterazioni mysql on Thu, 12 Feb 2015 21:05:45 GMT]]></title><description><![CDATA[<p dir="auto">L'errore era sulla funzione mysqli_num_rows in quanto riceveva in input una stringa... Non mi sono segnato la riga ma se non sbaglio era quando veniva richiamata in fetchNextObject</p>
]]></description><link>https://dev.connect.gt/post/1189338</link><guid isPermaLink="true">https://dev.connect.gt/post/1189338</guid><dc:creator><![CDATA[luigiberducci]]></dc:creator><pubDate>Thu, 12 Feb 2015 21:05:45 GMT</pubDate></item><item><title><![CDATA[Reply to Classe PHP per iterazioni mysql on Thu, 12 Feb 2015 18:03:05 GMT]]></title><description><![CDATA[<p dir="auto">Mi dici che errore mysqli ti ha dato?</p>
]]></description><link>https://dev.connect.gt/post/1189330</link><guid isPermaLink="true">https://dev.connect.gt/post/1189330</guid><dc:creator><![CDATA[oslino]]></dc:creator><pubDate>Thu, 12 Feb 2015 18:03:05 GMT</pubDate></item><item><title><![CDATA[Reply to Classe PHP per iterazioni mysql on Thu, 12 Feb 2015 17:43:01 GMT]]></title><description><![CDATA[<p dir="auto">Innanzitutto ti ringrazio per il tempo che mi hai dato <img src="https://dev.connect.gt/assets/plugins/nodebb-plugin-emoji/emoji/android/1f642.png?v=12b9555a9a1" class="not-responsive emoji emoji-android emoji--slightly_smiling_face" style="height:1.4em;width:auto;vertical-align:middle" title=":)" alt="🙂" /> La classe così come l'hai scritta mi da un errore legato ad una funzione mysqli ma comunque ho risolto semplicemente sostituendo nella classe "&lt;?" con "&lt;?php".....<br />
Quando mi ha funzionato quasi non ci volevo credere,per una cosa simile <img src="https://dev.connect.gt/assets/plugins/nodebb-plugin-emoji/emoji/customizations/29fff08e-17e5-4013-ae14-11c7dde7a411-124.gif?v=12b9555a9a1" class="not-responsive emoji emoji-customizations emoji--rollo" style="height:1.4em;width:auto;vertical-align:middle" title=":rollo:" alt="" /><br />
Buona serata!</p>
]]></description><link>https://dev.connect.gt/post/1189337</link><guid isPermaLink="true">https://dev.connect.gt/post/1189337</guid><dc:creator><![CDATA[luigiberducci]]></dc:creator><pubDate>Thu, 12 Feb 2015 17:43:01 GMT</pubDate></item><item><title><![CDATA[Reply to Classe PHP per iterazioni mysql on Wed, 11 Feb 2015 23:12:20 GMT]]></title><description><![CDATA[<p dir="auto">Ho aggiornato la classe a PHP5 e a mysqli.<br />
Il funzionamento rimane quello originario.</p>
<pre><code class="language-php">&lt;?php
  /** A PHP class to access MySQL database with convenient methods
    * in an object oriented way, and with a powerful debug system.\n
    * Licence:  LGPL \n
    * Web site: http://slaout.linux62.org/
    * @version  1.0
    * @author   S&amp;eacute;bastien Lao&amp;ucirc;t (slaout@linux62.org)
    */
  class DB
  {
    /** Put this variable to true if you want ALL queries to be debugged by default:
      */
    private $defaultDebug = false;

    /** INTERNAL: The start time, in miliseconds.
      */
    private $mtStart;
    /** INTERNAL: The number of executed queries.
      */
    private $nbQueries;
    /** INTERNAL: The last result ressource of a query().
      */
   private $lastResult;
   
   /** RESOURCE to DB
     */
   private $cnt;

    /** Connect to a MySQL database to be able to use the methods below.
      */
    function __construct($base, $server, $user, $pass)
    {
      $this-&gt;mtStart    = $this-&gt;getMicroTime();
      $this-&gt;nbQueries  = 0;
      $this-&gt;lastResult = NULL;
     $this-&gt;cnt = mysqli_connect($server, $user, $pass, $base) or die('Server connexion not possible.');
     
    }

    /** Query the database.
      * @param $query The query.
      * @param $debug If true, it output the query and the resulting table.
      * @return The result of the query, to use with fetchNextObject().
      */
    function query($query, $debug = -1)
    {
      $this-&gt;nbQueries++;
      $this-&gt;lastResult = mysqli_query($this-&gt;cnt, $query) or $this-&gt;debugAndDie($query);

      $this-&gt;debug($debug, $query, $this-&gt;lastResult);

      return $this-&gt;lastResult;
    }
    /** Do the same as query() but do not return nor store result.\n
      * Should be used for INSERT, UPDATE, DELETE...
      * @param $query The query.
      * @param $debug If true, it output the query and the resulting table.
      */
    function execute($query, $debug = -1)
    {
      $this-&gt;nbQueries++;
      mysqli_query($this-&gt;cnt,$query) or $this-&gt;debugAndDie($query);

      $this-&gt;debug($debug, $query);
    }
    /** Convenient method for mysql_fetch_object().
      * @param $result The ressource returned by query(). If NULL, the last result returned by query() will be used.
      * @return An object representing a data row.
      */
    function fetchNextObject($result = NULL)
    {
      if ($result == NULL)
        $result = $this-&gt;lastResult;

      if ($result == NULL || mysqli_num_rows($result) &lt; 1)
        return NULL;
      else
        return mysqli_fetch_object($result);
    }
    /** Get the number of rows of a query.
      * @param $result The ressource returned by query(). If NULL, the last result returned by query() will be used.
      * @return The number of rows of the query (0 or more).
      */
    function numRows($result = NULL)
    {
      if ($result == NULL)
        return mysqli_num_rows($this-&gt;lastResult);
      else
        return mysqli_num_rows($result);
    }
    /** Get the result of the query as an object. The query should return a unique row.\n
      * Note: no need to add "LIMIT 1" at the end of your query because
      * the method will add that (for optimisation purpose).
      * @param $query The query.
      * @param $debug If true, it output the query and the resulting row.
      * @return An object representing a data row (or NULL if result is empty).
      */
    function queryUniqueObject($query, $debug = -1)
    {
      $query = "$query LIMIT 1";

      $this-&gt;nbQueries++;
      $result = mysqli_query($this-&gt;cnt,$query) or $this-&gt;debugAndDie($query);

      $this-&gt;debug($debug, $query, $result);

      return mysqli_fetch_object($result);
    }
    /** Get the result of the query as value. The query should return a unique cell.\n
      * Note: no need to add "LIMIT 1" at the end of your query because
      * the method will add that (for optimisation purpose).
      * @param $query The query.
      * @param $debug If true, it output the query and the resulting value.
      * @return A value representing a data cell (or NULL if result is empty).
      */
    function queryUniqueValue($query, $debug = -1)
    {
      $query = "$query LIMIT 1";

      $this-&gt;nbQueries++;
      $result = mysqli_query($this-&gt;cnt,$query) or $this-&gt;debugAndDie($query);
      $line = mysqli_fetch_row($result);

      $this-&gt;debug($debug, $query, $result);

      return $line[0];
    }
    /** Get the maximum value of a column in a table, with a condition.
      * @param $column The column where to compute the maximum.
      * @param $table The table where to compute the maximum.
      * @param $where The condition before to compute the maximum.
      * @return The maximum value (or NULL if result is empty).
      */
    function maxOf($column, $table, $where)
    {
      return $this-&gt;queryUniqueValue("SELECT MAX(`$column`) FROM `$table` WHERE $where");
    }
    /** Get the maximum value of a column in a table.
      * @param $column The column where to compute the maximum.
      * @param $table The table where to compute the maximum.
      * @return The maximum value (or NULL if result is empty).
      */
    function maxOfAll($column, $table)
    {
      return $this-&gt;queryUniqueValue("SELECT MAX(`$column`) FROM `$table`");
    }
    /** Get the count of rows in a table, with a condition.
      * @param $table The table where to compute the number of rows.
      * @param $where The condition before to compute the number or rows.
      * @return The number of rows (0 or more).
      */
    function countOf($table, $where)
    {
      return $this-&gt;queryUniqueValue("SELECT COUNT(*) FROM `$table` WHERE $where");
    }
    /** Get the count of rows in a table.
      * @param $table The table where to compute the number of rows.
      * @return The number of rows (0 or more).
      */
    function countOfAll($table)
    {
      return $this-&gt;queryUniqueValue("SELECT COUNT(*) FROM `$table`");
    }
    /** Internal function to debug when MySQL encountered an error,
      * even if debug is set to Off.
      * @param $query The SQL query to echo before diying.
      */
    function debugAndDie($query)
    {
      $this-&gt;debugQuery($query, "Error");
      die("&lt;p style=\"margin: 2px;\"&gt;".mysqli_error($this-&gt;cnt)."&lt;/p&gt;&lt;/div&gt;");
    }
    /** Internal function to debug a MySQL query.\n
      * Show the query and output the resulting table if not NULL.
      * @param $debug The parameter passed to query() functions. Can be boolean or -1 (default).
      * @param $query The SQL query to debug.
      * @param $result The resulting table of the query, if available.
      */
    function debug($debug, $query, $result = NULL)
    {
      if ($debug === -1 &amp;&amp; $this-&gt;defaultDebug === false)
        return;
      if ($debug === false)
        return;

      $reason = ($debug === -1 ? "Default Debug" : "Debug");
      $this-&gt;debugQuery($query, $reason);
      if ($result == NULL)
        echo "&lt;p style=\"margin: 2px;\"&gt;Number of affected rows: ".mysqli_affected_rows($this-&gt;cnt)."&lt;/p&gt;&lt;/div&gt;";
      else
        $this-&gt;debugResult($result);
    }
    /** Internal function to output a query for debug purpose.\n
      * Should be followed by a call to debugResult() or an echo of "&lt;/div&gt;".
      * @param $query The SQL query to debug.
      * @param $reason The reason why this function is called: "Default Debug", "Debug" or "Error".
      */
    function debugQuery($query, $reason = "Debug")
    {
      $color = ($reason == "Error" ? "red" : "orange");
      echo "&lt;div style=\"border: solid $color 1px; margin: 2px;\"&gt;".
           "&lt;p style=\"margin: 0 0 2px 0; padding: 0; background-color: #DDF;\"&gt;".
           "&lt;strong style=\"padding: 0 3px; background-color: $color; color: white;\"&gt;$reason:&lt;/strong&gt; ".
           "&lt;span style=\"font-family: monospace;\"&gt;".htmlentities($query)."&lt;/span&gt;&lt;/p&gt;";
    }
    /** Internal function to output a table representing the result of a query, for debug purpose.\n
      * Should be preceded by a call to debugQuery().
      * @param $result The resulting table of the query.
      */
    function debugResult($result)
    {
      echo "&lt;table border=\"1\" style=\"margin: 2px;\"&gt;".
           "&lt;thead style=\"font-size: 80%\"&gt;";
      $numFields = mysqli_num_fields($result);
      // BEGIN HEADER
      $tables    = array();
      $nbTables  = -1;
      $lastTable = "";
      $fields    = array();
      $nbFields  = -1;
      while ($column = mysqli_fetch_field($result)) {
        if ($column-&gt;table != $lastTable) {
          $nbTables++;
          $tables[$nbTables] = array("name" =&gt; $column-&gt;table, "count" =&gt; 1);
        } else
          $tables[$nbTables]["count"]++;
        $lastTable = $column-&gt;table;
        $nbFields++;
        $fields[$nbFields] = $column-&gt;name;
      }
      for ($i = 0; $i &lt;= $nbTables; $i++)
        echo "&lt;th colspan=".$tables[$i]["count"]."&gt;".$tables[$i]["name"]."&lt;/th&gt;";
      echo "&lt;/thead&gt;";
      echo "&lt;thead style=\"font-size: 80%\"&gt;";
      for ($i = 0; $i &lt;= $nbFields; $i++)
        echo "&lt;th&gt;".$fields[$i]."&lt;/th&gt;";
      echo "&lt;/thead&gt;";
      // END HEADER
      while ($row = mysqli_fetch_array($result)) {
        echo "&lt;tr&gt;";
        for ($i = 0; $i &lt; $numFields; $i++)
          echo "&lt;td&gt;".htmlentities($row[$i])."&lt;/td&gt;";
        echo "&lt;/tr&gt;";
      }
      echo "&lt;/table&gt;&lt;/div&gt;";
      $this-&gt;resetFetch($result);
    }
    /** Get how many time the script took from the begin of this object.
      * @return The script execution time in seconds since the
      * creation of this object.
      */
    function getExecTime()
    {
      return round(($this-&gt;getMicroTime() - $this-&gt;mtStart) * 1000) / 1000;
    }
    /** Get the number of queries executed from the begin of this object.
      * @return The number of queries executed on the database server since the
      * creation of this object.
      */
    function getQueriesCount()
    {
      return $this-&gt;nbQueries;
    }
    /** Go back to the first element of the result line.
      * @param $result The resssource returned by a query() function.
      */
    function resetFetch($result)
    {
      if (mysqli_num_rows($result) &gt; 0)
        mysqli_data_seek($result, 0);
    }
    /** Get the id of the very last inserted row.
      * @return The id of the very last inserted row (in any table).
      */
    function lastInsertedId()
    {
      return mysqli_insert_id($this-&gt;cnt);
    }
    /** Close the connexion with the database server.\n
      * It's usually unneeded since PHP do it automatically at script end.
      */
    function __destruct()
    {
      mysqli_close($this-&gt;cnt);
    }

    /** Internal method to get the current time.
      * @return The current time in seconds with microseconds (in float format).
      */
    function getMicroTime()
    {
      list($msec, $sec) = explode(' ', microtime());
      return floor($sec / 1000) + $msec;
    }
  } // class DB
?&gt;
</code></pre>
<p dir="auto">Premetto che non sono stato a guardare la logica della classe ma  esclusivamente il codice. Non garantisco che mi sia sfuggito qualcosa,  ma dalle prove che ho fatto funziona.</p>
]]></description><link>https://dev.connect.gt/post/1189329</link><guid isPermaLink="true">https://dev.connect.gt/post/1189329</guid><dc:creator><![CDATA[oslino]]></dc:creator><pubDate>Wed, 11 Feb 2015 23:12:20 GMT</pubDate></item><item><title><![CDATA[Reply to Classe PHP per iterazioni mysql on Wed, 11 Feb 2015 22:22:51 GMT]]></title><description><![CDATA[<p dir="auto">La classe è scritta in php 4. Il codice utilizza gli short tag &lt;? anzichè &lt;?php<br />
Lasciando tutto com'è tranne gli short tag e mettendo uno script nella stessa cartella su server locale XAMPP, ha funzionato tutto a dovere</p>
<pre><code class="language-php">&lt;?php
include "db.class.php";
$db = new DB("prova","localhost","root","");
$res = $db-&gt;query('select * from utenti');
echo mysql_num_rows($res); //risultato: 1 (esatto)
?&gt;
</code></pre>
<p dir="auto">Che il tuo sia un problema di path errato dell'include?</p>
]]></description><link>https://dev.connect.gt/post/1189328</link><guid isPermaLink="true">https://dev.connect.gt/post/1189328</guid><dc:creator><![CDATA[oslino]]></dc:creator><pubDate>Wed, 11 Feb 2015 22:22:51 GMT</pubDate></item><item><title><![CDATA[Reply to Classe PHP per iterazioni mysql on Wed, 11 Feb 2015 21:28:30 GMT]]></title><description><![CDATA[<p dir="auto">La classe l'ho trovata da qui <a href="http://slaout.linux62.org/php/index.html" rel="nofollow ugc">slaout.linux62.org/php/index.html</a><br />
Il codice era solo per provare la connessione al db quindi<br />
&lt;?php<br />
include "db.class.php";<br />
$db = new DB("nomeDB","localhost","root","psw");<br />
?&gt;<br />
O comunque qualcosa di molto simile,non ho il codice sottomana :S</p>
]]></description><link>https://dev.connect.gt/post/1189336</link><guid isPermaLink="true">https://dev.connect.gt/post/1189336</guid><dc:creator><![CDATA[luigiberducci]]></dc:creator><pubDate>Wed, 11 Feb 2015 21:28:30 GMT</pubDate></item><item><title><![CDATA[Reply to Classe PHP per iterazioni mysql on Wed, 11 Feb 2015 21:20:42 GMT]]></title><description><![CDATA[<p dir="auto">Ciao e benvenuto nel forum.<br />
Non sarebbe male se mettessi il link alla classe e il codice che hai provato.<br />
Il link copialo senza http iniziale.</p>
]]></description><link>https://dev.connect.gt/post/1189327</link><guid isPermaLink="true">https://dev.connect.gt/post/1189327</guid><dc:creator><![CDATA[oslino]]></dc:creator><pubDate>Wed, 11 Feb 2015 21:20:42 GMT</pubDate></item></channel></rss>