Your Ad Here

Simple PHP MySQL class

Jan 22 2012 12:27 AM
php class mysql

This is just a simple PHP class to connect to and use MySQL.
Please feel free to comment and show any changes you may have made to this class.

File: mysql.class.php

<?php
 
/*
 * Date: 01/22/12
 * Simple PHP MySQL class
 * Snippet: http://adamlacombe.com/simple-php-mysql-class/7
 *
 * Adam LaCombe
 * CC BY-NC-SA 3.0
 * http://creativecommons.org/licenses/by-nc-sa/3.0/
 */

class MySQL
{
    private 
$connect;
    private 
$sql;

    public function 
__construct($host$user$pass$dbname)
    {
        
$this->connect mysql_connect($host,$user,$pass);
        
mysql_select_db($dbname,$this->connect);
    }

    public function 
execute($query)  
    {
        
$this->sql mysql_query($query);
        return 
$this->sql;
    }

    public function 
fetch_array()
    {
        
$output mysql_fetch_array($this->sql);
        return 
$output;
    }

    public function 
num_rows($query)
    {
        
$output mysql_fetch_array(mysql_query($query));
        return 
$output['total_num'];
    }

    public function 
protect($str)
    {
        
$output mysql_real_escape_string($str);
        return 
$output;
    }
}
?>




<?php
include('mysql.class.php');

$mysql = new MySQL("localhost""USER""PASS""DBNAME");

$query $mysql->execute("SELECT * FROM stuff");


while(
$row $mysql->fetch_array()){

    echo 
"".$row['name']."<br />".$row['desc']."<hr />";

}

$nums $mysql->num_rows("SELECT count(id) as total_num FROM stuff");

echo 
"<br />There are $nums rows of data";

?>




*Name:
Email:
Location:
Website:
Example: http://google.com/
*Comment


*Solve: Random Number