Location: PHPKode > scripts > MySQL data into array > class.MySqlConvert.php
<?php
/**
 * Class for convert a Mysql Db into array  .
 * 
 * @author Riccardo Castagna
 */
class MySqlConvert{

  var $dbHost;
  var $dbUser;
  var $dbPass;
  var $dbName;
 
  function connessioneDb(){
  $connect;
  $db;
  $crea;
  {
    $this->connect=MYSQL_CONNECT($this->dbHost, $this->dbUser, $this->dbPass) or die ("Error");
    $this->crea=mysql_query("CREATE DATABASE IF NOT EXISTS ".$this->dbName." ;");
    $this->db=MYSQL_SELECT_DB($this->dbName, $this->connect) or die ("Error ".$this->dbName."");
  }

    }
   
   function country(){
   $sql = "CREATE TABLE IF NOT EXISTS country (
   iso varchar(2) collate latin1_general_ci NOT NULL,
   country varchar(80) collate latin1_general_ci NOT NULL,
   PRIMARY KEY  (iso)) 
   ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;";
   
   $exesql = mysql_query($sql);
   
   $country = "INSERT INTO country (iso, country) VALUES ('AD', 'Andorra'),
   ('AF', 'Afghanistan'),('AI', 'Anguilla'),('AL', 'Albania'),('AM', 'Armenia'),
   ('AO', 'Angola'),('IT', 'Italy');";
   
    $insert = mysql_query($country);
    
    $view = "SELECT * FROM country;";
    $result = mysql_query($view);
    
    
    $q = null; //or you can write $q = ""; may be faster
    $p = null; //or you can write $p = ""; may be faster
    while ($row = mysql_fetch_array($result))
    {
    $q .= "".$row['iso']."-";
    $p .= "".$row['country']."-";
    }
    $myarray_1 = explode("-", $q);    
    $myarray_2 = explode("-", $p);
    /********** NOW I NEED TO FIND THE LAST INDEX *************/
    $n = (count($myarray_1)-2); // -2 because the array start with the [0] index
 

/***************** NOW YOU CAN PRINT WHATEVER ROW YOU WILL NEED ********************/

    echo "print the first row: ".$myarray_1[0]."&nbsp;".$myarray_2[0]."<br />";
    echo "print the second row: ".$myarray_1[1]."&nbsp;".$myarray_2[1]."<br />";
    echo "print the third row: ".$myarray_1[2]."&nbsp;".$myarray_2[2]."<br />";
    echo "print the last row: ".$myarray_1[$n]."&nbsp;".$myarray_2[$n]."<br /><br /><br />";
/***************** NOW YOU CAN PRINT WHATEVER ROW YOU WILL NEED WITH A LOOP ********/    
    echo "end if do you want print with a loop using the indexes range<br /> 
    for example from the third row to last row:<br /><br />";
          for($i = 2; $i<=$n; $i++){
          echo "print the range row: ".$myarray_1[$i]."&nbsp;".$myarray_2[$i]."<br />";
          } 
    
    }
    
    function destroydb(){
     $sql2="DROP DATABASE ".$this->dbName.";";
     $exesql2 = mysql_query($sql2);
    }
   

}
?>
Return current item: MySQL data into array