WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDESPHP CLASSESCODE SEARCHARTICLES SEARCHPHP FORUMSPHP MANUALPHP FUNCTIONS LISTWEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Join us on FaceBook
Submit a code Example / Snippet Submit Your Code
Poker Tournaments Poker Tournaments
Poker Guide for Developers Poker Guide for Developers
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
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 Resources
Web Development Content
Internet Security Software
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
פרייסז - הכח לקנות עובר לידיים שלך
Texas Holdem Poker Evangelists

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 10 times)
Viewed : 37370
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  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. */

?>






Specify your connection settings and create a link to a MySQL database.
Categories : PHP, PHP Classes, Databases, MySQL, Beginner Guides
MySQL Connection/Query Class
Categories : Databases, MySQL, PHP, PHP Classes
phpFormGenerator for Dynamic Form Generation from MySQL
Categories : PHP, PHP Classes, MySQL, Databases, HTML and PHP
DBXML- A Class to backup databases in XML Format using web interface
Categories : PHP, PHP Classes, Databases, MySQL, XML
MySQL Class to ease Database connectivity
Categories : MySQL, PHP Classes, Databases, PHP
This class splits the results of the query into multiple pages like what the search engine does.
Categories : PHP Classes, PHP, MySQL, Databases
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
Create and restore backup of MySQL databases
Categories : MySQL, Databases, PHP, PHP Classes, Complete Programs
TAB_STRUCT Class: Is supporting Class for the DBXML Class
Categories : PHP, PHP Classes, MySQL, XML, Databases
MySQL database class
Categories : PHP, MySQL, Databases, PHP Classes
Setting up InnoDB on MySQL and using Transactions Begin, Commit, Rollback in PHP.
Categories : PHP Classes, Databases, PHP, MySQL, InnoDB
PHP Transfer data from text file to Mysql Table
Categories : PHP, PHP Classes, Filesystem, Databases, MySQL
Link Manager for Link Exchangers
Categories : PHP, PHP Classes, Databases, MySQL, CURL
Sort the results from a SELECT query (any number of columns) into an array automatically.
Categories : PHP, PHP Classes, Arrays, Databases, MySQL