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 : DBOperations - Handles almost all routine mysql queries
Categories : PHP, PHP Classes, Databases, MySQL Click here to Update Your Picture
Anjani Kumar
Date : Aug 29th 2004
Grade : 5 of 5 (graded 6 times)
Viewed : 6511
File : No file for this code example.
Images : No Images for this code example.
Search : More code by Anjani Kumar
Action : Grade This Code Example
Tools : My Examples List

  Submit your own code examples 
 

Author                :        ANJANI KUMAR PIRATLA        
File Name         :        database.php                        
Licence                :        Freeware                                 
                                                                                        
FUNCTIONS LIST:
function connect() // connect to mysql server and select db
function delete() // delete records from a table without using clause
function deleteClause() // delete records from a table using clause
function update() // update records of a table
function dropTable() // drop a table from a database
function createTable() // create a table
function insertValuesIntoTable() // insert values into a table
function showTables() // show all tables in a database
function showDatabases() // show all databases
function GetAllRecordsFromTable() // fetch/display all records from a table
function close() // close database connection

<?php
// database variables
// change variables accordingly to suit your database settings
$server          = "localhost";
$database     = "printorder";
$user        = "";
$pass        = "";

// define variables
define("br","<br>");

                     
// main class dbOperations
class DBOperations {
var
$connection;
var
$db;
var
$del;
var
$update;
var
$sql;
var
$row;
var
$drop;
var
$insert;
var
$create;
var
$close;
var
$query;
var
$count;
var
$fields
var
$results;
var
$values;
var
$getrecords;
var
$numrows;
var
$records;
// connect to server
function connect(){
  include(
"database.php");
 
$this->connection = @mysql_connect($server,$user,$pass);
 
$this->db = @mysql_select_db($database);
if(!
$this->connection)
  echo
"Failed to connect to MySQL server. Check whehter MySQL server is running or not.",br;
 
if(!
$this->db)
  echo
"Failed to select db. Check whether DB $database exists or not.",br;
}
// delete records without using clause
function delete($table){
 
$this->del = @mysql_query("DELETE FROM $table"); 
 
  if(!
$this->del)
   echo
"Delete failed without using clause. Delete table name: ",$table,br;
}
// delete records using clause
function deleteClause($table,$clause){
 
$this->del = @mysql_query("DELETE FROM $table WHERE $clause");
 
  if(!
$this->del)
   echo
"Delete failed using clause. Delete table name: ",$table,br;
}

// update records
function update($table,$clause){
 
$this->update = @mysql_query("UPDATE $table $clause");
 
  if(!
$this->update)
   echo
"Update failed. Update table name: ",$table,br;
}

// drop table
function dropTable($table){
 
$this->drop = @mysql_query("DROP TABLE $table");
 
  if(!
$this->drop)
   echo
"Failed to drop the table. Check whether table $table exists or not.",br;
}
// create table
function createTable($table,$clause){
 
$this->create = @mysql_query("CREATE TABLE $table $clause");
 
  if(!
$this->create)
   echo
"Failed to create the table. Create table name: ",$table,br;
}

// insert values into table
function insertValuesIntoTable($table,$fields,$values){
 
$this->insert = @mysql_query("INSERT INTO $table ($fields) VALUES($values)");
 
  if(!
$this->insert)
   echo
"Failed to insert values into the table: $table",br;
}

// show tables in a database
function showTables($db_name){
 
$this->query = @mysql_query("SHOW TABLES FROM $db_name");
 
$this->count = @mysql_num_rows($this->query);
  for(
$i=0;$i<$this->count;$i++){
   while(
$this->row=@mysql_fetch_array($this->query)){
    echo
$this->row[$i],br;
   }
  }
 
  if(!
$this->query)
   echo
"Failed to show tables from $db_name",br;
}
// show databases
function showDatabases(){
 
$this->query = @mysql_query("SHOW DATABASES");
 
$this->count = @mysql_num_rows($this->query);
  for(
$i=0;$i<$this->count;$i++){
   while(
$this->row=@mysql_fetch_array($this->query)){
    echo
$this->row[$i],br;
   }
  }
  if(!
$this->query)
   echo
"Failed to show databases",br;
}
// show all records from a table
function GetAllRecordsFromTable($db_name,$table){
 
$this->query = @mysql_list_fields($db_name,$table);
 
$this->fields = @mysql_num_fields($this->query); 
 
$this->results = array();
 
$this->values = array();
  for(
$i=0; $i<$this->fields;$i++){
   
$this->row = @mysql_field_name($this->query,$i);
   
$this->results[$i] = $this->row;
  }
 
$this->count = count($this->results);
 
$this->getrecords = @mysql_query("SELECT * FROM $table");
 
$this->numrows = @mysql_num_rows($this->getrecords);

  for(
$j=0;$j<$this->numrows;$j++){
   
$this->records = @mysql_fetch_array($this->getrecords);
   
$this->values[$j] = $this->records;
  }
  foreach(
$this->values as $this->v){
   for(
$k=0;$k<$this->count;$k++){
    echo
$this->v[$k]." ";
   }
   echo
br;
  }
  if(!
$this->query)
   echo
"Failed to get field names",br;
   
  if(!
$this->getrecords)
   echo
"Failed to show records",br;
}
//close connection
function close(){
 
$this->close = @mysql_close($this->connection);
}
}
?>





Usage Example
<?php

include("dboperations.php");

// connect to server and database
DBOperations::connect();   

// specify the name of the table to delete records :: use this for deleting all the records
// replace $table with the name of the table from which you want to delete records
DBOperations::delete($table);   

// use this for deleting records with where clause
// replace $table with table name and $clause respectively
// for example name of the table=>"test" id='12'
DBOperations::deleteClause($table,$clause);   

// use this for updating records
// replace $table with table name and $clause i.e $table name=>"test", $clause=>"set username='anjani' WHERE id='21'"
DBOperations::update($table,$clause);

// use this for creating a table
// replace $table with table name and $clause i.e.
//$table name=>"test", $clause=>" (`id` INT UNSIGNED AUTO_INCREMENT, `username` VARCHAR (30), `password` VARCHAR (30), PRIMARY KEY(`id`))"
DBOperations::createTable($table,$clause);

// use this for dropping a table
// replace $table with the name of the table name i.e. $table name=>"test"
DBOperations::dropTable($table);

// use this for inserting values into a table
// replace the $table with the name of the table name i.e. $table name=>"test"
// replace the $clause with cleasue i.e. "username,password","'anjani','kumar'"
DBOperations::insertValuesIntoTable($table,$clause);

// use this for displaying all the tables in a database
// replace the $db_name with respective database name
DBOperations::showTables($db_name);

// use this for displaying all the databases
// replace the $db_name with respective database name
DBOperations::showDatabases($db_name);

// use this for fetching/displaying all records from a table
// replace $db_name and $table with respective database and table names i.e. $db_name=>"mysql" $table=>"test"
DBOperations::GetAllRecordsFromTable($db_name,$table);

// close database connection
DBOperations::close();
?>



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
 Eroc M wrote :1214
This code is missing one thing and that`s how to get 1 record instead of the "GetAllRecordsFromTable".

Eroc