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 : Simple database class
Categories : PHP, PHP Classes, MySQL, Databases Update Picture
JJ Harrison
Date : Sep 10th 2003
Grade : 4 of 5 (graded 9 times)
Viewed : 14799
File : No file for this code example.
Images : No Images for this code example.
Search : More code by JJ Harrison
Action : Grade This Code Example
Tools : My Examples List

  Submit your own code examples 
 

<?
/*
$result = $db->query($query);

while ($row = $db->fetch_array($result))
{
echo '<a href="'.$row['page_name'].'">'.$row['title'].'</a> - '.$row['description'];
}
*/

class db
         {// db.class.php, Database connection / query functions.
var $hostname;// Hostname that the object should connect to
var $username;// Username that the object should use
var $password;// Password that the object should use
         var $database;// Database that the object should use
var $query_num;// counts the total queries that the object has done. Some BBs do this. Might
be of use for optimization etc
                        
                        function set_cred($hostname,$username,$password)
         {
                         $this->hostname = $hostname
                                 $this->username = $username
                                 $this->password = $password
                        }
                        function db_connect()
{
                         $result = mysql_connect($this->hostname,
         $this->username,
                                                                                        
         $this->password);
                        /*
                        Open a persistant connection to the server. Same as mysql_connect with CGI
                        Because PHP has to be spawned each time anyway.
                        */
if (!$result)
                                 {
echo 'Connection to database server at: '.$this->hostname.' failed.';
                                                         return false;
                                 }
                                                
return $result;
}
                        

                        function db_pconnect()
{
                         $result = mysql_pconnect($this->hostname,
         $this->username,
                                                                                        
         $this->password);
                        /*
                        Open a persistant connection to the server. Same as mysql_connect with CGI
                        Because PHP has to be spawned each time anyway.
                        */
if (!$result)
                                 {
echo 'Connection to database server at: '.$this->hostname.' failed.';
                                                         return false;
                                 }
                                                
return $result;
}

                        function select_db($database)
                        {
                         $this->database = $database;
                         if (!mysql_select_db($this->database))
                                                {
echo 'Selection of database: '.$this->database.' failed.';
return false;
                                                }
                        }
                        
         function query($query)
                        {
                                 $result = mysql_query($query) or die("Query failed:
$query<br><br>" . mysql_error());
return $result;
                        }
                        
                        function fetch_array(&$result)
                        {
                         return mysql_fetch_array($result);
                        }
                        
                        function return_query_num()
                        {
                         return $this->query_num;
                        }
                        
                        function num_rows($result)
                        {
                         return mysql_num_rows($result);
                        }
};


/* EXAMPLE: */

$hostname = 'localhost';
$username = 'user';
$password = 'xxxxxxxx';
$database = 'database';

$db->set_cred($hostname,$username,$password)//set credentials ready to connect,
$db->db_connect();// connect to the database using a non persistant connection
// $db->db_pconnect();// connect to the database using a persistant connection

$db->select_db($database); // Select database

$query = "SELECT * FROM table";
$result = $db->query($query); // excute query with $result as a handle
$numrows = $db->num_rows($result); // Get the number of rows returned or affected

while($row = $db->fetch_array)
{
echo $row[0] . '<br>' . $row['somerow'] . '<br>';
} // echo some data in a loop

echo $numrows . 'Returned from ' . $query;


/* This class is ideally used for managing multiple database connections more efficiently. It could
be modified to work with other databases. It is NOT designed as a more complex database abstraction
layer. */

?>






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
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 Mini Poll class library (SimPoll)
Categories : PHP, PHP Classes, Databases, MySQL, Complete Programs
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
MySQL Optimizer - a class to analyze, optimize or repair tables of a MySQL database.
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
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
Sort the results from a SELECT query (any number of columns) into an array automatically.
Categories : PHP, PHP Classes, Arrays, Databases, MySQL