MyBB Community Forums

Full Version: mybb 1.2 db_mysql.php
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
so ive got some old db queries that look like this:
$query = $db->query("SELECT * FROM ".TABLE_PREFIX."shouts ORDER BY dateline LIMIT $start, 5");
simple selects i think

but how do i convert these to the new versions?

        /**
         * Performs a simple select query.
         *
         * @param string The table name to be queried.
         * @param string Comma delimetered list of fields to be selected.
         * @param string SQL formatted list of conditions to be matched.
         * @param array List of options, order by, order direction, limit, limit start
         */

        function simple_select($table, $fields="*", $conditions="", $options=array())
        {
                $query = "SELECT ".$fields." FROM ".$table;
                if($conditions != "")
                {
                        $query .= " WHERE ".$conditions;
                }
                if(isset($options['order_by']))
                {
                        $query .= " ORDER BY ".$options['order_by'];
                        if(isset($options['order_dir']))
                        {
                                $query .= " ".strtoupper($options['order_dir']);
                        }
                }
                if(isset($options['limit_start']) && isset($options['limit']))
                {
                        $query .= " LIMIT ".$options['limit_start'].", ".$options['limit'];
                }
                elseif(isset($options['limit']))
                {
                        $query .= " LIMIT ".$options['limit'];
                }
                return $this->query($query);
        }

        /**
         * Performs a select query
         *
         * @param array Array of query information
         * @return resource The query data.
         */
        function select_query($data)
        {
                $select = array($data['select']);
                $join_sql = '';
                if(is_array($data['joins']))
                {
                        foreach($data['joins'] as $join)
                        {
                                if($join['select'])
                                {
                                        $select[] = $join['select'];
                                }
                                if($join['type'] == "left")
                                {
                                        $join_sql .= "LEFT JOIN {$join['table']} ";
                                        if($join['where'])
                                        {
                                                $join_sql .= "ON ({$join['where']}) ";
                                        }
                                }
                                else if($join['type'] == "inner")
                                {
                                        $join_sql .= "INNER JOIN {$join['table']} ";
                                        if($join['where'])
                                        {
                                                $join_sql .= "ON ({$join['where']}) ";
                                        }
                                }
                        }
                }
                $select = implode(", ", $select);
                $rows = "";
                if($data['rows'] != "")
                {
                        $rows = "({$data['rows']})";
                }
                $query = "SELECT {$select} FROM {$data['table']} $rows {$join_sql}";
                if($data['where'])
                {
                        $query .= " WHERE {$data['where']}";
                }
                if($data['order'])
                {
                        $query .= " ORDER BY {$data['order']}";
                }
                if(is_array($data['limit']))
                {
                        $query .= " LIMIT {$data['limit'][0]}";
                        if($data['limit'][1])
                        {
                                $query .= ", {$data['limit'][1]}";
                        }
                }
                return $this->query($query);
        }


        /**
         * Build an insert query from an array.
         *
         * @param string The table name to perform the query on.
         * @param array An array of fields and their values.
         * @return resource The query data.
         */
        function insert_query($table, $array)
        {
                $comma = $query1 = $query2 = "";
                if(!is_array($array))
                {
                        return false;
                }
                $comma = "";
                $query1 = "";
                $query2 = "";
                foreach($array as $field => $value)
                {
                        $query1 .= $comma.$field;
                        $query2 .= $comma."'".$value."'";
                        $comma = ", ";
                }
                return $this->query("
                        INSERT
                        INTO ".$table." (".$query1.")
                        VALUES (".$query2.")
                ");
        }
You don't have to convert them if you don't want to convert them.

Essentially you'd use simple_select in the following format:

$query = $db->simple_select(TABLE_PREFIX."shouts", "*", "", array("order_by" => "dateline", "order_dir" => "desc", "limit_start" => $start, "limit" => 5));

In terms of the select_query() method, this currently is not used in MyBB.
oh so $db->query() still works?

thats good!
what about $db->result() then? i know thats changed.
It is now $db->fetch_field and now accepts the field name:
Quote: /**
* Return a specific field from a query.
*
* @param resource The query ID.
* @param string The name of the field to return.
* @param int The number of the row to fetch it from.
*/

Example:

$query = $db->query("SELECT username FROM ".TABLE_PREFIX."users WHERE uid='1'");
$username = $db->fetch_field($query, "username");