Location: PHPKode > scripts > HTTP class > http-class/mysql.php
<?php
	define ("_TRACESQL", 0);
	define ("_TRACESQLINFILE", 0);
	define ("_TRACESQLFILE", "mysql.log");
	define ("ER_DB_CREATE_EXISTS", 1007);
	define ("ER_DB_DROP_EXISTS", 1008);
	define ("ER_DUP_ENTRY", 1062);
	define ("ER_BAD_TABLE_ERROR", 1051);

	class CMySql
	{
		var $mysql;
		var $fp; // for log files
		function CMySql() // Kapselung aller DB-Klassen
		{
			$this->mysql = 0;
			$fp = NULL;
		}
		function GetLastError($bDisplay)
		{
			$err = mysql_errno($this->mysql);
			if ($bDisplay) echo $err.": ".mysql_error($this->mysql)."<br>";
			return $err;
		}
		function Connect($server, $user, $pwd)
		{
			$flag = false;
			if ($this->mysql = mysql_connect($server, $user, $pwd))
			{
				if (_TRACESQLINFILE)
					$this->fp = fopen(_TRACESQLFILE, "w");
				$flag = true;
			}
			
			return $flag;
		}
		function Close()
		{
			if ($this->mysql)
				mysql_close($this->mysql);
			$this->mysql = 0;
			if (_TRACESQLINFILE && $this->fp)
			{
				fclose($this->fp);
				$this->fp = NULL;
			}
			return true;
		}
		function AddLogFileEntry($str) // only for debugging !!!!
		{
			if (_TRACESQLINFILE)
			{
				if ($this->fp)
					fputs($this->fp, $str."\r\n");
			}
		}
		function DoQuery($str)
		{
			if (_TRACESQL)
			{
				if (_TRACESQLINFILE)
				{
					if ($this->fp)
						fputs($this->fp, $str."\r\n");
				}
				else echo $str."<br>";
			}
			return mysql_query($str, $this->mysql);
		}
		function CreateDatabase($dbName)
		{
			$flag = false;
			$str = "CREATE DATABASE ".$dbName;
			if ($this->DoQuery($str))
				$flag = true;
			else if (mysql_errno($this->mysql) == ER_DB_CREATE_EXISTS)
				$flag = true;
			return $flag;
		}
		function DropDatabase($dbName)
		{
			$flag = false;
			$str = "DROP DATABASE ".$dbName;
			if ($this->DoQuery($str) || mysql_errno($this->mysql) == ER_DB_DROP_EXISTS)
				$flag = true;
			return $flag;
		}
		function SelectDatabase($dbName)
		{
			return mysql_select_db($dbName, $this->mysql);
		}
		function CreateTable($tblName)
		{
			$flag = false;
			$str = "CREATE TABLE ".$tblName." (ID INT AUTO_INCREMENT PRIMARY KEY)";
			if ($this->DoQuery($str) || mysql_errno($this->mysql) == ER_BAD_TABLE_ERROR)
				$flag = true;
			return $flag;
		}
		function DropTable($tblName)
		{
			$flag = false;
			$str = "DROP TABLE ".$tblName;
			$flag =  $this->DoQuery($str);
			return $flag;
		}
		function AddColToTable($tblName, $col)
		{
			$flag = false;
			$str = "ALTER TABLE ".$tblName." ADD ".$col;
			$flag = $this->DoQuery($str);
			return $flag;
		}
		function InsertRow($tblName, $row)
		{
			$flag = false;
			$str = "INSERT INTO ".$tblName." VALUES (".$row.")";
			$flag = $this->DoQuery($str);
			return $flag;
		}
		function InsertRowEx($tblName, $col, $row)
		{
			$flag = false;
			$str = "INSERT INTO ".$tblName." (".$col.") VALUES (".$row.")";
			$flag = $this->DoQuery($str);
			return $flag;
		}
		function GetInsertId()
		{
			return mysql_insert_id($this->mysql);
		}
		function UpdateRow($tblName, $row, $where)
		{
			$flag = false;
			$str = "UPDATE ".$tblName." SET ".$row." WHERE ".$where;
			$flag = $this->DoQuery($str);
			return $flag;
		}
		function Escape($str)
		{
			return mysql_real_escape_string($str);
		}
		function MakeSqlString($str)
		{
			$str = $this->Escape($str);
			$str = "'".$str."'";
			return $str;
		}
		function Select($sql)
		{
			return $this->DoQuery($sql);
		}
		function GetNumRows($result)
		{
			return mysql_num_rows($result);
		}
		function GetAffectedRows($result)
		{
			return mysql_affected_rows($result);
		}
		function FreeResult($result)
		{
			return mysql_free_result($result);
		}
		function FetchRow($result)
		{
			return mysql_fetch_row($result);
		}
		function FetchArray($result)
		{
			return mysql_fetch_array($result);
		}
		function DumpTableToFile($table, $file, $bGzip = true)
		{
			if ($bGzip)
				$fp = gzopen($file, "w9");
			else
				$fp = fopen($file, "w");
			if ($fp)
			{
				// Write the fields
				$str = "";
				if ($result = $this->Select("DESCRIBE ".$table))
				{
					while ($data = $this->FetchArray($result))
					{
						if (!empty($str))
							$str .= ",";
						$str .= $data['Field'];
					}
					if (!$this->FreeResult($result))
						$this->GetLastError(true);
					$str .= "\r\n";
					gzwrite($fp, $str);
				}
				else $this->GetLastError(true);

				// Write the datas
				$str = "";
				if ($result = $this->Select("SELECT * FROM ".$table))
				{
					while ($data = $this->FetchRow($result))
					{
						$str = "";
						foreach ($data as $item)
						{
							if (!empty($str))
								$str .= ",";
							$str .= $this->MakeSqlString($item);
						}
						$str .= "\r\n";
						if ($bGzip)
							gzwrite($fp, $str);
						else
							fwrite($fp, $str);
					}
					if (!$this->FreeResult($result))
						$this->GetLastError(true);
				}
				else $this->GetLastError(true);
				if ($bGzip)
					gzclose($fp);
				else
					fclose($fp);
			}
		}
		function ReadTableFromFileDump($table, $file, $bEmptyTable = true)
		{
			if ($fp = gzopen($file, "r"))
			{
				if ($bEmptyTable)
				{
					if (!$this->Select("DELETE FROM ".$table))
						$this->GetLastError(true);
				}
				
				$bFirst = true;
				$colStr = "";
				while (!gzeof($fp)) 
				{
					$str = gzgets($fp, 8192);
					if ($bFirst)
					{
						$bFirst = false;
						$colStr = $str;
					}
					else
					{
						if (!empty($str))
						{
							if (!$this->InsertRowEx($table, $colStr, $str))
								$this->GetLastError(true);
						}
					}
				}
				gzclose($fp);
			}
		}
	}
?>
Return current item: HTTP class