Ravens PHP Scripts: Forums
 

 

View next topic
View previous topic
Post new topic   Reply to topic    Ravens PHP Scripts And Web Hosting Forum Index -> MySQL
Author Message
testy1
Involved
Involved



Joined: Apr 06, 2008
Posts: 484

PostPosted: Wed Oct 01, 2008 5:58 pm Reply with quote

Ive been playing with raven trying to use the mysqli extension.I would like to get this working and eventually have a look at using stored procedures for some of the common queries, as I think there could be significant speed increases in this area.

so far I have come up with the following, although I am not sure how to change

Code:


mysql_field_name
mysql_field_type


Im thinking mysqli_fetch_field or mysqli_fetch_field_direct ???

anyway here is the class Ive been working on which was originally the mysql.php file in raven

Code:


<?php
/***************************************************************************
 *                                 mysql.php
 *                            -------------------
 *   begin                : Saturday, Feb 13, 2001
 *   copyright            : (C) 2001 The phpBB Group
 *   email                : [ Only registered users can see links on this board! Get registered or login! ]
 *
 *   $Id: mysql.php,v 1.16.2.1 2005/09/18 16:17:20 acydburn Exp $
 *
 ***************************************************************************/

/***************************************************************************
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 ***************************************************************************/

if(!defined('SQL_LAYER'))
{

define('SQL_LAYER','mysqli');

class sql_db
{

   var $db_connect_id;
   var $query_result;
   var $row = array();
   var $rowset = array();
   var $num_queries = 0;

   //
   // Constructor
   //
   function sql_db($sqlserver, $sqluser, $sqlpassword, $database, $persistency = true)
   {

      $this->persistency = $persistency;
      $this->user = $sqluser;
      $this->password = $sqlpassword;
      $this->server = $sqlserver;
      $this->dbname = $database;

/*
      if($this->persistency)
      {
         $this->db_connect_id = @mysql_pconnect($this->server, $this->user, $this->password);
      }
      else
      {
         $this->db_connect_id = @mysql_connect($this->server, $this->user, $this->password);
      }
*/
// seems there is no persistancy in mysqli
                $this->db_connect_id = @mysqli_connect($this->server, $this->user, $this->password);
      if($this->db_connect_id)
      {
         if(!empty($database))
         {
            $this->dbname = $database;
            $dbselect = @mysqli_select_db($this->dbname);
            if(!$dbselect)
            {
               @mysqli_close($this->db_connect_id);
               $this->db_connect_id = $dbselect;
            }
         }
         return $this->db_connect_id;
      }
      else
      {
         return false;
      }
   }

   //
   // Other base methods
   //
   function sql_close()
   {
      if($this->db_connect_id)
      {
         if($this->query_result)
         {
            @mysqli_free_result($this->query_result);
         }
         $result = @mysqli_close($this->db_connect_id);
         return $result;
      }
      else
      {
         return false;
      }
   }

   //
   // Base query method
   //
   function sql_query($query = '', $transaction = FALSE)
   {
      global $loglevel, $querycount;
   // Remove any pre-existing queries
      unset($this->query_result);
      if(!empty($query))
         {
         $this->query_result = @mysql_query($query, $this->db_connect_id);
         }
         if ($loglevel == 2) {
            $querycount = $querycount + 1;
            $fplog = fopen(NUKE_BASE_DIR.'rnlogs/dblog','a');
            $logvar = date("F j, Y, g:i a") . ' ' ;
            $logvar .= 'SQL was: ' . preg_replace('/\s+/', ' ', trim($query)) . "\n";
            $logvar .= 'querycount = ' . $querycount . "\n";
            fwrite($fplog, "$logvar" . "\n");
            fclose($fplog);
      }
      if($this->query_result)
      {
         unset($this->row[$this->query_result]);
         unset($this->rowset[$this->query_result]);
         return $this->query_result;
      }
      else
         {
            if ($loglevel > 0) {
               $error = $this->sql_error($query);
               $fplog = fopen(NUKE_BASE_DIR.'rnlogs/dblog','a');
               $logvar = date("F j, Y, g:i a") . ' ' ;
               $logvar .= $error['code'] . ' : ' .  $error['message'] . "\n";
               $logvar .= 'SQL was: ' . preg_replace('/\s+/', ' ', trim($query)) . "\n";
               $logvar .= ' remote addr: ' . $_SERVER['REMOTE_ADDR'];
               fwrite($fplog, "$logvar" . "\n");
               fclose($fplog);
            }
         return ( $transaction == END_TRANSACTION ) ? true : false;
      }
   }

   //
   // Other query methods
   //
   function sql_numrows($query_id = 0)
   {
      if(!$query_id)
      {
         $query_id = $this->query_result;
      }
      if($query_id)
      {
      $result = @mysqli_num_rows($query_id);
         return $result;
      }
      else
      {
         return false;
      }
   }
   function sql_affectedrows()
   {
      if($this->db_connect_id)
      {
      $result = @mysqli_affected_rows($this->db_connect_id);
         return $result;
      }
      else
      {
         return false;
      }
   }
   function sql_numfields($query_id = 0)
   {
      if(!$query_id)
      {
         $query_id = $this->query_result;
      }
      if($query_id)
      {
      $result = @mysqli_num_fields($query_id);
         return $result;
      }
      else
      {
         return false;
      }
   }
   function sql_fieldname($offset, $query_id = 0)
   {
      if(!$query_id)
      {
         $query_id = $this->query_result;
      }
      if($query_id)
      {
         $result = @mysql_field_name($query_id, $offset);
         return $result;
      }
      else
      {
         return false;
      }
   }
   function sql_fieldtype($offset, $query_id = 0)
   {
      if(!$query_id)
      {
         $query_id = $this->query_result;
      }
      if($query_id)
      {
         $result = @mysql_field_type($query_id, $offset);
         return $result;
      }
      else
      {
         return false;
      }
   }
   function sql_fetchrow($query_id = 0)
   {
      if(!$query_id)
      {
         $query_id = $this->query_result;
      }
      if($query_id)
      {
         $this->row[(int)$query_id] = @mysql_fetch_array($query_id);
         return $this->row[(int)$query_id];
      }
      else
      {
         return false;
      }
   }
   function sql_fetchrowset($query_id = 0)
   {
      if(!$query_id)
      {
         $query_id = $this->query_result;
      }
      if($query_id)
      {
         unset($this->rowset[$query_id]);
         unset($this->row[$query_id]);
         while($this->rowset[$query_id] = @mysqli_fetch_array($query_id))
         {
            $result[] = $this->rowset[$query_id];
         }
         return $result;
      }
      else
      {
         return false;
      }
   }
   function sql_fetchfield($field, $rownum = -1, $query_id = 0)
   {
      if(!$query_id)
      {
         $query_id = $this->query_result;
      }
      if($query_id)
      {
         if($rownum > -1)
         {
            $result = @mysqli_result($query_id, $rownum, $field);
         }
         else
         {
            if(empty($this->row[$query_id]) && empty($this->rowset[$query_id]))
            {
               if($this->sql_fetchrow())
               {
                  $result = $this->row[$query_id][$field];
               }
            }
            else
            {
               if($this->rowset[$query_id])
               {
                  $result = $this->rowset[$query_id][0][$field];
               }
               else if($this->row[$query_id])
               {
                  $result = $this->row[$query_id][$field];
               }
            }
         }
         return $result;
      }
      else
      {
         return false;
      }
   }
   function sql_rowseek($rownum, $query_id = 0){
      if(!$query_id)
      {
         $query_id = $this->query_result;
      }
      if($query_id)
      {
      $result = @mysqli_data_seek($query_id, $rownum);
         return $result;
      }
      else
      {
         return false;
      }
   }
   function sql_nextid(){
      if($this->db_connect_id)
      {
      $result = @mysqli_insert_id($this->db_connect_id);
         return $result;
      }
      else
      {
         return false;
      }
   }
   function sql_freeresult($query_id = 0){
      if(!$query_id)
      {
         $query_id = $this->query_result;
      }

      if ( $query_id )
      {
         unset($this->row[$query_id]);
         unset($this->rowset[$query_id]);

         @mysqli_free_result($query_id);

         return true;
      }
      else
      {
         return false;
      }
   }
   function sql_error($query_id = 0)
   {
      $result['message'] = @mysqli_error($this->db_connect_id);
      $result['code'] = @mysqli_errno($this->db_connect_id);

      return $result;
   }

} // class sql_db

} // if ... define

?>
 
View user's profile Send private message
Raven
Site Admin/Owner



Joined: Aug 27, 2002
Posts: 17088

PostPosted: Sat Oct 04, 2008 9:33 am Reply with quote

Testy1,

I don't have time right now to look at this - I think you know why Wink. I have made a note and will try to get to this as soon as I can. Hopefully someone else will be able to assist.

[Edited, although I can't remember why killing me]


Last edited by Raven on Sat Oct 04, 2008 4:56 pm; edited 1 time in total 
View user's profile Send private message
testy1







PostPosted: Sat Oct 04, 2008 4:53 pm Reply with quote

loki ?

although my brother has a bit to do with loki it's not me.....But I wouldn't mind having his knowledge lol
 
Raven







PostPosted: Sat Oct 04, 2008 4:57 pm Reply with quote

Embarassed Blonde Moment worship
 
testy1







PostPosted: Sat Oct 04, 2008 4:59 pm Reply with quote

lol stop working so hard
 
Loki
Worker
Worker



Joined: Oct 05, 2003
Posts: 107
Location: Illinois

PostPosted: Sat Oct 04, 2008 5:00 pm Reply with quote

We all look alike to Raven. killing me I will take a look at it though.
 
View user's profile Send private message Visit poster's website
Loki







PostPosted: Sat Oct 04, 2008 5:35 pm Reply with quote

For that part that file should be like this:

Code:
<?php

/***************************************************************************
 *                                 mysql.php
 *                            -------------------
 *   begin                : Saturday, Feb 13, 2001
 *   copyright            : (C) 2001 The phpBB Group
 *   email                : [ Only registered users can see links on this board! Get registered or login! ]
 *
 *   $Id: mysql.php,v 1.16.2.1 2005/09/18 16:17:20 acydburn Exp $
 *
 ***************************************************************************/

/***************************************************************************
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 ***************************************************************************/

if(!defined('SQL_LAYER'))
{

define('SQL_LAYER','mysql');

class sql_db
{

    var $db_connect_id;
    var $query_result;
    var $row = array();
    var $rowset = array();
    var $num_queries = 0;

    //
    // Constructor
    //
    function sql_db($sqlserver, $sqluser, $sqlpassword, $database, $persistency = true)
    {

        $this->persistency = $persistency;
        $this->user = $sqluser;
        $this->password = $sqlpassword;
        $this->server = $sqlserver;
        $this->dbname = $database;

        if($this->persistency)
        {
            $this->db_connect_id = @($GLOBALS["___mysqli_ston"] = mysqli_connect($this->server,  $this->user,  $this->password));
        }
        else
        {
            $this->db_connect_id = @($GLOBALS["___mysqli_ston"] = mysqli_connect($this->server,  $this->user,  $this->password));
        }
        if($this->db_connect_id)
        {
            if($database != '')
            {
                $this->dbname = $database;
                $dbselect = @((bool)mysqli_query($GLOBALS["___mysqli_ston"], "USE $this->dbname"));
                if(!$dbselect)
                {
                    @((is_null($___mysqli_res = mysqli_close($this->db_connect_id))) ? false : $___mysqli_res);
                    $this->db_connect_id = $dbselect;
                }
            }
            return $this->db_connect_id;
        }
        else
        {
            return false;
        }
    }

    //
    // Other base methods
    //
    function sql_close()
    {
        if($this->db_connect_id)
        {
            if($this->query_result)
            {
                @((mysqli_free_result($this->query_result) || (is_object($this->query_result) && (get_class($this->query_result) == "mysqli_result"))) ? true : false);
            }
            $result = @((is_null($___mysqli_res = mysqli_close($this->db_connect_id))) ? false : $___mysqli_res);
            return $result;
        }
        else
        {
            return false;
        }
    }

    //
    // Base query method
    //
    function sql_query($query = '', $transaction = FALSE)
    {
        global $loglevel, $querycount;
    // Remove any pre-existing queries
        unset($this->query_result);
        if($query != '')
            {
            $this->query_result = @mysqli_query( $this->db_connect_id, $query);
            }
            if ($loglevel == 2) {
                $querycount = $querycount + 1;
                $fplog = fopen(NUKE_BASE_DIR.'rnlogs/dblog','a');
                $logvar = date("F j, Y, g:i a") . ' ' ;
                $logvar .= 'SQL was: ' . preg_replace('/\s+/', ' ', trim($query)) . "\n";
                $logvar .= 'querycount = ' . $querycount . "\n";
                fwrite($fplog, "$logvar" . "\n");
                fclose($fplog);
        }
        if($this->query_result)
        {
            unset($this->row[$this->query_result]);
            unset($this->rowset[$this->query_result]);
            return $this->query_result;
        }
        else
            {
                if ($loglevel > 0) {
                    $error = $this->sql_error($query);
                    $fplog = fopen(NUKE_BASE_DIR.'rnlogs/dblog','a');
                    $logvar = date("F j, Y, g:i a") . ' ' ;
                    $logvar .= $error['code'] . ' : ' .  $error['message'] . "\n";
                    $logvar .= 'SQL was: ' . preg_replace('/\s+/', ' ', trim($query)) . "\n";
                    $logvar .= ' remote addr: ' . $_SERVER['REMOTE_ADDR'];
                    fwrite($fplog, "$logvar" . "\n");
                    fclose($fplog);
                }
            return ( $transaction == END_TRANSACTION ) ? true : false;
        }
    }

    //
    // Other query methods
    //
    function sql_numrows($query_id = 0)
    {
        if(!$query_id)
        {
            $query_id = $this->query_result;
        }
        if($query_id)
        {
            $result = @mysqli_num_rows($query_id);
            return $result;
        }
        else
        {
            return false;
        }
    }
    function sql_affectedrows()
    {
        if($this->db_connect_id)
        {
            $result = @mysqli_affected_rows($this->db_connect_id);
            return $result;
        }
        else
        {
            return false;
        }
    }
    function sql_numfields($query_id = 0)
    {
        if(!$query_id)
        {
            $query_id = $this->query_result;
        }
        if($query_id)
        {
            $result = @(($___mysqli_tmp = mysqli_num_fields($query_id)) ? $___mysqli_tmp : false);
            return $result;
        }
        else
        {
            return false;
        }
    }
    function sql_fieldname($offset, $query_id = 0)
    {
        if(!$query_id)
        {
            $query_id = $this->query_result;
        }
        if($query_id)
        {
            $result = @((($___mysqli_tmp = mysqli_fetch_field_direct($query_id, 0)->name) && (!is_null($___mysqli_tmp))) ? $___mysqli_tmp : false);
            return $result;
        }
        else
        {
            return false;
        }
    }
    function sql_fieldtype($offset, $query_id = 0)
    {
        if(!$query_id)
        {
            $query_id = $this->query_result;
        }
        if($query_id)
        {
            $result = @((is_object($___mysqli_tmp = mysqli_fetch_field_direct($query_id, 0)) && !is_null($___mysqli_tmp = $___mysqli_tmp->type)) ? ((($___mysqli_tmp = (string)(substr(( (($___mysqli_tmp == MYSQLI_TYPE_STRING) || ($___mysqli_tmp == MYSQLI_TYPE_VAR_STRING) ) ? "string " : "" ) . ( (in_array($___mysqli_tmp, array(MYSQLI_TYPE_TINY, MYSQLI_TYPE_SHORT, MYSQLI_TYPE_LONG, MYSQLI_TYPE_LONGLONG, MYSQLI_TYPE_INT24))) ? "int " : "" ) . ( (in_array($___mysqli_tmp, array(MYSQLI_TYPE_FLOAT, MYSQLI_TYPE_DOUBLE, MYSQLI_TYPE_DECIMAL, ((defined("MYSQLI_TYPE_NEWDECIMAL")) ? constant("MYSQLI_TYPE_NEWDECIMAL") : -1)))) ? "real " : "" ) . ( ($___mysqli_tmp == MYSQLI_TYPE_TIMESTAMP) ? "timestamp " : "" ) . ( ($___mysqli_tmp == MYSQLI_TYPE_YEAR) ? "year " : "" ) . ( (($___mysqli_tmp == MYSQLI_TYPE_DATE) || ($___mysqli_tmp == MYSQLI_TYPE_NEWDATE) ) ? "date " : "" ) . ( ($___mysqli_tmp == MYSQLI_TYPE_TIME) ? "time " : "" ) . ( ($___mysqli_tmp == MYSQLI_TYPE_SET) ? "set " : "" ) . ( ($___mysqli_tmp == MYSQLI_TYPE_ENUM) ? "enum " : "" ) . ( ($___mysqli_tmp == MYSQLI_TYPE_GEOMETRY) ? "geometry " : "" ) . ( ($___mysqli_tmp == MYSQLI_TYPE_DATETIME) ? "datetime " : "" ) . ( (in_array($___mysqli_tmp, array(MYSQLI_TYPE_TINY_BLOB, MYSQLI_TYPE_BLOB, MYSQLI_TYPE_MEDIUM_BLOB, MYSQLI_TYPE_LONG_BLOB))) ? "blob " : "" ) . ( ($___mysqli_tmp == MYSQLI_TYPE_NULL) ? "null " : "" ), 0, -1))) == "") ? "unknown" : $___mysqli_tmp) : false);
            return $result;
        }
        else
        {
            return false;
        }
    }
    function sql_fetchrow($query_id = 0)
    {
        if(!$query_id)
        {
            $query_id = $this->query_result;
        }
        if($query_id)
        {
            $this->row[(int)$query_id] = @mysqli_fetch_array($query_id);
            return $this->row[(int)$query_id];
        }
        else
        {
            return false;
        }
    }
    function sql_fetchrowset($query_id = 0)
    {
        if(!$query_id)
        {
            $query_id = $this->query_result;
        }
        if($query_id)
        {
            unset($this->rowset[$query_id]);
            unset($this->row[$query_id]);
            while($this->rowset[$query_id] = @mysqli_fetch_array($query_id))
            {
                $result[] = $this->rowset[$query_id];
            }
            return $result;
        }
        else
        {
            return false;
        }
    }
    function sql_fetchfield($field, $rownum = -1, $query_id = 0)
    {
        if(!$query_id)
        {
            $query_id = $this->query_result;
        }
        if($query_id)
        {
            if($rownum > -1)
            {
                $result = @mysql_result($query_id, $rownum, $field);
            }
            else
            {
                if(empty($this->row[$query_id]) && empty($this->rowset[$query_id]))
                {
                    if($this->sql_fetchrow())
                    {
                        $result = $this->row[$query_id][$field];
                    }
                }
                else
                {
                    if($this->rowset[$query_id])
                    {
                        $result = $this->rowset[$query_id][0][$field];
                    }
                    else if($this->row[$query_id])
                    {
                        $result = $this->row[$query_id][$field];
                    }
                }
            }
            return $result;
        }
        else
        {
            return false;
        }
    }
    function sql_rowseek($rownum, $query_id = 0){
        if(!$query_id)
        {
            $query_id = $this->query_result;
        }
        if($query_id)
        {
            $result = @mysqli_data_seek($query_id,  $rownum);
            return $result;
        }
        else
        {
            return false;
        }
    }
    function sql_nextid(){
        if($this->db_connect_id)
        {
            $result = @((is_null($___mysqli_res = mysqli_insert_id($this->db_connect_id))) ? false : $___mysqli_res);
            return $result;
        }
        else
        {
            return false;
        }
    }
    function sql_freeresult($query_id = 0){
        if(!$query_id)
        {
            $query_id = $this->query_result;
        }

        if ( $query_id )
        {
            unset($this->row[$query_id]);
            unset($this->rowset[$query_id]);

            @((mysqli_free_result($query_id) || (is_object($query_id) && (get_class($query_id) == "mysqli_result"))) ? true : false);

            return true;
        }
        else
        {
            return false;
        }
    }
    function sql_error($query_id = 0)
    {
        $result['message'] = @((is_object($this->db_connect_id)) ? mysqli_error($this->db_connect_id) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false));
        $result['code'] = @((is_object($this->db_connect_id)) ? mysqli_errno($this->db_connect_id) : (($___mysqli_res = mysqli_connect_errno()) ? $___mysqli_res : false));

        return $result;
    }

} // class sql_db

} // if ... define

?>




Of course there are several other areas that will need changes also. I will look at them.
 
Display posts from previous:       
Post new topic   Reply to topic    Ravens PHP Scripts And Web Hosting Forum Index -> MySQL

View next topic
View previous topic
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum


Powered by phpBB © 2001-2007 phpBB Group
All times are GMT - 6 Hours
 
Forums ©