Thread: MySQLi wrapper

Results 1 to 5 of 5
  1. #1 MySQLi wrapper 
    Super Donator

    Join Date
    Oct 2008
    Posts
    328
    Thanks given
    34
    Thanks received
    20
    Rep Power
    66
    It's some very old work, but still a good base, seeing as mysql_ functions will soon be completely depreciated from PHP.

    Honestly, not even 100% if this works, I can help with any problems, other than using this over mysql, I recommend PDO.

    Code:
    <?php
    /**
     * Database class
     */
    class Database
    {
    	/**
    	 * @const The database host
    	 */
    	const DATABASE_HOST = 'localhost';
    	/**
    	 * @const The database username
    	 */
    	const DATABASE_USER = 'root';
    	/**
    	 * @const The database password
    	 */
    	const DATABASE_PASS = '';
    	/**
    	 * @const The database port
    	 */
    	const DATABASE_PORT = 3306;
    	/**
    	 * @const The database name
    	 */
    	const DATABASE_NAME = 'mysqli';
    	
    	/**
    	 * @private $connection The database connection
    	 */
    	private $connection;
    	
    	/**
    	 * @private $query The last preformed MySQL Query
    	 */
    	private $query;
    	
    	/**
    	 * @var $error The last error the query
    	 */
    	var $error;
    	
    	/**
    	 * Constructs the MySQL connection, connecting to the database
    	 */
    	function __construct()
    	{
    		$this->connection = new mysqli(DATABASE_HOST, DATABASE_USER, DATABASE_PASS, NULL, DATABASE_PORT);
    		if ($this->connection->connect_error)
    		{
    			$this->error = "Unable to connect to MySQL Server.\n<!--\nMySQLi Said:\n" . $this->connection->connect_error . "\n-->\n";
    			return false;
    		}
    		if (!$this->select_db())
    		{
    			$this->error = "Unable to select database.";
    			return false;
    		}
    		return true;
    	}
    	
    	/**
    	 * Selects the database we're going to use
    	 *
    	 * @return boolean
    	 */
    	private function select_db()
    	{
    		return $this->connection->select_db(DATABASE_NAME);
    	}
    	
    	/**
    	 * Executes a simple MySQL Query
    	 *
    	 * @param   string $query The MySQL Query to execute
    	 * @return  mysql
    	 */
    	function query($query)
    	{
    		$this->query = $query;
    		$debug_text  = "";
    		$result      = null;
    		if ($result = $this->connection->query($query))
    		{
    			$MYSQL_QUERIES += 1;
    			return $result;
    		}
    		else
    		{
    			$this->error = $this->connection->error;
    			if (DEBUG)
    			{
    				$debug_text .= "\n<!--\nMySQL Query Error:\n";
    				$debug_text .= "Error number: " . $this->connection->errno . "\n";
    				$debug_text .= "Error: " . $this->connection->error . "\n";
    				$debug_text .= "Query: $this->query\n-->\n";
    				echo $debug_text;
    			}
    			return false;
    		}
    	}
    	
    	/**
    	 * Fetches data from a MySQL result
    	 *
    	 * @param  mysql  $result The MySQL query resultset
    	 * @param  int    $row   The row number to retrieve
    	 * @param  mixed  $field The field to retrieve
    	 * @return mixed
    	 */
    	function result($result, $row, $field = 0)
    	{
    		$result->data_seek($row);
    		$assoc = $this->fetch_assoc();
    		return $assoc[$field];
    	}
    	
    	/**
    	 * Returns associative array corresponding to the row
    	 *
    	 * @param  mysql $result The MySQL query resultset
    	 * @return array
    	 */
    	function fetch_assoc($result)
    	{
    		return $result->fetch_assoc();
    	}
    	
    	/**
    	 * Returns array of strings corresponding to the row
    	 *
    	 * @param  mysql $result The MySQL query resultset
    	 * @return array
    	 */
    	function fetch_array($result)
    	{
    		$results = array();
    		while ($row = $result->fetch_assoc())
    		{
    			$results[] = $row;
    		}
    		return $results;
    	}
    	
    	/**
    	 * Returns the count of rows from a result
    	 *
    	 * @param  mysql $result The MySQL query resultset
    	 * @return int
    	 */
    	function num_rows($result)
    	{
    		return $result->num_rows;
    	}
    	
    	/**
    	 * Returns the count of affected rows from a result
    	 *
    	 * @return int
    	 */
    	function affected_rows()
    	{
    		return $this->connection->affected_rows;
    	}
    	
    	/**
    	 * Returns the last insert ID
    	 *
    	 * @return int
    	 */
    	function last_insertid()
    	{
    		return $this->connection->insert_id;
    	}
    	
    	/**
    	 * Gets the MySQLi connection
    	 *
    	 * @return $mysqli
    	 */
    	function get_connection()
    	{
    		return $connection;
    	}
    	
    	/**
    	 * Escapes a string for MySQL
    	 * 
    	 * @param  mixed $string The string to be escaped
    	 * @return mixed
    	 */
    	function escape($string)
    	{
    		if (is_array($string))
    		{
    			foreach ($string as $key => $value)
    			{
    				$string[$key] = get_magic_quotes_gpc() ? $this->connection->escape_string(stripslashes($value)) : $this->connection->escape_string($value);
    			}
    			return $string;
    		}
    		else
    		{
    			return get_magic_quotes_gpc() ? $this->connection->escape_string(stripslashes($string)) : $this->connection->escape_string($string);
    		}
    	}
    }
    $db = new Database;
    MySQLi documentation

    You need the MySQLi library, which you should have, if not I recommend upgrading your PHP version, if it's disabled, just enable it in php.ini

    To use the class, use it just like MySQL really.

    Code:
    $db->query("SELECT * FROM `users` WHERE `username` = '" . escape($_POST['username']) ."');
    You can do prepared statements with MySQLi too, which is really better.

    When binding parameters, you use a question mark indicating there's going to be data inserted, then when you use the binding function, the type is indicated by a string;

    Code:
    Character 	Description
    i 	corresponding variable has type integer
    d 	corresponding variable has type double
    s 	corresponding variable has type string
    b 	corresponding variable is a blob and will be sent in packets
    MySQLi - Prepared statements

    An example of a prepared statement:

    Code:
    $mysqli = $db->get_connection();
    
    $stmt = $mysqli->prepare("SELECT * FROM `users` WHERE `username` = ? AND `userid` = ?"); // prepares to select where username = ?
    $stmt->bind_param("s", $_POST['username']); // binds "s" (string) to the first param
    $stmt->bind_param("i", $_POST['userid']); // binds "i" (integer) to the second param
    $stmt->execute(); // executes the statement
    
    while ($row = $stmt->fetch()) {
    echo "User email: $row[email]";
    }
    
    $stmt->close(); // closes the statement
    Idk, somebody mentioned it in the shoutbox.
    menth
    Reply With Quote  
     

  2. Thankful users:


  3. #2  
    Registered Member
    Join Date
    Oct 2013
    Posts
    11
    Thanks given
    1
    Thanks received
    0
    Rep Power
    11
    You should look into PDO, PHP: PDO - Manual
    Reply With Quote  
     

  4. #3  
    Registered Member
    Anthony`'s Avatar
    Join Date
    Sep 2008
    Age
    29
    Posts
    763
    Thanks given
    75
    Thanks received
    164
    Rep Power
    204
    Quote Originally Posted by xReckful View Post
    You should look into PDO, PHP: PDO - Manual
    MySQLi is optimized for MySQL, so if someone is specifically going to use MySQL then MySQLi is better than PDO in that instance.
    Reply With Quote  
     

  5. #4  
    Super Donator

    Join Date
    Oct 2008
    Posts
    328
    Thanks given
    34
    Thanks received
    20
    Rep Power
    66
    Quote Originally Posted by xReckful View Post
    You should look into PDO, PHP: PDO - Manual
    Honestly, not even 100% if this works, I can help with any problems, other than using this over mysql, I recommend PDO.
    I use PDO as stated, this is just some work I had used a while ago, PDO is great, and agreed with what Anthony said.
    menth
    Reply With Quote  
     

  6. #5  
    Banned

    Join Date
    Apr 2012
    Posts
    803
    Thanks given
    179
    Thanks received
    125
    Rep Power
    0
    Very good work man
    Reply With Quote  
     


Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

Similar Threads

  1. MySQLi Update Row
    By Intredy in forum Application Development
    Replies: 2
    Last Post: 04-14-2013, 03:28 AM
  2. Simple PHP MySQLi
    By Cody in forum Website Development
    Replies: 2
    Last Post: 03-05-2013, 10:56 AM
  3. [PHP] Introduction to MySQLi [PHP]
    By Ruby in forum Website Development
    Replies: 5
    Last Post: 09-27-2012, 12:26 AM
  4. Integraded MySQLi Trade logs System - Flawless
    By range inc in forum Selling
    Replies: 1
    Last Post: 04-16-2012, 05:03 AM
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •