Chip Wiegand [EMAIL PROTECTED] wrote
I have a database, all the data is numbers. I want to make a query that
will extract the data and then make it available in an array, so the
array is populated 'real-time'. I could just enter the number into an
array manually, but I want to automate the job. I don't know what to
start looking for, the 3 books I have either don't talk about this or I
just am missing it.
Could someone point me in the right direction?
This class makes it possible to return an array in two ways very easy.
Just try this
$db = new Ms_db;
$db-connect(true); // open a persistant connection
$vars1 = $db-query(SELECT * FROM table);
$vars2 = $db-query(SELECT * FROM table,off);
$vars1 is now build this way
$vars1[row][column] contains the required value...
$vars2[column][row] contains the required value...
Maybe this helps:
?php
/**
mySQL - Database Class
**/
// Database Configuration Variables (local) //
define (DB_SERVER,localhost);
define (DB_USER,root);
define (DB_PASS,);
define (DB_DB,testfaq);
class Ms_db
{
/**
* Attributes of this class
**/
var $db_result = 0; // Stores the last result_id
var $db_id = 0; // Stores the current Database Connection ID
var $db_datasets = array(); // Stores the last read database-Set as hash
[colum-name][row_number]
/**
* Connect
* @author Marcel Schindler
* @param bool $persistant
* @return void
**/
function connect($persistant =
FALSE,$server=DB_SERVER,$user=DB_USER,$pass=DB_PASS,$database=DB_DB)
{
if (($persistant == FALSE))
{
$this-db_id = @mysql_connect($server,$user,$pass)
or $this-db_error(Failed to connect to
Database-Server,mysql_error());
}
else
{
$this-db_id = @mysql_pconnect($server,$user,$pass)
or $this-db_error(Persistant connection
failed,mysql_error());
}
@mysql_select_db($database) or $this-db_error(Failed to select
Database,mysql_error());
}
/**
* Query
* @param SQL-Query
* @param flip boolean
* @return array $dataset
**/
function query($sql,$flipped=on)
{
$db = $this-db_id;
$wert = array();
$count= 0;
if (!$db) $this-db_error(Keine Verbindung zur Datenbank);
$res = @mysql_query($sql) or $this-db_error(Fehlerhaftes
SQL-Statement,$sql.br.mysql_error());
while ($data = mysql_fetch_assoc($res))
{
$wert[$count] = $data;
$count ;
}
if ($flipped == on) $wert = $this-reorder_array($wert);
$this-db_datasets=$wert;
return $wert;
}
/**
* Close - closes the connection to the Database
**/
function close()
{
mysql_close();
}
/**
* Reorder_array
* @param: $array = array();
* @return: $array;
**/
function reorder_array($arr)
{
$wert = array();
// Array ist vom Typ $wert[Zeile][Spalte], soll aber als
$wert[Spalte][Zeile]
foreach ($arr as $sub)
{
while (list ($key1,$val1) = each ($sub))
{
$wert[$key1][] = $val1;
}
}
$this-db_datasets = $wert;
return $wert;
}
/**
* db_error - displays a well-formatted HTML-Page with the error-message
* @param string errormessage
* @param string mysql-error (optional)
**/
function db_error($message,$error='')
{
echo 'htmlheadtitle'.$message.'/title/headbody
bgcolor=#ee';
echo 'table align=center width=600 height=400
bgcolor=#ff';
echo 'trtdh1Error:/h1/td/trtrtd';
highlight_string($message);
if ($error!='')
{
echo 'brbrMySQL said:'.$error;
}
echo '/td/tr/table/body/html';
die();
}
/**
* NumRows
* Checks the Number of rows affected by the last query
* @return integer $number
**/
function numrows()
{
$number = @mysql_num_rows($this-db_result);
return $number;
}
/**
* DUMP - just dumps the current array (just for informal purposes
* @param VOID
**/
function dump()
{
$x = $this-numrows();
if ($x == 0 ) $this-db_error(There was no query placed
before,);
echo 'strongQuery DUMP/strongbrtable border=1
cellspacing=0 cellpadding=0 width=100%tr';
$wert = $this-db_datasets;
$spalten = array_keys($wert);
foreach ($spalten as $values)
{
$spaltenname[] = $values;
echo th bgcolor=\#00\font
color=\#ff\$values/font/th;
}
echo /tr;
for ($t = 0; $t = $x; $t )
{
if ($t % 2 == 0) echo tr bgcolor=\#dd\;
else echo tr bgcolor=\#ee\;
foreach($spaltenname as $values)
{
echo td vAlign=\top\.$wert[$values][$t]./td