PHP Classes
elePHPant
Icontem

Simple SQL: MySQL wrapper to shortcut common tasks execution

Recommend this page to a friend!
  Info   View files View files (5)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2004-07-06 (12 years ago) RSS 2.0 feedStarStarStarStar 66%Total: 1,992 All time: 1,914 This week: 871Up
Version License Categories
simplesql-class-php 1.0GNU General Publi...Databases
Description Author

SYNOPSIS:
This class is meant to shortcut common MySQL database access tasks.

DESCRIPTION:
Simple SQL provides functions to retrieve single fields or rows of a table, insert rows given arrays of column names and values, update single fields of a given table row, delete given table rows and many other common needs.

ADVANTAGES:
- This PHP class file will eliminate nearly all code from the built in std mysql PHP functions.
- Speed, it will dramatically decrease development and execution time.
- Extremely easy to use; one hardly has to understand the MySQL syntax.
- It automatically determines whether or not there is an open connection to the MySQL server. It will connect and reconnect automatically.
- The built in debugging functions make it easy to solve syntax error and logic error.
- Supports arrays for arguments and returns ASSOC arrays from the DB with indexes.
- Stores resource links and common variables such as the last table used.

USE:
Simply include the file at the top of any script you wish to use it on. This class is highly customizeable.

EXAMPLES:


--- Example 1 ----------------------------- (Shows how little line of code is required to simply grab content from a table without using ANY mysql syntax)

<? include('SimpleSQL.class.php');
$db=new SimpleSQL("test_db","localhost","root","");
//Select all rows and coloumns of data from table 'tablename' and put into array
$db->get_content("tablename");
print_r($db->result);
//echo one cell from the result
echo $db->result[4]["active"];


--- Example 2 ----------------------------- (grab one row from a table)

//Get one row from the table "tablename" (note argument for table can be empty because script stores last table used into a public variable)
$db->get_content("","WHERE location='home' AND active='1'");
print_r($db->result);
//echo the `content` (example) from the result
echo $db->result["content"];


--- Example 3 ----------------------------- (shows how to order your results and to limit by 5)

//Get all rows where active is =0, order by id DESC, limit to 5
$db->get_content("","WHERE active='0'","id DESC",5);
print_r($db->result);


--- Example 4 ----------------------------- (shows how simple it is to change the public variables in the script)

//Switch on debugging mode
$db->debug=true;
//Switch on error messaging
$db->errmsgs=true;
//Switch to another DB and change username and password
$db->db_name="AnotherDatabase";
$db->db_username="username";
$db->db_password="password";


--- Example 5 ----------------------------- (insert data into given fields)

//insert data into a database, first list the fields, then the data, use this format: 'filed1','field2','field3'
$db->insert("'field1','field2','field3'","'data for field 1','data for field 2','data for field 3'");
//echo errors if any found
echo $db->error;


--- Example 6 ----------------------------- (update data in any given field)

//Following queries: "UPDATE `new_table` SET `field1`='new data here' WHERE id='34' LIMIT 1;"
$db->update("field1","new value","new_table","WHERE id='34'",1);


--- Example 7 ----------------------------- (delete data)

//Following queries: "DELETE FROM `new_table` WHERE active='0' LIMIT 5;"
//(note: how the following exempts the 2nd argument which is the table name, table names are stored from the last query)
//The third argument is the ORDER BY clause, may be omitted
$db->delete("WHERE active='0'","","",5);


--- Example 8 ----------------------------- (list the number of rows from last query OR new select statment)

//calling the num_rows function will RETURN the integer and not STORE the results into the result public variable
//Following code will return the number of rows from the last SELECT statment, if there was no last SELECT statment then it will return NULL
$num_rows=$db->num_rows();
//Following exemplifies: mysql_num_rows(mysql_query("SELECT * FROM `new_table` WHERE active='1' ORDER BY id DESC LIMIT 4;"))
$num_rows=$db->num_rows("new_table","WHERE active='1'","id DESC", 4);


--- Example 9 ----------------------------- (if there is a need to send your own query then call the private function itself)

//A long complicated query, the resourse is stored in $db->_link and if any errors are stored in $db->error
$db->_query("CREATE TABLE `new_table` ( `id` INT(10) NOT NULL AUTO_INCREMENT, `username` VARCHAR(25) NOT NULL, `password` VARCHAR(255) NOT NULL, `active` TINYINT(1) DEFAULT '0' NOT NULL, PRIMARY KEY (`id`) ) TYPE = MYISAM;");

Picture of Paul Williamson
Name: Paul Williamson <contact>
Classes: 2 packages by
Country: United States United States

Details
SYNOPSIS: 
This class is meant to shortcut common MySQL database access tasks.

DESCRIPTION:
Simple SQL provides functions to retrieve single fields or rows of a table, insert rows given arrays of column names and values, update single fields of a given table row, delete given table rows and many other common needs.

ADVANTAGES:
- This PHP class file will eliminate nearly all code from the built in std mysql PHP functions.
- Speed, it will dramatically decrease development and execution time.
- Extremely easy to use; one hardly has to understand the MySQL syntax.
- It automatically determines whether or not there is an open connection to the MySQL server. It will connect and reconnect automatically.
- The built in debugging functions make it easy to solve syntax error and logic error.
- Supports arrays for arguments and returns ASSOC arrays from the DB with indexes.
- Stores resource links and common variables such as the last table used.

USE:
Simply include the file at the top of any script you wish to use it on. This class is highly customizeable.

EXAMPLES:


--- Example 1 ----------------------------- (Shows how little line of code is required to simply grab content from a table without using ANY mysql syntax)

<? include('SimpleSQL.class.php'); 
$db=new SimpleSQL("test_db","localhost","root","");
//Select all rows and coloumns of data from table 'tablename' and put into array
$db->get_content("tablename");
print_r($db->result);
//echo one cell from the result
echo $db->result[4]["active"];


--- Example 2 ----------------------------- (grab one row from a table)

//Get one row from the table "tablename" (note argument for table can be empty because script stores last table used into a public variable)
$db->get_content("","WHERE location='home' AND active='1'"); 
print_r($db->result);
//echo the `content` (example) from the result
echo $db->result["content"];


--- Example 3 ----------------------------- (shows how to order your results and to limit by 5

//Get all rows where active is =0, order by id DESC, limit to 5
$db->get_content("","WHERE active='0'","id DESC",5); 
print_r($db->result);


--- Example 4 ----------------------------- (shows how simple it is to change the public variables in the script)

//Switch on debugging mode
$db->debug=true;
//Switch on error messaging
$db->errmsgs=true;
//Switch to another DB and change username and password
$db->db_name="AnotherDatabase";
$db->db_username="username";
$db->db_password="password";


--- Example 5 ----------------------------- (insert data into given fields)

//insert data into a database, first list the fields, then the data, use this format: 'filed1','field2','field3'
$db->insert("'field1','field2','field3'","'data for field 1','data for field 2','data for field 3'");
//echo errors if any found
echo $db->error;


--- Example 6 ----------------------------- (update data in any given field)

//Following queries: "UPDATE `new_table` SET `field1`='new data here' WHERE id='34' LIMIT 1;"
$db->update("field1","new value","new_table","WHERE id='34'",1);


--- Example 7 ----------------------------- (delete data)

//Following queries: "DELETE FROM `new_table` WHERE active='0' LIMIT 5;"
//(note: how the following exempts the 2nd argument which is the table name, table names are stored from the last query)
//The third argument is the ORDER BY clause, may be omitted
$db->delete("WHERE active='0'","","",5);


--- Example 8 ----------------------------- (list the number of rows from last query OR new select statment)

//calling the num_rows function will RETURN the integer and not STORE the results into the result public variable
//Following code will return the number of rows from the last SELECT statment, if there was no last SELECT statment then it will return NULL
$num_rows=$db->num_rows();
//Following exemplifies: mysql_num_rows(mysql_query("SELECT * FROM `new_table` WHERE active='1' ORDER BY id DESC LIMIT 4;"))
$num_rows=$db->num_rows("new_table","WHERE active='1'","id DESC", 4);


--- Example 9 ----------------------------- (if there is a need to send your own query then call the private function itself)

//A long complicated query, the resourse is stored in $db->_link and if any errors are stored in $db->error
$db->_query("CREATE TABLE `new_table` ( `id` INT(10) NOT NULL AUTO_INCREMENT, `username` VARCHAR(25) NOT NULL, `password` VARCHAR(255) NOT NULL, `active` TINYINT(1) DEFAULT '0' NOT NULL, PRIMARY KEY (`id`) ) TYPE = MYISAM;");
  Files folder image Files  
File Role Description
Accessible without login Plain text file example.php Example examples on how to use this class
Accessible without login Plain text file README.txt Doc. details about this class with 9 discriptive examples
Accessible without login Plain text file simplesql.class.php Class Main file
Accessible without login Plain text file test.php Example A Full test script and setup
Plain text file TODO Doc. Upcomming features

 Version Control Unique User Downloads Download Rankings  
 0%
Total:1,992
This week:0
All time:1,914
This week:871Up
 User Ratings  
 
 All time
Utility:91%StarStarStarStarStar
Consistency:87%StarStarStarStarStar
Documentation:60%StarStarStarStar
Examples:62%StarStarStarStar
Tests:-
Videos:-
Overall:66%StarStarStarStar
Rank:559