1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
|
<?php
if (!$SQL_CLASS_LOAD){
class cSQL {
var $sql_type;
var $errstr;
var $errno;
var $db;
var $num_fields;
var $num_rows;
var $no_queries;
var $result;
var $myrow;
var $sql_query;
function connect($sql_type, $hostname, $username, $password, $db){
//function connect(){
//$sql_type = 'MySQL';
//$hostname = 'localhost';
//$username = 'root';
//$password = 'xxx';
//$db = 'chat';
switch (strtolower($sql_type)){
/* MySQL */
case 'mysql' :
$this->sql_type = "MySQL";
if (($this->db = mysql_connect($hostname, $username, $password)) === FALSE){
$this->error(mysql_error(), mysql_errno());
}
if ($db != 'null'){
if ((mysql_select_db($db, $this->db)) === FALSE){
$this->error(mysql_error(), mysql_errno());
}
}
return TRUE;
break;
/* END MySQL */
/* PostgrSQL */
case 'postgresql' :
$this->sql_type = "PostgreSQL";
if (($this->db = pg_connect("host=".$hostname." dbname=".$db." user=".$username." password=".$password)) === FALSE){
$this->error(pg_errormessage(), 1);
}
return TRUE;
break;
/* END PostgreSQL */
default :
$this->error("Unkown database type '$sql_type'", 1);
break;
}
$this->no_queries = 0;
}
function select_db($db){
switch($this->sql_type){
/* MySQL */
case 'MySQL' :
if ((mysql_select_db($db, $this->db)) === FALSE){
$this->error(mysql_error(), mysql_errno());
return FALSE;
}
return TRUE;
break;
/* END MySQL */
}
}
function query($sql){
switch($this->sql_type){
/* MySQL */
case 'MySQL' :
$this->sql_query = $sql;
if (($this->result = mysql_query($sql)) === FALSE){
$this->error(mysql_error(), mysql_errno());
}
++$this->no_query;
if (is_resource($this->result)){
$this->num_fields = mysql_num_fields($this->result);
$this->num_rows = mysql_num_rows($this->result);
}
return TRUE;
break;
/* END MySQL */
/* PostgreSQL */
case 'PostgreSQL' :
$this->sql_query = $sql;
if (($this->result = pg_query($sql)) === FALSE){
$this->error(pg_errormessage(), 1);
}
++$this->no_query;
if (is_resource($this->result)){
$this->num_fields = pg_num_fields($this->result);
$this->num_rows = pg_num_rows($this->result);
}
return TRUE;
break;
/* END PostgreSQL */
}
}
function result(){
switch($this->sql_type){
/* MySQL */
case 'MySQL' :
$this->myrow = mysql_fetch_array($this->result);
return $this->myrow;
break;
/* END MySQL */
/* PostgreSQL */
case 'PostgreSQL' :
$this->myrow = pg_fetch_array($this->result);
return $this->myrow;
break;
/* END PostgreSQL */
}
}
function show_tables(){
switch($this->sql_type){
/* MySQL */
case 'MySQL' :
$this->query("show tables;");
break;
/* END MySQL */
/* PostgreSQL */
case 'PostgreSQL' :
$this->query("select * from \"pg_tables\""); //test
break;
/* END PostgreSQL */
}
}
function describe($sql_table){
switch($this->sql_type){
/* MySQL */
case 'MySQL' :
$this->query("describe ".$sql_table);
break;
/* END MySQL */
/* PostgreSQL */
case 'PostgreSQL' :
$this->query("SELECT a.attname, format_type(a.atttypid, a.atttypmod), a.attnotnull, a.atthasdef, a.attnum, col_description(a.attrelid, a.attnum) FROM pg_class c, pg_attribute a WHERE c.relname='".$sql_table."' AND a.attnum > 0 AND a.attrelid = c.oid ORDER BY a.attnum"); //test
break;
/* END PostgreSQL */
}
}
function optimize($table){
switch($this->sql_type){
/* MySQL */
case 'MySQL' :
$this->query("optimize table `".$table."`");
break;
/* END MySQL */
/* PostgreSQL */
case 'PostgreSQL' :
$this->query("vacuum \"".$table."\"");
$this->query("reindex table \"".$table."\"");
break;
/* END PostgreSQL */
}
}
function report(){
echo $this->no_query;
}
function last_insert_id(){
return mysql_insert_id($this->db);
}
function error($errstr, $errno){
echo "<FONT FACE = \"Tahoma\" SIZE = 1 COLOR = \"#FF0000\">\n";
echo $this->sql_type . " Error: <B>$errstr</B> [$errno]<BR>\n";
echo "<B>SQL Query:</B><I> ".$this->sql_query."<BR>\n";
echo "</FONT>\n";
die();
}
function close(){
switch($this->sql_type){
/* MySQL */
case 'MySQL' :
mysql_close($this->db);
break;
/* END MySQL */
/* PostgreSQL */
case 'PostgreSQL' :
pg_close($this->db);
break;
/* END PostgreSQL */
}
}
}
}
$SQL_CLASS_LOAD = 1;
?> |
Partager