WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDES  |  PHP CLASSES  |  CODE SEARCH  |  ARTICLES SEARCH  |  PHP FORUMS  |  PHP MANUAL  |  PHP FUNCTIONS LIST  |  WEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
WeberDev's Monthly code contest PHP Code Contest
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Index
PHP Web Logs (BLogs)
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
Submit Site
Forex Trading Online forex trading platform

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : Mssql database Manager
Categories : PHP, Databases, MS SQL Server, Classes and Objects, PHP Classes Click here to Update Your Picture
leaping langoor
Date : Aug 22nd 2004
Grade : 2 of 5 (graded 2 times)
Viewed : 8954
File : No file for this code example.
Images : No Images for this code example.
Search : More code by leaping langoor
Action : Grade This Code Example
Tools : My Examples List

  Submit your own code examples 
 

Its been a long time sinse I did a Mssql manager so here's a standardised version. You can see the example at the bottom

Mssql.php



MSSQL Manager Class
Author....: leapinglangoor[ leapinglangoor@yahoo.co.in ]
Updated...: 22 Aug 2004
Version...: 1.00
<?php
class Mssql_db
{

    var
$db_connect_id;
    var
$result_ids = array();
    var
$result;

    var
$next_id;

    var
$num_rows = array();
    var
$current_row = array();
    var
$field_names = array();
    var
$field_types = array();
    var
$result_rowset = array();

    var
$num_queries = 0;

   
//
    // Constructor
    //
   
function Mssql_db($sqlserver, $sqluser, $sqlpassword, $database, $persistency = true)
    {
       
$this->persistency = $persistency;
       
$this->server = $sqlserver;
       
$this->user = $sqluser;
       
$this->password = $sqlpassword;
       
$this->dbname = $database;

       
$this->db_connect_id = ($this->persistency) ? odbc_pconnect($this->server, $this->user, $this->password) : odbc_connect($this->server, $this->user, $this->password);

        return (
$this->db_connect_id ) ? $this->db_connect_id : false;
    }
   
//
    // Other base methods
    //
   
function sql_close()
    {
        if(
$this->db_connect_id)
        {
            if(
$this->in_transaction )
            {
                @
odbc_commit($this->db_connect_id);
            }

            if(
count($this->result_rowset) )
            {
                unset(
$this->result_rowset);
                unset(
$this->field_names);
                unset(
$this->field_types);
                unset(
$this->num_rows);
                unset(
$this->current_row);
            }

            return @
odbc_close($this->db_connect_id);
        }
        else
        {
            return
false;
        }
    }

   
//
    // Query method
    //
   
function sql_query($query = "", $transaction = FALSE)
    {
        if(
$query != "" )
        {
           
$this->num_queries++;

            if(
$transaction == BEGIN_TRANSACTION && !$this->in_transaction )
            {
                if( !
odbc_autocommit($this->db_connect_id, false) )
                {
                    return
false;
                }
               
$this->in_transaction = TRUE;
            }

           
$query = str_replace("LOWER(", "LCASE(", $query);

            if(
preg_match("/^SELECT(.*?)(LIMIT ([0-9]+)[, ]*([0-9]+)*)?$/s", $query, $limits) )
            {
               
$query = $limits[1];

                if( !empty(
$limits[2]) )
                {
                   
$row_offset = ( $limits[4] ) ? $limits[3] : "";
                   
$num_rows = ( $limits[4] ) ? $limits[4] : $limits[3];

                   
$query = "TOP " . ( $row_offset + $num_rows ) . $query;
                }

               
$this->result = odbc_exec($this->db_connect_id, "SELECT $query");

                if(
$this->result )
                {
                    if( empty(
$this->field_names[$this->result]) )
                    {
                        for(
$i = 1; $i < odbc_num_fields($this->result) + 1; $i++)
                        {
                           
$this->field_names[$this->result][] = odbc_field_name($this->result, $i);
                           
$this->field_types[$this->result][] = odbc_field_type($this->result, $i);
                        }
                    }

                   
$this->current_row[$this->result] = 0;
                   
$this->result_rowset[$this->result] = array();

                   
$row_outer = ( isset($row_offset) ) ? $row_offset + 1 : 1;
                   
$row_outer_max = ( isset($num_rows) ) ? $row_offset + $num_rows + 1 : 1E9;
                   
$row_inner = 0;

                    while(
odbc_fetch_row($this->result, $row_outer) && $row_outer < $row_outer_max )
                    {
                        for(
$j = 0; $j < count($this->field_names[$this->result]); $j++)
                        {
                           
$this->result_rowset[$this->result][$row_inner][$this->field_names[$this->result][$j]] = stripslashes(odbc_result($this->result, $j + 1));
                        }

                       
$row_outer++;
                       
$row_inner++;
                    }

                   
$this->num_rows[$this->result] = count($this->result_rowset[$this->result]);

                   
odbc_free_result($this->result);
                }

            }
            else if(
eregi("^INSERT ", $query) )
            {
               
$this->result = odbc_exec($this->db_connect_id, $query);

                if(
$this->result )
                {
                   
$result_id = odbc_exec($this->db_connect_id, "SELECT @@IDENTITY");
                    if(
$result_id )
                    {
                        if(
odbc_fetch_row($result_id) )
                        {
                           
$this->next_id[$this->db_connect_id] = odbc_result($result_id, 1);
                           
$this->affected_rows[$this->db_connect_id] = odbc_num_rows($this->result);
                        }
                    }
                }
            }
            else
            {
               
$this->result = odbc_exec($this->db_connect_id, $query);

                if(
$this->result )
                {
                   
$this->affected_rows[$this->db_connect_id] = odbc_num_rows($this->result);
                }
            }

            if( !
$this->result )
            {
                if(
$this->in_transaction )
                {
                   
odbc_rollback($this->db_connect_id);
                   
odbc_autocommit($this->db_connect_id, true);
                   
$this->in_transaction = FALSE;
                }

                return
false;
            }

            if(
$transaction == END_TRANSACTION && $this->in_transaction )
            {
               
$this->in_transaction = FALSE;

                if ( !@
odbc_commit($this->db_connect_id) )
                {
                   
odbc_rollback($this->db_connect_id);
                   
odbc_autocommit($this->db_connect_id, true);
                    return
false;
                }
               
odbc_autocommit($this->db_connect_id, true);
            }

            return
$this->result;
        }
        else
        {
            if(
$transaction == END_TRANSACTION && $this->in_transaction )
            {
               
$this->in_transaction = FALSE;

                if ( !@
odbc_commit($this->db_connect_id) )
                {
                   
odbc_rollback($this->db_connect_id);
                   
odbc_autocommit($this->db_connect_id, true);
                    return
false;
                }
               
odbc_autocommit($this->db_connect_id, true);
            }

            return
true;
        }
    }

   
//
    // Other query methods
    //
   
function sql_numrows($query_id = 0)
    {
        if( !
$query_id )
        {
           
$query_id = $this->result;
        }

        return (
$query_id ) ? $this->num_rows[$query_id] : false;
    }

    function
sql_numfields($query_id = 0)
    {
        if( !
$query_id )
        {
           
$query_id = $this->result;
        }

        return (
$query_id ) ? count($this->field_names[$query_id]) : false;
    }

    function
sql_fieldname($offset, $query_id = 0)
    {
        if( !
$query_id )
        {
           
$query_id = $this->result;
        }

        return (
$query_id ) ? $this->field_names[$query_id][$offset] : false;
    }

    function
sql_fieldtype($offset, $query_id = 0)
    {
        if( !
$query_id )
        {
           
$query_id = $this->result;
        }

        return (
$query_id ) ? $this->field_types[$query_id][$offset] : false;
    }

    function
sql_fetchrow($query_id = 0)
    {
        if( !
$query_id )
        {
           
$query_id = $this->result;
        }

        if(
$query_id )
        {
            return (
$this->num_rows[$query_id] && $this->current_row[$query_id] < $this->num_rows[$query_id] ) ? $this->result_rowset[$query_id][$this->current_row[$query_id]++] : false;
        }
        else
        {
            return
false;
        }
    }

    function
sql_fetchrowset($query_id = 0)
    {
        if( !
$query_id )
        {
           
$query_id = $this->result;
        }

        if(
$query_id )
        {
            return (
$this->num_rows[$query_id] ) ? $this->result_rowset[$query_id] : false;
        }
        else
        {
            return
false;
        }
    }

    function
sql_fetchfield($field, $row = -1, $query_id = 0)
    {
        if( !
$query_id )
        {
           
$query_id = $this->result;
        }

        if(
$query_id )
        {
            if(
$row < $this->num_rows[$query_id] )
            {
               
$getrow = ($row == -1) ? $this->current_row[$query_id] - 1 : $row;

                return
$this->result_rowset[$query_id][$getrow][$this->field_names[$query_id][$field]];
            }
            else
            {
                return
false;
            }
        }
        else
        {
            return
false;
        }
    }

    function
sql_rowseek($offset, $query_id = 0)
    {
        if( !
$query_id )
        {
           
$query_id = $this->result;
        }

        if(
$query_id )
        {
           
$this->current_row[$query_id] = $offset - 1;
            return
true;
        }
        else
        {
            return
false;
        }
    }

    function
sql_nextid()
    {
        return (
$this->next_id[$this->db_connect_id] ) ? $this->next_id[$this->db_connect_id] : false;
    }

    function
sql_affectedrows()
    {
        return (
$this->affected_rows[$this->db_connect_id] ) ? $this->affected_rows[$this->db_connect_id] : false;
    }

    function
sql_freeresult($query_id = 0)
    {
        if( !
$query_id )
        {
           
$query_id = $this->result;
        }

        unset(
$this->num_rows[$query_id]);
        unset(
$this->current_row[$query_id]);
        unset(
$this->result_rowset[$query_id]);
        unset(
$this->field_names[$query_id]);
        unset(
$this->field_types[$query_id]);

        return
true;
    }

    function
sql_error()
    {
       
$error['code'] = "";//odbc_error($this->db_connect_id);
       
$error['message'] = "Error";//odbc_errormsg($this->db_connect_id);

       
return $error;
    }

}
// class sql_db

} // if ... define

?>



Example1.php:
This is an example file for Mssql class.
<?php

$db
= new Mssql_db('localhost', 'username', '**password**', 'databasename');

$sql = "The Mssql query you want to execute";
$result = $db -> sql_query($sql);

/* Execute the result you recieved */

$db -> sql_close();
?>



MySQL Handler
Categories : PHP, Databases, MySQL, Classes and Objects, PHP Classes
Online Automatic Class Generator for MySQL Tables
Categories : PHP, PHP Classes, Classes and Objects, Databases, MySQL
Recordset Class for MSSQL database
Categories : PHP Classes, Databases, PHP, MS SQL Server
Simple class for accessing databases like MSSql Server, Oracle etc by Raju
Categories : PHP, MS SQL Server, Databases, PHP Classes, Oracle
Convert SQL from oracle,mysql,mssql,sqlite and odbc to SQL compatible
Categories : PHP, PHP Classes, Databases, MySQL, MS SQL Server
[PHP5] aDB PDO LIKE Database Abstraction. Switch easily from one db server to another, strong errors management, manage transactions, queries preparation and more.
Categories : PHP, PHP Classes, Databases, MS SQL Server, MySQL
PHP CLASS for ORACLE (database connectivity)
Categories : PHP, PHP Classes, Classes and Objects, Databases, Oracle
Powerful php/mysql Pagination for up to 6 URL Params
Categories : PHP, PHP Classes, Databases, MySQL, Navigation
Password reminder
Categories : PHP, PHP Classes, Databases, MySQL, Mail
MySQL Class to ease Database connectivity
Categories : MySQL, PHP Classes, Databases, PHP
ECHO-PHP Class Real Time Transaction Processor v1.4.4 for Credit Cards and Checks / ACH
Categories : PHP Classes, Cybercash, Classes and Objects, Ecommerce, PHP
usercounter class
Categories : PHP, PHP Classes, Databases, MySQL, Environment Variables
Simple Mini Poll class library (SimPoll)
Categories : PHP, PHP Classes, Databases, MySQL, Complete Programs
Simple class to create insert and update statements. Independent of the access to the database. Makes handling complex inserts easier - especially when the table structure is liable to change.
Categories : Databases, PHP Classes, PHP
PostGreSQL and MySQL 2 in 1 db Manager
Categories : PHP, PHP Classes, Databases, PostgreSQL, MySQL