Advertisement
Guest User

asasda

a guest
Dec 21st, 2010
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.14 KB | None | 0 0
  1. <?php
  2. /**
  3.  * OrestesFW Database Class
  4.  *
  5.  * Completely envisioned by Ian Macalinao
  6.  *
  7.  * @package OrestesFW
  8.  * @subpackage Database
  9.  * @since 0.1
  10.  */
  11. if (!ORESTES) { die("Access Denied."); } //Checks to see if this file is included
  12.  
  13. /**
  14.  * OrestesFW Database Access Object
  15.  *
  16.  * @package OrestesFW
  17.  * @subpackage Database
  18.  * @since 0.1
  19.  */
  20. class orestes_db {
  21.  
  22.     /**
  23.      * OrestesFW Table Prefix
  24.      *
  25.      * Just in case you have multiple installs of Orestes within the same database.
  26.      *
  27.      * @since 0.1
  28.      * @access private
  29.      * @var string
  30.      */
  31.     var $prefix;
  32.  
  33.     /**
  34.      * Connects to the database server and selects a database.
  35.      *
  36.      * PHP4 compatibility layer for calling the PHP5 constructor.
  37.      *
  38.      * @uses orestes_db::__construct() Passes parameters and returns result
  39.      * @since 0.1
  40.      *
  41.      * @param string $db_host MySQL Database Host
  42.      * @param string $db_user MySQL Database User
  43.      * @param string $db_pass MySQL Database Password
  44.      * @param string $db_name MySQL Database Name
  45.      */
  46.     function ordb($db_host, $db_user, $db_pass, $db_name) {
  47.         return $this->__construct($db_host, $db_user, $db_pass, $db_name);
  48.     }
  49.  
  50.     /**
  51.      * Connects to the database server and selects a database.
  52.      *
  53.      * PHP5 style constructor for compatibility with PHP5. Sets
  54.      * up the actual connection.
  55.      *
  56.      * @since 0.1
  57.      *
  58.      * @param string $db_host MySQL Database Host
  59.      * @param string $db_user MySQL Database User
  60.      * @param string $db_pass MySQL Database Password
  61.      * @param string $db_name MySQL Database Name
  62.      */
  63.     function __construct($db_host, $db_user, $db_pass, $db_name) {
  64.         global $db_prefix;
  65.         $link = mysql_connect($db_host, $db_user, $db_pass);
  66.         $db = mysql_select_db($db_name);
  67.         $this->prefix = $db_prefix;
  68.         return $link;
  69.     }
  70.  
  71.     /**
  72.      * Perform a MySQL database query using the current database connection
  73.      *
  74.      * Derived from PHP-Fusion.
  75.      *
  76.      * @since 0.1
  77.      *
  78.      * @param string $query Database query
  79.      * @return resource|false Returns the resource of the query or a false value if the query fails.
  80.      */
  81.     function query($query) {
  82.         $result = @mysql_query($query);
  83.         if (!$result) {
  84.             echo mysql_error();
  85.             return false;
  86.         } else {
  87.             return $result;
  88.         }
  89.     }
  90.  
  91.     /**
  92.      * Counts the number of rows matching the selected conditions
  93.      *
  94.      * Derived from PHP-Fusion.
  95.      *
  96.      * @since 0.1
  97.      *
  98.      * @param string $field The field you wish to count, usually parenthesized.
  99.      * @param string $table The table which the field is located in.
  100.      * @param string $conditions Optional. Conditions in the form of SQL query "x='y' AND y='x'".
  101.      * @return int|false Number of rows fulfilling the conditions, or if none, false
  102.      */
  103.     function count($field,$table,$conditions="") {
  104.         $cond = ($conditions ? " WHERE ".$conditions : "");
  105.         $result = @mysql_query("SELECT Count".$field." FROM ".DB_PREFIX.$table.$cond);
  106.         if (!$result) {
  107.             echo mysql_error();
  108.             return false;
  109.         } else {
  110.             $rows = mysql_result($result, 0);
  111.             return $rows;
  112.         }
  113.     }
  114.  
  115.     /**
  116.      * Performs mysql_result without errors.
  117.      *
  118.      * Derived from PHP-Fusion.
  119.      *
  120.      * @since 0.1
  121.      *
  122.      * @param resource $query MySQL Resource
  123.      * @param int $row Row number to select.
  124.      * @return mixed|false Cell data
  125.      */
  126.     function result($query, $row) {
  127.         $result = @mysql_result($query, $row);
  128.         if (!$result) {
  129.             echo mysql_error();
  130.             return false;
  131.         } else {
  132.             return $result;
  133.         }
  134.     }
  135.  
  136.     /**
  137.      * Returns the number of rows in a result set.
  138.      *
  139.      * Derived from PHP-Fusion.
  140.      *
  141.      * @since 0.1
  142.      *
  143.      * @param resource $query MySQL Resource
  144.      * @return int|false Number of rows that the query returns
  145.      */
  146.     function rows($query) {
  147.         $result = @mysql_num_rows($query);
  148.         return $result;
  149.     }
  150.  
  151.     /**
  152.      * Creates an associative array based on a MySQL Resource
  153.      *
  154.      * Derived from PHP-Fusion.
  155.      *
  156.      * @since 0.1
  157.      *
  158.      * @param resource $query MySQL Resource
  159.      * @return array Associative array of query.
  160.      */
  161.     function makearray($query) {
  162.         $result = @mysql_fetch_assoc($query);
  163.         if (!$result) {
  164.             echo mysql_error();
  165.             return false;
  166.         } else {
  167.             return $result;
  168.         }
  169.     }
  170.  
  171.     /**
  172.      * Gets a specific row from a table in an associative array.
  173.      *
  174.      * @since 0.1
  175.      *
  176.      * @param string $query Database query
  177.      * @return array The row's contents.
  178.      */
  179.     function get_row($query) {
  180.         return $this->makearray($this->query($query));
  181.     }
  182.  
  183.     /**
  184.      * Returns an array of multiple rows that match a single query
  185.      *
  186.      * @param <type> $query
  187.      * @return <type> Multi-dimensional array of rows sort
  188.      */
  189.     function results($query) {
  190.         $result = $this->query($query);
  191.         $i = 0;
  192.         while ($row = $this->makearray($result)) {
  193.             $results[$i] = $row;
  194.             $i++;
  195.         }
  196.         return $results;
  197.     }
  198.  
  199.     function arraynum($query) {
  200.         $result = @mysql_fetch_row($query);
  201.         if (!$result) {
  202.             echo mysql_error();
  203.             return false;
  204.         } else {
  205.             return $result;
  206.         }
  207.     }
  208.  
  209.     /**
  210.      * Retrieves a single database cell's contents in the current connection.
  211.      *
  212.      * Chooses the first result found, if multiple
  213.      * cells fulfill the condition. (I'm Feeling Lucky)
  214.      *
  215.      * @since 0.1
  216.      *
  217.      * @param string $query Database query
  218.      * @return mixed|false Cell data
  219.      */
  220.     function get_var($query) {
  221.         $result = $this->query($query);
  222.         return $this->result($result, 0);
  223.     }
  224. }
  225.  
  226. //Connect to the database and initialize the orestes_db object
  227. $o_d = new orestes_db($db_host, $db_user, $db_pass, $db_name);
  228. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement