2 //==============================================================================
4 // Description: OO DB interface. Currently geared towards mysql
6 // Author: johanp@aditus.nu
7 // Version: $Id: jpdb.php,v 1.3 2002/08/25 22:03:51 aditus Exp $
10 // Copyright (C) 2001,2002 Johan Persson
11 //==============================================================================
14 // SQL Result object. Returned after a query.
22 function DBResult($aDB,$aRes) {
28 $this->iDBRow = mysql_fetch_array($this->iRes);
33 $this->iDBObj = mysql_fetch_object($this->iRes);
38 if( $this->iNumRows == -1 )
39 $this->iNumRows = mysql_num_rows($this->iRes);
40 return $this->iNumRows;
43 function NumFields() {
44 if( $this->iNumFields == -1 )
45 $this->iNumFields = mysql_num_fields($this->iRes);
46 return $this->iNumFields;
49 function GetFieldNames() {
50 $nbr = $this->NumFields();
53 $meta = mysql_fetch_field($this->iRes);
54 $flds[] = $meta->name;
61 // Abstraction for a DB server
73 // If DryRun then no action on DB will be taken and all
74 // DB calls will succeed
75 var $iDryRun = false ;
77 function DBServer($aUser,$aPWD,$aServer="localhost") {
78 if( $this->iDryRun ) {
79 $this->iUserID = "DryRunUser";
80 $this->iUserPWD = "DryRunPWD";
81 $this->iServer = "DryRunServer";
84 $this->iLink = @mysql_connect($aServer,$aUser,$aPWD);
85 if( $this->iLink == false ) {
86 $this->SetError("Can't connect to server $aServer as $aUser");
89 $this->iUserID = $aUser;
90 $this->iUserPWD = $aPWD;
91 $this->iServer = $aServer;
95 function SetDB($aDBName,$aIgnoreError=false) {
96 if( $this->iDryRun ) {
97 $this->iDBName = "DryRunDBName";
100 $this->iDBName = $aDBName;
101 if( @mysql_select_db($aDBName) )
105 $this->SetError("Can't select database $aDBName.");
110 function SetDryRun($aFlg=true) {
111 $this->iDryRun = $aFlg;
114 function SetError($aMsg) {
115 if( $this->iDryRun ) {
119 $err = mysql_error($this->iLink);
122 $this->iLastErr = "<b>DB ERROR:</b>".$aMsg."<br>MySQL Error:".$err;
124 die($this->iLastErr);
128 function Query($aQuery,$aIgnoreError=false) {
129 if( $this->iDryRun ) {
132 $res=@mysql_query($aQuery,$this->iLink);
133 if( !$res && !$aIgnoreError ) {
134 $this->SetError("Error in query:<br> $aQuery<P>");
138 return new DBResult($this,$res);
143 if( $this->iDryRun ) {
146 return mysql_insert_id($this->iLink);
149 function Create($aDBName) {
150 mysql_create_db($aDBName,$this->iLink);
154 if( $this->iDryRun ) {
157 mysql_close($this->iLink);
160 function GetTables() {
161 $r = mysql_list_tables($this->iDBName,$this->iLink);
162 $n = mysql_num_rows($r);
164 for( $i=0; $i < $n; ++$i ) {
165 $tn[$i] = mysql_tablename($r,$i);
170 function GetFields($aTbl) {
171 $r = mysql_list_fields($this->iDBName,$aTbl,$this->iLink);
172 $n = mysql_num_fields($r);
174 for( $i=0; $i < $n; ++$i ) {
175 $fn[$i] = mysql_field_name($r,$i).' : '. mysql_field_type($r,$i);
180 function GetTablesFields($aFlgPrint=false) {
181 $tblnames = $this->GetTables();
182 $n = count($tblnames);
184 for( $i=0; $i < $n; ++$i ) {
185 $fn = $this->GetFields($tblnames[$i]);
187 echo '<b>'.$tblnames[$i].'</b><br>';
189 for( $j=0; $j < $nn; ++$j ) {
190 echo $fn[$j]; //[0]. ': <font color=blue>' . $fn[$j][1].'</font><br>';
194 $res[$i] = array($tblnames[$i],$fn);