MySQL table --> array
Posted: Wed Dec 22, 2010 4:01 pm
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.
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.
Code: Select all
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 ---------------------------------------------------------------------------------------//
}