SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Zealot DraginX's Avatar
    Join Date
    Mar 2003
    Location
    US
    Posts
    161
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Which is better performance?

    In PHP, using arrays or using objects?

    Also I have just started using things in OOP (Object Oriented Programming).

    And I was wondering which would be faster?
    PHP Code:
    $sql = new MySQL_class;
    $sql->Query("SELECT...");
    $sql->Fetch(0);
    echo 
    $sql->data['field']; 
    Or...
    PHP Code:
    $sql mysql_query("SELECT...");
    $sql mysql_fetch_array(or object)($sql);
    echo 
    $sql['field']; 
    And do you guys know any REALLY good sites on optimizing your files and whatnut for best performance?

    P.S. Is there a way to OPTIMIZE all tables without having to use a loop or naming all the tables to optimize?

  2. #2
    o_O O_o BlueFire2k5's Avatar
    Join Date
    Mar 2003
    Location
    Sioux Falls, SD
    Posts
    475
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The $sql = mysql_query("SELECT..." ); ... code would probably be faster (by a tiny bit), due to the fact that it doesn't have to make a class and everything, but the OOP code would be more easily adapted to different things. You could change your database type and still use all of the same code, just changing the OOP, ect.

    So yeah, it's up to you. I use a class to handle all my database requests, personally.

  3. #3
    SitePoint Zealot DraginX's Avatar
    Join Date
    Mar 2003
    Location
    US
    Posts
    161
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, I was wondering if maybe you cna give me a few examples and whatnut, etc.

  4. #4
    o_O O_o BlueFire2k5's Avatar
    Join Date
    Mar 2003
    Location
    Sioux Falls, SD
    Posts
    475
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by DraginX
    Thanks, I was wondering if maybe you cna give me a few examples and whatnut, etc.
    Of what?

  5. #5
    SitePoint Zealot DraginX's Avatar
    Join Date
    Mar 2003
    Location
    US
    Posts
    161
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Of like, hrmm making a query of something, changing the DB. In my coding I require_once() a file (mysql_class.php). And here's what it looks like...
    PHP Code:
    <? 
    /* 
    * Utility routines for MySQL. 
    */ 
    class MySQL_class 
    var 
    $db$id$result$rows$data$a_rows
    var 
    $host
    function 
    Setup ($user$pass) { 
    $this->user $user
    $this->pass $pass

    function 
    Create ($db) { 
    if (!
    $this->user) { 
    $this->user "USER"

    if (!
    $this->pass) { 
    $this->pass "PASS"

    $this->db "DBNAME";
    $this->host "localhost"
    $this->id = @mysql_pconnect($this->host$this->user$this->pass) or 
    MySQL_ErrorMsg("Unable to connect to MySQL server: $this->host
    : '
    $SERVER_NAME'" ); 
    $this->selectdb($db); 

    function 
    SelectDB ($db) { 
    @
    mysql_select_db($db$this->id) or 
    MySQL_ErrorMsg ("Unable to select database: $db); 

    # Use this function is the query will return multiple rows. Use the Fetch 
    # routine to loop through those rows. 
    function Query ($query) { 
    $this->result = @mysql_query($query$this->id) or 
    MySQL_ErrorMsg ("Unable to perform query: $query); 
    $this->rows = @mysql_num_rows($this->result); 
    $this->a_rows = @mysql_affected_rows($this->result); 

    # Use this function if the query will only return a 
    # single data element. 
    function QueryItem ($query) { 
    $this->result = @mysql_query($query$this->id) or 
    MySQL_ErrorMsg ("Unable to perform query: $query); 
    $this->rows = @mysql_num_rows($this->result); 
    $this->a_rows = @mysql_affected_rows($this->result); 
    $this->data = @mysql_fetch_array($this->result) or 
    MySQL_ErrorMsg ("Unable to fetch data from query: $query); 
    return(
    $this->data[0]); 

    # This function is useful if the query will only return a 
    # single row. 
    function QueryRow ($query) { 
    $this->result = @mysql_query($query$this->id) or 
    MySQL_ErrorMsg ("Unable to perform query: $query); 
    $this->rows = @mysql_num_rows($this->result); 
    $this->a_rows = @mysql_affected_rows($this->result); 
    $this->data = @mysql_fetch_array($this->result) or 
    MySQL_ErrorMsg ("Unable to fetch data from query: $query); 
    return(
    $this->data); 

    function 
    Fetch ($row) { 
    @
    mysql_data_seek($this->result$row) or 
    MySQL_ErrorMsg ("Unable to seek data row: $row); 
    $this->data = @mysql_fetch_array($this->result) or 
    MySQL_ErrorMsg ("Unable to fetch row: $row); 

    function 
    Insert ($query) { 
    $this->result = @mysql_query($query$this->id) or 
    MySQL_ErrorMsg ("Unable to perform insert: $query); 
    $this->a_rows = @mysql_affected_rows($this->result); 

    function 
    Update ($query) { 
    $this->result = @mysql_query($query$this->id) or 
    MySQL_ErrorMsg ("Unable to perform update: $query); 
    $this->a_rows = @mysql_affected_rows($this->result); 

    function 
    Delete ($query) { 
    $this->result = @mysql_query($query$this->id) or 
    MySQL_ErrorMsg ("Unable to perform Delete: $query); 
    $this->a_rows = @mysql_affected_rows($this->result); 


    /* ******************************************************************** 
    * MySQL_ErrorMsg 

    * Print out an MySQL error message 

    */ 
    function MySQL_ErrorMsg ($msg) { 
    # Close out a bunch of HTML constructs which might prevent
    # the HTML page from displaying the error text.
    echo("</ul></dl></ol>\n" );
    echo(
    "</table></script>\n" );
    # Display the error message
    $text "<font color=\"#ff0000\" size=+2><p>Error: $msg :";
    $text .= mysql_error();
    $text .= "</font>\n";
    die(
    $text);
    }
    ?>
    Everything works beatifully but this is what I have to do everytime I run a query:
    PHP Code:
    $sql = new MySQL_class;
    $sql->Create("DBNAME"); // <----- That gets annoying :(
    $sql->Query("SELECT" );
    $sql->Fetch(0);
    echo 
    $sql->data['field']; 
    I was wonderinf if you had a much better/faster/easier system then that.

  6. #6
    o_O O_o BlueFire2k5's Avatar
    Join Date
    Mar 2003
    Location
    Sioux Falls, SD
    Posts
    475
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hm. This is my Dao class:
    PHP Code:
    class Dao
    {
        var 
    $sqlLinkId;
        var 
    $queryCount;
        function 
    connect($dbhost$dbuser$dbpass)
        {
            
    $this->sqlLinkId = @mysql_connect($dbhost,$dbuser,$dbpass) or $this->error(mysql_error().' in '.__FILE__.' on line '.__LINE__);
        }
        
        function 
    selectDb($dbname)
        {
            @
    mysql_select_db($dbname$this->sqlLinkId) or $this->error(mysql_error().' in '.__FILE__.' on line '.__LINE__);
        }
        
        function 
    query($string)
        {
            
    $this->queryCount++;
            
    $query = @mysql_query($string$this->sqlLinkId);
            if(!
    $query)
            {
                 
    $this->error(mysql_error());
                 return 
    false;
             }
            return 
    $query;
        }
        
        function 
    queryFirst($string)
        {
            
    $queryId $this->query($string);
            
    $rArray $this->fetchArray($queryId);
            
    $this->freeResult($queryId);
            return 
    $rArray;
        }
        
        function 
    escapeString($string)
        {
            return 
    mysql_real_escape_string($string);
        }
        
        function 
    fetchArray($queryId)
        {
            
    $return = @mysql_fetch_array($queryIdMYSQL_ASSOC);
            if(!isset(
    $return))
            {
                
    $this->error($php_errormsg.' in '.__FILE__.' on line '.__LINE__);
                return 
    false;
            }
            return 
    $return;
        }
        
        function 
    numRows($queryId)
        {
            
    $return = @mysql_num_rows($queryId);
            if(!isset(
    $return))
            {
                
    $this->error($php_errormsg.' in '.__FILE__.' on line '.__LINE__);
                return 
    false;
            }
            return 
    $return;
        }
        
        function 
    numFields($queryId)
        {
            
    $return = @mysql_num_fields($queryId);
            if(!isset(
    $return))
            {
                
    $this->error($php_errormsg.' in '.__FILE__.' on line '.__LINE__);
                return 
    false;
            }
            return 
    $return;
        }
        
        function 
    insertId($queryId)
        {
            
    $return = @mysql_insert_id($queryId);
            if(!isset(
    $return))
            {
                
    $this->error($php_errormsg.' in '.__FILE__.' on line '.__LINE__);
                return 
    false;
            }
            return 
    $return;
        }
        
        function 
    freeResult($queryId)
        {
            
    $return = @mysql_free_result($queryId);
            if(!isset(
    $return))
            {
                
    $this->error($php_errormsg.' in '.__FILE__.' on line '.__LINE__);
                return 
    false;
            }
            return 
    $return;
        }
        
        function 
    close()
        {
            @
    mysql_close();
        }

    I use the php.ini setting of track_errors to fill the $php_errormsg and write it to a errors.log file and display a default message, as to not give anyone a lot of error messages if something goes wrong.

    [Note that the error function is not including in this, since it's in another class that extends this class]

    This is how I would use it:
    PHP Code:
    $db = new Dao// Only have to call this once in the script
    $db->connect($dbhost$dbuser$dbpass); // Only have to call this once in the script
    $query $db->query("SELECT * FROM users");
    while(
    $r $db->fetchArray($query))
    {
        
    // blah blah
    }
    $options $db->queryFirst("SELECT * FROM options WHERE tempid = 1"); // Will return the first row of the query in an array stored in '$options' 
    That help?

  7. #7
    SitePoint Zealot DraginX's Avatar
    Join Date
    Mar 2003
    Location
    US
    Posts
    161
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes that did, thank you very much

  8. #8
    o_O O_o BlueFire2k5's Avatar
    Join Date
    Mar 2003
    Location
    Sioux Falls, SD
    Posts
    475
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by DraginX
    Yes that did, thank you very much
    Anytime.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •