Looking for a simple MySQL class...

Here non-phpBB topics can be discussed.
User avatar
Mike TUMS
Member
Member
Posts: 162
Joined: 16 Jan 2007, 05:51
Real name: Mihail
Location: Moscow, RU

Looking for a simple MySQL class...

Postby Mike TUMS » 15 Jun 2008, 18:58

subj =)
Image
Introduction forum's welcome bot. Guru of Indian-style coding .
Russian spy. Master of copypasta. Google's right hand.
Advanced lamer of Modders.


May translate your MOD to Russian.

User avatar
igorw
Past Contributor
Past Contributor
Posts: 1967
Joined: 01 Jun 2006, 20:48
Real name: Igor

Re: Looking for a simple MySQL class...

Postby igorw » 15 Jun 2008, 19:49

Not perfect, but it's usable. It's inspired by phpBB 2.0 and 3.0, plus using PHP5 features.

Code: Select all

<?php
/**
 * Database class
 * includes/db.php
 *
 * The Wiedler Website v3
 *
 * @author      Igor Wiedler
 * @copyright   (c) 2007 wiedler.ch
 * @link      http://wiedler.ch
 * @version      1.0.0
 */

class db
{
   /**
    * The connection id
    *
    * @var int
    */
   public $con_id = 0;

   /**
    * Total number of queries
    *
    * @var int
    */
   public $query_count = 0;

   /**
    * Constructor, connect to db
    *
    * @param   string   $dbhost
    * @param   string   $dbuser
    * @param   string   $dbpass
    * @param   string   $dbname
    * @return   db
    */
   public function __construct($dbhost, $dbuser, $dbpass, $dbname)
   {
      if (false === ($this->con_id = @mysql_connect($dbhost, $dbuser, $dbpass)))
      {
         trigger_error('Failed to connect to sql server', E_USER_ERROR);
      }

      if (!mysql_select_db($dbname, $this->con_id))
      {
         trigger_error('Failed to select database ' . $dbname, E_USER_ERROR);
      }

      @mysql_query("SET NAMES 'utf8'", $this->con_id);

      return;
   }

   /**
    * Close the existing connection
    *
    * @return bool mysql_close()
    */
   public function sql_close()
   {
      return mysql_close($this->con_id);
   }

   /**
    * Query the SQL Database
    *
    * @param   string   $sql
    * @return   int      result
    */
   public function sql_query($sql)
   {
      $this->query_count++;

      if (false === ($result = mysql_query($sql, $this->con_id)))
      {
         trigger_error($this->sql_error($result) . '<br /><br />' . $sql, E_USER_ERROR);
      }

      return $result;
   }

   /**
    * Fetch data from a query result
    *
    * @param   int   $result
    * @return   array data
    */
   public function sql_fetchrow($result)
   {
      return mysql_fetch_assoc($result);
   }

   /**
    * Fetch a whole rowset from a query result
    *
    * @param   int   $result
    * @return   array data
    */
   public function sql_fetchrowset($result)
   {
      $data = array();
      while ($row = $this->sql_fetchrow($result))
      {
         $data[] = $row;
      }
      return $data;
   }

   /**
    * Get number of rows
    *
    * @param   int   $result
    * @return   int   number of rows
    */
   public function sql_numrows($result)
   {
      return (int) mysql_num_rows($result);
   }

   /**
    * Next mysql id
    *
    * @return next id
    */
   public function sql_nextid()
   {
      return ($this->con_id) ? mysql_insert_id($this->con_id) : false;
   }

   /**
    * Escape a string for db insertion
    *
    * @param   string   $string
    * @return   string   escaped string
    */
   public function sql_escape($string)
   {
      return mysql_real_escape_string($string, $this->con_id);
   }

   /**
    * Free a query result
    *
    * @param   int      $result
    * @return   bool   result freed
    */
   public function sql_freeresult($result)
   {
      return mysql_free_result($result);
   }

   /**
    * Get the error of the last sql query
    *
    * @param   int      $result
    * @return   string   error str
    */
   public function sql_error($result)
   {
      return mysql_error($this->con_id);
   }

   /**
    * Build an sql part from an array
    *
    * @param string $mode
    * @param array $sql_ary
    * @return string sql query part
    */
   public function sql_build($mode, $sql_ary)
   {
      switch ($mode)
      {
         case 'update':
            $output = '';
            foreach ($sql_ary as $field => $value)
            {
               $output .= (!empty($output) ? ',' : '') . " $field = " . (is_int($value) ? $value : "'" . $this->sql_escape($value) . "'") . ' ';
            }
            return $output;
            break;

         case 'insert':
            $output1 = $output2 = '';
            foreach ($sql_ary as $field => $value)
            {
               $output1 .= (!empty($output1) ? ',' : '') . ' ' . $field . ' ';
               $output2 .= (!empty($output2) ? ',' : '') . (is_int($value) ? $value : "'" . $this->sql_escape($value) . "'") . ' ';
            }
            $output = "($output1) VALUES ($output2)";
            return $output;
            break;

         default:
            return;
            break;
      }
   }

   function sql_in_set($in_ary)
   {
      $output = '';
      foreach ($in_ary as $item)
      {
         $output .= (!empty($output) ? ',' : '') . (is_int($item) ? $item : "'" . $this->sql_escape($item) . "'") . ' ';
      }

      return $output;
   }
}


?>

User avatar
Mike TUMS
Member
Member
Posts: 162
Joined: 16 Jan 2007, 05:51
Real name: Mihail
Location: Moscow, RU

Re: Looking for a simple MySQL class...

Postby Mike TUMS » 15 Jun 2008, 19:55

Damn you, Igor, you even don't know how I love your classes =)
Image
Introduction forum's welcome bot. Guru of Indian-style coding .
Russian spy. Master of copypasta. Google's right hand.
Advanced lamer of Modders.


May translate your MOD to Russian.

User avatar
igorw
Past Contributor
Past Contributor
Posts: 1967
Joined: 01 Jun 2006, 20:48
Real name: Igor

Re: Looking for a simple MySQL class...

Postby igorw » 15 Jun 2008, 19:59

Hehe, thank you michael :D

User avatar
Mike TUMS
Member
Member
Posts: 162
Joined: 16 Jan 2007, 05:51
Real name: Mihail
Location: Moscow, RU

Re: Looking for a simple MySQL class...

Postby Mike TUMS » 15 Jun 2008, 20:01

Some manual will be real nice :P
Image
Introduction forum's welcome bot. Guru of Indian-style coding .
Russian spy. Master of copypasta. Google's right hand.
Advanced lamer of Modders.


May translate your MOD to Russian.

User avatar
igorw
Past Contributor
Past Contributor
Posts: 1967
Joined: 01 Jun 2006, 20:48
Real name: Igor

Re: Looking for a simple MySQL class...

Postby igorw » 15 Jun 2008, 20:17

Oh come on :P

Code: Select all

$db = new db($dbhost, $dbuser, $dbpass, $dbname);

$result = $db->sql_query("SELECT something FROM somewhere WHERE something_else = '{$db->sql_escape($var)}'");
$row = $db->sql_fetchrow($result);
$db->sql_freeresult($result);

echo $db->query_count;

$db->sql_close();


Return to “General Discussion”

Who is online

Users browsing this forum: CommonCrawl [Bot], Mojeek [Bot] and 0 guests