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 : PHP Mysql Database Connection , Query Execute, Fetch and more
Categories : PHP, PHP Classes, Databases, MySQL Click here to Update Your Picture
Suraj Thapaliya
Date : Apr 13th 2006
Grade : 5 of 5 (graded 4 times)
Viewed : 7954
File : 4372.zip
Images : No Images for this code example.
Search : More code by Suraj Thapaliya
Action : Grade This Code Example
Tools : My Examples List

 
Like this code?
Show the author your appreciation.
Submit your own code examples 
 

Mr. Suraj Thapaliya
PHP Programmer
http://www.surajthapaliya.com.np

host_conf.php
<?

    $host
="hostname";
       
$user_name="username";
       
$password="password";
   
$db="databasename";

       
define("C_DB_HOST",$host);
       
define("C_DB_USER",$user_name);
       
define("C_DB_PASS",$password);
       
define("C_DB_NAME",$db);
?>



mysql.lib.php
<?php
class db
{
    var
$Host = C_DB_HOST;            // Hostname of our MySQL server
   
var $Database = C_DB_NAME;        // Logical database name on that server
   
var $User = C_DB_USER;            // Database user
   
var $Password = C_DB_PASS;        // Database user's password
   
var $Link_ID = 0;                // Result of mysql_connect()
   
var $Query_ID = 0;                // Result of most recent mysql_query()
   
var $Record    = array();            // Current mysql_fetch_array()-result
   
var $Row;                        // Current row number
   
var $Errno = 0;                    // Error state of query
   
var $Error = "";

    function
halt($msg)
    {
        echo(
"</TD></TR></TABLE><B>Database error:</B> $msg<BR>\n");
        echo(
"<B>MySQL error</B>: $this->Errno ($this->Error)<BR>\n");
        die(
"Session halted.");
    }

    function
connect()
    {
        if(
$this->Link_ID == 0)
        {
           
$this->Link_ID = mysql_connect($this->Host, $this->User, $this->Password);
            if (!
$this->Link_ID)
            {
               
$this->halt("Link_ID == false, connect failed");
            }
           
$SelectResult = mysql_select_db($this->Database, $this->Link_ID);
            if(!
$SelectResult)
            {
               
$this->Errno = mysql_errno($this->Link_ID);
               
$this->Error = mysql_error($this->Link_ID);
               
$this->halt("cannot select database <I>".$this->Database."</I>");
            }
        }
    }

    function
query($Query_String)
    {
       
$this->connect();
       
$this->Query_ID = mysql_query($Query_String,$this->Link_ID);
       
$this->Row = 0;
       
$this->Errno = mysql_errno();
       
$this->Error = mysql_error();
        if (!
$this->Query_ID)
        {
           
$this->halt("Invalid SQL: ".$Query_String);
        }
        return
$this->Query_ID;
    }
   
function
query_fetch($fetch=0)
{
    if(
$fetch==0) {
       
$result=@mysql_fetch_assoc($this->Query_ID);
    } else {
       
$result=@mysql_fetch_array($this->Query_ID);
    }
   
    if(!
is_array($result))
    return
false;
   
$this->total_field=mysql_num_fields($this->Query_ID);

    foreach(
$result as $key=>$val){
       
$result[$key]=trim(htmlspecialchars($val));
    }
     return
$result;
}

function
num_field()
{
    return
mysql_num_fields($this->Query_ID);
}
/*
function fetch_field()
{
    return mysql_fetch_field($this->Query_ID,2);
}
*/
function next_record()
    {
       
$this->Record = mysql_fetch_array($this->Query_ID);
       
$this->Row += 1;
       
$this->Errno = mysql_errno();
       
$this->Error = mysql_error();
       
$stat = is_array($this->Record);
        if (!
$stat)
        {
           
mysql_free_result($this->Query_ID);
           
$this->Query_ID = 0;
        }
        return
$this->Record;
    }

    function
num_rows()
    {
        return
mysql_num_rows($this->Query_ID);
    }
    function
maxRow($tablename,$field)
    {
       
$sql="select max($field) from $tablename";
       
$this->query($sql);
       
$result=@mysql_fetch_array($this->Query_ID);
        return
$result[0];
    }
    function
affected_rows()
    {
        return
mysql_affected_rows($this->Link_ID);
    }

    function
optimize($tbl_name)
    {
       
$this->connect();
       
$this->Query_ID = @mysql_query("OPTIMIZE TABLE $tbl_name",$this->Link_ID);
    }

    function
clean_results()
    {
        if(
$this->Query_ID != 0) mysql_freeresult($this->Query_ID);
    }

    function
close()
    {
        if(
$this->Link_ID != 0) mysql_close($this->Link_ID);
    }
}
?>



usercounter class
Categories : PHP, PHP Classes, Databases, MySQL, Environment Variables
MySQL Handler
Categories : PHP, Databases, MySQL, Classes and Objects, PHP Classes
PostGreSQL and MySQL 2 in 1 db Manager
Categories : PHP, PHP Classes, Databases, PostgreSQL, MySQL
MySQL Class to ease Database connectivity
Categories : MySQL, PHP Classes, Databases, PHP
Simple Mini Poll class library (SimPoll)
Categories : PHP, PHP Classes, Databases, MySQL, Complete Programs
Online Automatic Class Generator for MySQL Tables
Categories : PHP, PHP Classes, Classes and Objects, Databases, MySQL
Specify your connection settings and create a link to a MySQL database.
Categories : PHP, PHP Classes, Databases, MySQL, Beginner Guides
Simple database class
Categories : PHP, PHP Classes, MySQL, Databases
Simple usersOnline class - keep track of how many users are online on your site
Categories : PHP, PHP Classes, Databases, MySQL
Setting up InnoDB on MySQL and using Transactions Begin, Commit, Rollback in PHP.
Categories : PHP Classes, Databases, PHP, MySQL, InnoDB
Ajax PHP Tree (Left and Right) with MySQL
Categories : PHP, Databases, MySQL, AJAX, PHP Classes
YellowPages Content Grabber (PHP5 +)
Categories : PHP, PHP Classes, Regexps, Databases, MySQL
Convert SQL from oracle,mysql,mssql,sqlite and odbc to SQL compatible
Categories : PHP, PHP Classes, Databases, MySQL, MS SQL Server
MySQL Connection/Query Class
Categories : Databases, MySQL, PHP, PHP Classes
Create and restore backup of MySQL databases
Categories : MySQL, Databases, PHP, PHP Classes, Complete Programs