[PHP]MySQL Class

Results 1 to 4 of 4
  1. #1
    Account Upgraded | Title Enabled! Mindblaster7 is offline
    MemberRank
    Oct 2008 Join Date
    DenmarkLocation
    773Posts

    config [PHP]MySQL Class

    Hi. I've been working on this for a few hours, mostly to make some stuff easier for myself, make work faster and get some experience/knowledge on PHP.

    I am not aware if there is a MySQL class with PHP and most of you are laughing at me, but I think I did a good job here, and just wanted to share with you, for critic, tips and if you feel like using it or take it and develop on it, be my guest.

    Since the class is not that big (yet) I think I can permit myself to post it and not a hyperlink or a download.

    PHP Code:
    class MySQL {
            
    /*
             * Author: Mindblaster7
             * Version: 1.0
             * Last update: 06-06-2010
             */    

            
    private static $connected false;
            private static 
    $failed false;
            private static 
    $connection NULL;
            
            public function 
    connectTo($host$user$pass$db) {
                if (!
    self::isLoggedIn()) { //This check does not seem to work..
                    
    $conn mysql_connect($host$user$pass) or (die(mysql_error() && self::$failed true));
                    
    $db mysql_select_db($db) or (die(mysql_error() && self::$failed true));
                    if (
    self::$failed === false) {
                         
    self::$connected true;
                         
    self::$connection $conn;
                    } else {
                        
    self::alert('You failed to connect to the database!');   
                    }
                } else {
                    
    self::alert("You can't log in twice, please make another object of the MySQL class.");
                }
            }  
            
            public function 
    getData($sql$rowNames, &$returnTo NULL) {
                if (
    self::isLoggedIn()) {
                    
    $data mysql_query($sql);                
                    if (
    is_array($rowNames)) { 
                        
    $x 0;             
                            while (
    $row mysql_fetch_assoc($data)) {                            
                                for(
    $var 0$var <= count($rowNames); $var++) {
                                    
    $array[$rowNames[$var]][$x] = $row[$rowNames[$var]]; 
                                }
                                
    $x++;                    
                            }
                    } else { 
                        
    $row mysql_fetch_assoc($data);                    
                    }
                    if (isset(
    $array)) {
                        if (!
    $returnTo === NULL)
                            
    $returnTo $array;
                        else 
                            return 
    $array;
                    }
                    else {
                        if (!
    $returnTo === NULL)  
                            
    $returnTo $row;
                        else
                            return 
    $row[$rowNames];
                    }       
                }
                else {
                    
    self::alert("Could not get the data you requested, you were not connected to a MySQL Database.");
                }   
            }
            
            public function 
    insertData() {
                
    $args func_get_args();
                if (
    count($args) >= 2) {
                    if (
    self::isLoggedIn()) {
                        
    $sql "INSERT INTO $args[0] VALUES(";
                        for(
    $var 1$var <= count($args) - 1$var++) {
                            
    $sql .= "'{$args[$var]}'";
                            if (
    $var != count($args)- 1//Afslut SQL koden 
                                
    $sql .= ",";
                            else {
                                
    $sql .= ");";
                            }     
                        }    
                    
    mysql_query($sql) or self::alert(mysql_error());                
                    } else
                        
    self::alert("You were not logged into a MySQL database, therefor you were unable to insert your desired data.");
                } 
                
            }
            
            public function 
    deleteData() {
                
    $args func_get_args();
                if (
    count($args) >= 2) {
                    
    $sql "DELETE FROM $args[0] WHERE ";
                    for(
    $var 1$var <= count($args) - 1$var++) {
                        
    $sql .= $args[$var];
                        if (
    $var != count($args) - 1
                            
    $sql .= " AND ";
                        else {
                            
    $sql .= ";";
                        }
                    }
                
    mysql_query($sql) or self::alert(mysql_error());
                }
            }
            
            public function 
    executeSQL($sql) {
                if (
    self::isLoggedIn()) {
                    
    $query mysql_query($sql);
                    return 
    $query;
                } else
                    
    self::alert("You were not logged into a MySQL database, therefor you were unable to execute the SQL command."); 
            }
                    
            public function 
    closeCon() {
                if (
    self::isLoggedIn()) {
                    
    mysql_close(self::$connection);
                    
    self::$connected false;    
                }
                
            }
            
            private function 
    isLoggedIn() {
                return (
    self::$connected === true && self::$failed === false) ? true false;
            }
            
            private function 
    alert($message) {
                print 
    "<script type=\"text/javascript\">$message</script>";
            }

        } 
    // end of MySQL Class 
    PHP Code:
    $mysql->connectTo("localhost""root""""skema");
                    
    $mysql->deleteData("tabel""navn = 'Bob'""alder = '35'");
                    
    $mysql->insertData("tabel""""Bob""McGee""35");
                    
    $var $mysql->getData("SELECT * FROM tabel ORDER BY id", array("navn""alder"));
                    
    $length count($var['navn']) - 1;
                    for(
    $i 0$i <= $length$i++) {
                        print 
    "First name: {$var['navn'][$i]}<br />";
                        print 
    "Age: {$var['alder'][$i]}<br /><br />";
                    }
                    
    $mysql->closeCon(); 
    Output:
    Code:
    First name: Mathias
    Age: 16
    
    First name: Bob
    Age: 35


  2. #2
    Mother effin' clouds SaintsIan is offline
    MemberRank
    Apr 2008 Join Date
    fyrechat.netLocation
    2,809Posts

    Re: [PHP]MySQL Class

    Awesome work there! ;) I did something similiar but personally, I prefer working with Doctrine to make stuff easier for myself, especially with their DBAL.

    You can still improve on what you got though.
    executeSQL() seems to be an unused function.

    print "<script type=\"text/javascript\">$message</script>";
    I do believe it should be alert('Error Message Here');
    In terms of Error Handling, you should check out: set_error_handler();
    http://php.net/manual/en/function.set-error-handler.php

    I'm sure it would help :) Good luck.
    Last edited by SaintsIan; 08-06-10 at 02:17 PM.

  3. #3
    Account Upgraded | Title Enabled! Mindblaster7 is offline
    MemberRank
    Oct 2008 Join Date
    DenmarkLocation
    773Posts

    Re: [PHP]MySQL Class

    Thank you for your responce.
    About the alert function, yeah I forgot to add alert();, damn. But thank you, I will look into the error handling.

  4. #4
    Account Upgraded | Title Enabled! greetisgood is offline
    MemberRank
    May 2006 Join Date
    GermanyLocation
    512Posts

    Re: [PHP]MySQL Class

    PHP Code:
    <?php
    class MySQL
    {
        private 
    $con;
        
        public function 
    MySQL($Server "hidden",$User "hidden",$Pwd "hidden",$DB "hidden")
        {
            
    $this->con mysql_connect($Server,$User,$Pwd) or die("Konnte nicht zur Datenbank verbinden");
            
    mysql_select_db($DB) or die("Die Datenbank existiert nicht");
        }

        public function 
    Execute($query)
        {
            
    $result mysql_query($query,$this->con) or die(mysql_error());
            return 
    $result;
        }
        
        public function 
    Close()
        {
            
    mysql_close($this->con);
        }
    }
    ?>
    i use this one :)
    its enough for me ^^
    I just connect to one Server and everytime the same database.
    I mean i can insert other logins to the constructor :)

    PHP Code:
    include "chart.php";

    $sql = new MySQL();

    $res $sql->Execute("select * from table");

    while(
    $zeile mysql_fetch_row($res))
    {
       echo 
    "$zeile[0]";
    }

    $sql->Close(); 
    Last edited by greetisgood; 18-06-10 at 11:14 AM.



Advertisement