Alternc  3.2
Alternc logiel libre pour l'h├ębergement
 All Data Structures Namespaces Files Functions Variables Pages
DB_Sql Class Reference

Session Management for PHP3. More...

+ Inheritance diagram for DB_Sql:

Public Member Functions

 DB_Sql ($query="")
 Constructor.
 link_id ()
 query_id ()
 connect ($Database="", $Host="", $User="", $Password="")
 function for MySQL database connection management
 free ()
 Discard the query result.
 query ($Query_String)
 Perform a query.
 next_record ()
 walk result set
 seek ($pos=0)
 public: position in result set
 lock ($table, $mode="write")
 unlock ()
 affected_rows ()
 num_rows ()
 num_fields ()
 nf ()
 np ()
 f ($Name)
 p ($Name)
 lastid ()
 nextid ($seq_name)
 metadata ($table='', $full=false)
 halt ($msg)
 haltmsg ($msg)
 table_names ()

Data Fields

 $Host = ""
 $Database = ""
 $User = ""
 $Password = ""
 $Auto_Free = 0
 $Debug = 0
 $Halt_On_Error = "no"
 $Seq_Table = "db_sequence"
 $Record = array()
 $Row
 $Errno = 0
 $Error = ""
 $type = "mysql"
 $revision = "1.2"
 $Link_ID = 0
 $Query_ID = 0
 $Query_String = ""

Detailed Description

Session Management for PHP3.

Copyright (c) 1998-2000 NetUSE AG Boris Erdmann, Kristian Koehntopp

Id:
db_mysql.php,v 1.3 2005/03/05 16:27:30 said Exp

Definition at line 13 of file db_mysql.php.

Member Function Documentation

affected_rows ( )

Definition at line 271 of file db_mysql.php.

{
return @mysql_affected_rows($this->Link_ID);
}
connect (   $Database = "",
  $Host = "",
  $User = "",
  $Password = "" 
)

function for MySQL database connection management

This function manages the connection to the MySQL database.

Parameters
$Databasename of the database
$HostDNS of the MySQL hosting server
$Userthe user's name
$Passwordthe user's password
Returns
the class variable $Link_ID

Definition at line 79 of file db_mysql.php.

References $Database, $Host, $Link_ID, $Password, $User, and halt().

Referenced by DB_users\DB_users(), lock(), metadata(), nextid(), query(), and unlock().

{
/* Handle defaults */
if ("" == $Database)
if ("" == $Host)
if ("" == $User)
if ("" == $Password)
/* establish connection, select database */
if ( 0 == $this->Link_ID ) {
$this->Link_ID=mysql_pconnect($Host, $User, $Password);
if (!$this->Link_ID) {
$this->halt("pconnect($Host, $User, \$Password) failed.");
return 0;
}
if (!@mysql_select_db($Database,$this->Link_ID)) {
$this->halt("cannot use database ".$this->Database);
return 0;
}
}
//persistent connection don't conserve database selection
//if needed do a correct database selection
$db_connected = @mysql_fetch_array(@mysql_query("SELECT DATABASE();",$this->Link_ID));
if ($db_connected[0] != $this->Database)
mysql_select_db($Database,$this->Link_ID);
}
DB_Sql (   $query = "")

Constructor.

Definition at line 49 of file db_mysql.php.

References $query, and query().

{
$this->query($query);
}
f (   $Name)

Definition at line 292 of file db_mysql.php.

{
return $this->Record[$Name];
}
free ( )

Discard the query result.

This function discards the last query result.

Definition at line 119 of file db_mysql.php.

Referenced by next_record(), and query().

{
@mysql_free_result($this->Query_ID);
$this->Query_ID = 0;
}
halt (   $msg)

Definition at line 456 of file db_mysql.php.

References haltmsg().

Referenced by connect(), lock(), metadata(), next_record(), nextid(), query(), seek(), and unlock().

{
$this->Error = @mysql_error($this->Link_ID);
$this->Errno = @mysql_errno($this->Link_ID);
if ($this->Halt_On_Error == "no")
return;
$this->haltmsg($msg);
if ($this->Halt_On_Error != "report")
die("Session halted.");
}
haltmsg (   $msg)

Definition at line 468 of file db_mysql.php.

Referenced by halt().

{
printf("</td></tr></table><b>Database error:</b> %s<br />\n", $msg);
printf("<b>MySQL Error</b>: %s (%s)<br />\n",
$this->Errno,
$this->Error);
}
lastid ( )

Definition at line 300 of file db_mysql.php.

{
return @mysql_insert_id($this->Link_ID);
}
link_id ( )
Returns
the class variable Link_ID

Definition at line 56 of file db_mysql.php.

References $Link_ID.

{
}
lock (   $table,
  $mode = "write" 
)

Definition at line 234 of file db_mysql.php.

References $key, $query, $res, $value, connect(), and halt().

Referenced by nextid().

{
$this->connect();
$query="lock tables ";
if (is_array($table)) {
while (list($key,$value)=each($table)) {
if ($key=="read" && $key!=0) {
$query.="$value read, ";
} else {
$query.="$value $mode, ";
}
}
$query=substr($query,0,-2);
} else {
$query.="$table $mode";
}
$res = @mysql_query($query, $this->Link_ID);
if (!$res) {
$this->halt("lock($table, $mode) failed.");
return 0;
}
return $res;
}
metadata (   $table = '',
  $full = false 
)

Definition at line 342 of file db_mysql.php.

References $i, $Query_ID, $res, connect(), and halt().

{
$count = 0;
$id = 0;
$res = array();
/*
* Due to compatibility problems with Table we changed the behavior
* of metadata();
* depending on $full, metadata returns the following values:
*
* - full is false (default):
* $result[]:
* [0]["table"] table name
* [0]["name"] field name
* [0]["type"] field type
* [0]["len"] field length
* [0]["flags"] field flags
*
* - full is true
* $result[]:
* ["num_fields"] number of metadata records
* [0]["table"] table name
* [0]["name"] field name
* [0]["type"] field type
* [0]["len"] field length
* [0]["flags"] field flags
* ["meta"][field name] index of field named "field name"
* The last one is used, if you have a field name, but no index.
* Test: if (isset($result['meta']['myfield'])) { ...
*/
// if no $table specified, assume that we are working with a query
// result
if ($table) {
$this->connect();
$id = @mysql_list_fields($this->Database, $table);
if (!$id)
$this->halt("Metadata query failed.");
} else {
if (!$id)
$this->halt("No query specified.");
}
$count = @mysql_num_fields($id);
// made this IF due to performance (one if is faster than $count if's)
if (!$full) {
for ($i=0; $i<$count; $i++) {
$res[$i]["table"] = @mysql_field_table ($id, $i);
$res[$i]["name"] = @mysql_field_name ($id, $i);
$res[$i]["type"] = @mysql_field_type ($id, $i);
$res[$i]["len"] = @mysql_field_len ($id, $i);
$res[$i]["flags"] = @mysql_field_flags ($id, $i);
}
} else { // full
$res["num_fields"]= $count;
for ($i=0; $i<$count; $i++) {
$res[$i]["table"] = @mysql_field_table ($id, $i);
$res[$i]["name"] = @mysql_field_name ($id, $i);
$res[$i]["type"] = @mysql_field_type ($id, $i);
$res[$i]["len"] = @mysql_field_len ($id, $i);
$res[$i]["flags"] = @mysql_field_flags ($id, $i);
$res["meta"][$res[$i]["name"]] = $i;
}
}
// free the result only if we were called on a table
if ($table) @mysql_free_result($id);
return $res;
}
next_record ( )

walk result set

This function tests if a new record is available in the current query result.

Returns
TRUE if a new record is available

Definition at line 191 of file db_mysql.php.

References free(), and halt().

{
if (!$this->Query_ID) {
$this->halt("next_record called with no query pending.");
return 0;
}
$this->Record = @mysql_fetch_array($this->Query_ID);
$this->Row += 1;
$this->Errno = mysql_errno();
$this->Error = mysql_error();
$stat = is_array($this->Record);
if (!$stat && $this->Auto_Free) {
$this->free();
}
return $stat;
}
nextid (   $seq_name)

Definition at line 305 of file db_mysql.php.

References $q, $res, connect(), halt(), lock(), and unlock().

{
$this->connect();
if ($this->lock($this->Seq_Table)) {
/* get sequence number (locked) and increment */
$q = sprintf("select nextid from %s where seq_name = '%s'",
$this->Seq_Table,
$seq_name);
$id = @mysql_query($q, $this->Link_ID);
$res = @mysql_fetch_array($id);
/* No current value, make one */
if (!is_array($res)) {
$currentid = 0;
$q = sprintf("insert into %s values('%s', %s)",
$this->Seq_Table,
$seq_name,
$currentid);
$id = @mysql_query($q, $this->Link_ID);
} else {
$currentid = $res["nextid"];
}
$nextid = $currentid + 1;
$q = sprintf("update %s set nextid = '%s' where seq_name = '%s'",
$this->Seq_Table,
$nextid,
$seq_name);
$id = @mysql_query($q, $this->Link_ID);
$this->unlock();
} else {
$this->halt("cannot lock ".$this->Seq_Table." - has it been created?");
return 0;
}
return $nextid;
}
nf ( )

Definition at line 284 of file db_mysql.php.

References num_rows().

{
return $this->num_rows();
}
np ( )

Definition at line 288 of file db_mysql.php.

References num_rows().

{
print $this->num_rows();
}
num_fields ( )

Definition at line 279 of file db_mysql.php.

{
return @mysql_num_fields($this->Query_ID);
}
num_rows ( )

Definition at line 275 of file db_mysql.php.

Referenced by nf(), np(), and seek().

{
return @mysql_num_rows($this->Query_ID);
}
p (   $Name)

Definition at line 296 of file db_mysql.php.

{
print $this->Record[$Name];
}
query (   $Query_String)

Perform a query.

This function performs the MySQL query described in the string parameter

Parameters
astring describing the MySQL query
Returns
the $Query_ID class variable (null if fails)

Definition at line 132 of file db_mysql.php.

References $Query_ID, $Query_String, connect(), free(), halt(), isset, and THROW_EXCEPTIONS.

Referenced by DB_Sql(), and table_names().

{
global $debug_alternc;
/* No empty queries, please, since PHP4 chokes on them. */
if ($Query_String == "")
/* The empty query string is passed on from the constructor,
* when calling the class without a query, e.g. in situations
* like these: '$db = new DB_Sql_Subclass;'
*/
return 0;
if (!$this->connect()) {
return 0; /* we already complained in connect() about that. */
};
# New query, discard previous result.
if ($this->Query_ID) {
$this->free();
$this->Query_String = $Query_String;
}
if ($this->Debug)
printf("Debug: query = %s<br />\n", $Query_String);
$debug_chrono_start = microtime(true);
$this->Query_ID = @mysql_query($Query_String,$this->Link_ID);
$debug_chrono_start = (microtime(true) - $debug_chrono_start)*1000;
$this->Row = 0;
$this->Errno = mysql_errno();
$this->Error = mysql_error();
if( 0 != $this->Errno ){
if( defined("THROW_EXCEPTIONS") && THROW_EXCEPTIONS ){
throw new \Exception("Mysql query failed : $this->Error");
}
$this->halt("SQL Error: ".$Query_String);
return FALSE;
}
if (!$this->Query_ID) {
$this->halt("Invalid SQL: ".$Query_String);
}
if (isset($debug_alternc)) {
$debug_alternc->add("SQL Query : (".substr($debug_chrono_start,0,5)." ms)\t $Query_String");
$debug_alternc->nb_sql_query++;
$debug_alternc->tps_sql_query += $debug_chrono_start;
}
# Will return nada if it fails. That's fine.
}
query_id ( )
Returns
the class variable Query_ID

Definition at line 63 of file db_mysql.php.

References $Query_ID.

{
}
seek (   $pos = 0)

public: position in result set

Definition at line 214 of file db_mysql.php.

References halt(), and num_rows().

{
$status = @mysql_data_seek($this->Query_ID, $pos);
if ($status)
$this->Row = $pos;
else {
$this->halt("seek($pos) failed: result has ".$this->num_rows()." rows");
/* half assed attempt to save the day,
* but do not consider this documented or even
* desireable behaviour.
*/
@mysql_data_seek($this->Query_ID, $this->num_rows());
$this->Row = $this->num_rows;
return 0;
}
return 1;
}
table_names ( )

Definition at line 475 of file db_mysql.php.

References $Database, $i, $info, and query().

{
$this->query("SHOW TABLES");
$i=0;
while ($info=mysql_fetch_row($this->Query_ID))
{
$return[$i]["table_name"]= $info[0];
$return[$i]["tablespace_name"]=$this->Database;
$return[$i]["database"]=$this->Database;
$i++;
}
return $return;
}
unlock ( )

Definition at line 258 of file db_mysql.php.

References $res, connect(), and halt().

Referenced by nextid().

{
$this->connect();
$res = @mysql_query("unlock tables", $this->Link_ID);
if (!$res) {
$this->halt("unlock() failed.");
return 0;
}
return $res;
}

Field Documentation

$Auto_Free = 0

Definition at line 22 of file db_mysql.php.

$Database = ""

Definition at line 17 of file db_mysql.php.

Referenced by connect(), and table_names().

$Debug = 0

Definition at line 23 of file db_mysql.php.

$Errno = 0

Definition at line 32 of file db_mysql.php.

$Error = ""

Definition at line 33 of file db_mysql.php.

$Halt_On_Error = "no"

Definition at line 24 of file db_mysql.php.

$Host = ""

Definition at line 16 of file db_mysql.php.

Referenced by connect().

$Link_ID = 0

Definition at line 40 of file db_mysql.php.

Referenced by connect(), and link_id().

$Password = ""

Definition at line 19 of file db_mysql.php.

Referenced by connect().

$Query_ID = 0

Definition at line 41 of file db_mysql.php.

Referenced by metadata(), query(), and query_id().

$Query_String = ""

Definition at line 42 of file db_mysql.php.

Referenced by query().

$Record = array()

Definition at line 28 of file db_mysql.php.

$revision = "1.2"

Definition at line 37 of file db_mysql.php.

$Row

Definition at line 29 of file db_mysql.php.

$Seq_Table = "db_sequence"

Definition at line 25 of file db_mysql.php.

$type = "mysql"

Definition at line 36 of file db_mysql.php.

$User = ""

Definition at line 18 of file db_mysql.php.

Referenced by connect().


The documentation for this class was generated from the following file: