Egyszeru Mysql osztaly php ala [CRUD]

Indította Reconka, 2011 november 17, 04:08:46 DÉLUTÁN

Előző téma - Következő téma

Reconka

Semi transparent  MySQL  osztaly, amivel megkonnyited eleted
Kerdesed van, nyugodtan tedd fel, megprobalok valaszolni

Hasznalata:

Select resze:

$db = new Database(); 
$db->select('Account','*',"id = 15"); 
$res = $db->getResult();


INSERT resze:

$db = new Database(); 
$db->insert('account',array(3,"Myaccount","proba@enmailem.hu")); 
$res = $db->getResult();


Update resze

$db = new Database(); 
$db->('mysqlcrud',array('accname'=>'Changed!'),array('id',1)); 
$res = $db->getResult();


Delete resze

$db = new Database(); 
$db->delete('account',"email LIKE 'deletemayemail@mail.hu'"); 
$res = $db->getResult();


class Database
{

    private  $db_host;   // Database Host
    private $db_user;          // Username
    private $db_pass;          // Password
    private $db_name;          // Database
    private $con = false;               // Checks to see if the connection is active
    private $result = array();          // Results that are returned from the query


    /*
     * Connects to the database, only one connection
     * allowed
     */
   function __construct() {
    include"config.php";
    $this->db_host=$database_ip;
    $this->db_user=$database_username;
    $this->db_pass=$database_passwd;
    $this->db_name=$database_name;
    $this->connect();
   }
   function __destruct() {
       $this->disconnect();
   }   
    private  function connect()  {


        if(!$this->con)
        {
            $myconn = @mysql_connect($this->db_host,$this->db_user,$this->db_pass);
            if($myconn)
            {
                $seldb = @mysql_select_db($this->db_name,$myconn);
                if($seldb)
                {
                    $this->con = true;
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }
        else
        {
            return true;
        }
    }

    /*
    * Changes the new database, sets all current results
    * to null
    */
    private function setDatabase($name)
    {
        if($this->con)
        {
            if(@mysql_close())
            {
                $this->con = false;
                $this->results = null;
                $this->db_name = $name;
                $this->connect();
            }
        }

    }

    /*
    * Checks to see if the table exists when performing
    * queries
    */
    private function tableExists($table)
    {
        $tablesInDb = @mysql_query('SHOW TABLES FROM '.$this->db_name.' LIKE "'.$table.'"');
        if($tablesInDb)
        {
            if(mysql_num_rows($tablesInDb)==1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }

    /*
    * Selects information from the database.
    * Required: table (the name of the table)
    * Optional: rows (the columns requested, separated by commas)
    *           where (column = value as a string)
    *           order (column DIRECTION as a string)
    */
    public function select($table, $rows = '*', $where = null, $order = null)
    {

        $q = 'SELECT '.$rows.' FROM '.$table;
        if($where != null)
            $q .= ' WHERE '.$where;
        if($order != null)
            $q .= ' ORDER BY '.$order;
        //print("Debug: ".$q." <br>");  //DEBUG
        $query = @mysql_query($q);
        if($query)
        {
            $this->numResults = mysql_num_rows($query);
            for($i = 0; $i < $this->numResults; $i++)
            {
                $r = mysql_fetch_array($query);
                $key = array_keys($r);
                for($x = 0; $x < count($key); $x++)
                {
                    // Sanitizes keys so only alphavalues are allowed
                    if(!is_int($key[$x]))
                    {
                        if(mysql_num_rows($query) > 1)
                            $this->result[$i][$key[$x]] = $r[$key[$x]];
                        else if(mysql_num_rows($query) < 1)
                            $this->result = null;
                        else
                            $this->result[$key[$x]] = $r[$key[$x]];
                    }
                }
            }
            return true;
        }
        else
        {
            return false;
        }
    }
    /*
    * Insert values into the table
    * Required: table (the name of the table)
    *           values (the values to be inserted)
    * Optional: rows (if values don't match the number of rows)
    */
    public function insert($table,$values,$rows = null)
    {

        if($this->tableExists($table))
        {
            $insert = 'INSERT INTO '.$table;

            if($rows != null)
            {
                $insert .= ' ('.$rows.')';
            }

            for($i = 0; $i < count($values); $i++)
            {
                if(is_string($values[$i]))
                    $values[$i] = '"'.$values[$i].'"';
            }
            $values = implode(',',$values);
            $insert .= ' VALUES ('.$values.')';



            $ins = @mysql_query($insert);

            if($ins)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        else die("doesnt exist table");
    }
    /*
    * Dc
    */
    public function disconnect()
    {
        if($this->con)
        {
            if(@mysql_close())
            {
                           $this->con = false;
                return true;
            }
            else
            {
                return false;
            }
        }
    }

   /*
    * Deletes table or records where condition is true
    * Required: table (the name of the table)
    * Optional: where (condition [column =  value])
    */
    public function delete($table,$where = null)
    {
        if($this->tableExists($table))
        {
            if($where == null)
            {
                $delete = 'DELETE '.$table;
            }
            else
            {
                $delete = 'DELETE FROM '.$table.' WHERE '.$where;
            }
            $del = @mysql_query($delete);

            if($del)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        else
        {
            return false;
        }
    }

    /*
     * Updates the database with the values sent
     * Required: table (the name of the table to be updated
     *           rows (the rows/values in a key/value array
     *           where (the row/condition in an array (row,condition) )
     */
    public function update($table,$rows,$where)
    {
        if($this->tableExists($table))
        {
            // Parse the where values
            // even values (including 0) contain the where rows
            // odd values contain the clauses for the row
            for($i = 0; $i < count($where); $i++)
            {
                if($i%2 != 0)
                {
                    if(is_string($where[$i]))
                    {
                        if(($i+1) != null)
                            $where[$i] = '"'.$where[$i].'" AND ';
                        else
                            $where[$i] = '"'.$where[$i].'"';
                    }
                }
            }
            $where = implode('',$where);


            $update = 'UPDATE '.$table.' SET ';
            $keys = array_keys($rows);
            for($i = 0; $i < count($rows); $i++)
            {
                if(is_string($rows[$keys[$i]]))
                {
                    $update .= $keys[$i].'="'.$rows[$keys[$i]].'"';
                }
                else
                {
                    $update .= $keys[$i].'='.$rows[$keys[$i]];
                }

                // Parse to add commas
                if($i != count($rows)-1)
                {
                    $update .= ',';
                }
            }
            $update .= ' WHERE '.$where;
            $query = @mysql_query($update);
            if($query)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        else
        {
            return false;
        }
    }

    /*
    * Returns the result set
    */
    public function getResult()
    {
        return $this->result;
    }
}

imdawe

Én is írtam már egy pár ilyet de azok kicsit össze tettebek, viszont tapasztalatom szerint hasznosak mivel egy-egy projekt fejlesztési idejét lényegesen le tudják rövidíteni ha nem kell több száz karakteres query-ket írni.
Viszont ami káros hatása hogy a lefutási időt tudják növelni, de erre is van megoldás mégpedig egy erősebb vas.
Az én kódom szintaktikája a te példádtól kicsit eltérő:
Aktuális Tábla:

Aktuális tábla:
id, field, value
1, name, asd
2, lol, xd
3. asd, dgd


$DB = new DB;
$DB->SetPrefix("prefix");
$DB->Connect("host", "user", "pass", "database");
$query = $DB->table->findAll();
while($row = $query->Fetch())
        print_r($row);

Kimenet pedig a következő:

mysqli Object (
[id] => 1,
[field] => name,
[value] => asd
)
mysqli Object (
[id] => 2,
[field] => lol,
[value] => xd
)
mysqli Object (
[id] => 3,
[field] => asd,
[value] => dgd
)


Davidoff


Powered by EzPortal