Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Apr 2012
    Posts
    89
    Thanks
    15
    Thanked 0 Times in 0 Posts

    PHP OOP grabbing results from database

    Hello all,

    Please check out my code below, does anyone have any ideas to improve this code so I can use this function for basically anything? Also if I have more "WHERE" statements, like "AND"? Any advice would be appreciated. This is purely for the purpose of learning OOP

    PHP Code:
        public function grabResults($table$field1 '*'$where NULL$field2 NULL$order NULL)
        {
                
    $result 'SELECT '.$field1.' FROM '.$table;

                if(
    $where != NULL)
            {
                    
    $result 'SELECT '.$field1.' FROM '.$table.' WHERE '.$field2.' = '.$where;
            }

                if(
    $order != NULL)
            {
                    
    $result 'SELECT '.$field1.' FROM '.$table.' WHERE '.$field2.' = '.$where.' ORDER BY '.$order.' ASC';
            }

            
    $query $this->data->mysqli->query($result);

            if(
    $query)
            {
                while(
    $row $query->fetch_assoc())
                {
                    
    $rows[] = $row;
                }
                    return 
    $rows;
            }
            else {
                return 
    false;
            }
        } 

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    This doesn't really have a whole lot on the relevance of OO approaches, so it could be used either procedurally or object oriented.
    The simplest changes to use multiple would be to take an array into the where condition and field conditions. Then you simply iterate them. Only really useful for constraining it as the types would all have to be AND conditions.
    PHP Code:
    if ($where && $field2)
    {
        
    $aCondition = array();

        if (
    is_array($where) && is_array($field2))
        {
            
    // we'll just do a simple loop since its easy to read
            
    $aWhere array_combine($field2$where);
            foreach (
    $aWhere AS $key => $value)
            {
                
    $value $this->data->mysqli->real_escape_string($value);
                
    $aCondition[] = sprintf("%s=%s"$keyis_numeric($value) ? $value "'$value'");
            }
        }
        else
        {
            
    $value $this->data->mysqli->real_escape_string($where);
            
    $aCondition[] = sprintf("%s=%s"$field2is_numeric($where) ? $where "'$where'");
        }

        
    $result .= ' WHERE ' implode(' AND '$aCondition);

    Something like this. With mysqli, you'd probably look at using prepared statements if the input is provided by users. That's a bit more of a pain in mysqli, its a piece of cake in PDO though.

    Edit:
    Oh yeah that's right. If you want more of an OOP approach, you would then provide it with class typehinting. The where condition would then become something like a ConnectionWhere class which is a class wrapper that can keep taking more and more conditions and just wrapping them up. This would allow you to provide AND and OR conditions without needing to fudge code like you would with the above.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)


  •  

    Posting Permissions

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