PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Tue Dec 01, 2020 7:15 am

All times are UTC - 5 hours




Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: MySQL table --> array
PostPosted: Wed Dec 22, 2010 5:01 pm 
Offline
Forum Newbie

Joined: Sat Dec 18, 2010 10:08 pm
Posts: 5
All,

I use this class to read all of a database table into an array. I found this VERY useful because it allows me to work in memory instead of through a window into another site (my MySQL server). I think this is much faster and easier to use. I'm sure I would modify this if the tables are large but for moderately sized tables this, I believe, is much faster.

Have your way with it! And thanks.
Syntax: [ Download ] [ Hide ]
class database
{
  private $db = null;
//--- __construct --------------------------------------------------------------------------------//
  public function __construct()
  {
    global $namecode;
    try
    {
      $this->db = new mysqli( $namecode['MoyaDotPro']['hostspec'],
                              $namecode['MoyaDotPro']['username'],
                              $namecode['MoyaDotPro']['password'],
                              $namecode['MoyaDotPro']['database'] );
      if( $this->db->connect_error ) throw new Exception( "EFM: {$this->db->connect_error}" );
    }
//------------------------------------------------------------------------------------------------//
    catch( Exception $ex )
    {
      echo "<br>\n<b>Error Message:</b> " . $ex->getMessage() . "<br>\n" .
        '<b>In File:</b> ' . $ex->getFile() . "<br>\n" .
        '<b>On Line:</b> ' .$ex->getLine() . "<br>\n";
      exit;
    }
  }
//---- end ---------------------------------------------------------------------------------------//
//--- __destruct ---------------------------------------------------------------------------------//
  public function __destruct()
  {
    if( $this->db ) { $this->db->close(); }
    $this->db = null;
  }
//---- end ---------------------------------------------------------------------------------------//
//--- Get Array ----------------------------------------------------------------------------------//
  public function GetArray( $tnam )             // $tnam = table name
  /*
   * GetArray( $table-name )
   * reads the table and makes an array ( key => array ) where the key is the array entry id value
   * returns the new array
   */

  {
    $query = "SELECT * FROM {$tnam}";
    try
    {
    $qrslt = $this->db->query( $query );
    $nrows = $this->db->affected_rows;
    switch( $nrows )
    {
    default: $r = '';
    break;
    case -1: $r = ' '.$this->db->error;
    break;
    case 0: $r = ' Empty Table';
    break;
    }
    if( !$qrslt || $r ) throw new Exception( "\n<br><b>EFM: query</b><br>" .
      "\n\"{$query}\"<br>\n<b>failed</b><br>\n<b>MySQL said:</b>{$r}" );
//------------------------------------------------------------------------------------------------//
    $aray = array();
    for( $i = 0; $i < $nrows; $i++ )
    {
      $ntre = $qrslt->fetch_assoc();
      $n = $ntre['id'];
      $aray[$n] = $ntre;                        // id based key
    }
    $qrslt->close();
    return $aray;
    }
//------------------------------------------------------------------------------------------------//
    catch( Exception $ex )
    {
      echo "<br>\n<b>Error Message:</b> " . $ex->getMessage() . "<br>\n" .
        '<b>In File:</b> ' . $ex->getFile() . "<br>\n" .
        '<b>On Line:</b> ' .$ex->getLine() . "<br>\n";
      exit;
    }
  }
//---- end ---------------------------------------------------------------------------------------//
}


Top
 Profile  
 
PostPosted: Thu Dec 23, 2010 11:33 am 
Offline
Briney Mod
User avatar

Joined: Mon Jan 19, 2004 7:11 pm
Posts: 6446
Location: 53.01N x 112.48W

_________________
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group