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 : A set of functions sitting on top of the abstraction layer that makes it a little easier to do SQL stuff. Documentation is within
Categories : Databases, ODBC, Complete Programs, PHP Update Picture
Stig Bakken
Date : Jan 17th 1999
Grade : 2 of 5 (graded 2 times)
Viewed : 11694
File : No file for this code example.
Images : No Images for this code example.
Search : More code by Stig Bakken
Action : Grade This Code Example
Tools : My Examples List

  Submit your own code examples 
 

<?php /* -*- C++ -*- */
/*
* $Id: sql.phl,v 1.5 1998/07/02 09:52:13 ssb Exp $
*/

/*
* ABOUT:
*
* Lazy PHP programmer series: SQL helper functions.
* Written by Stig Bakken <ssb@guardian.no>
*
* CONFIGURATION:
*
* You must set some global variables prior to including this library:
* $db_type which database abstraction layer to use.
* If your layer is defined in "db-odbc.phl",
* set this variable to "odbc".
* $db_dsn Which database to connect to (dsn=data source name)
* $db_user Which user to connect as
* $db_pass Password used when connecting
*
* Some optional global variables can be set:
* $sql_debug true/false, toggles debug information
* $sql_log_facility how to log debug information
* $sql_log_dest debug information destination
*
* $sql_log_facility and $sql_log_dest are passed as the second
* and third args to PHP's error_log() function, respectively.
* The default is debugging through the PHP TCP debugger to
* port 1400 on localhost. Debug information is also disabled
* by default.
*
* The following global variables will be defined:
* $db_connection The opened connection.
*
*
* USAGE:
*
* All you have to do is require("sql.phl"), either explicitly in the
* pages where you want to use it, or through auto_prepend_file somehow.
* Being a lazy programmer, I always include everything I need with
* auto-prepend from a file I call "config.phl" looking something like
* this:
*
* <?php
* $db_type = "odbc";
* $db_user = "myuser";
* $db_pass = "mypassword";
* $db_dsn = "SVT=Solid; DSN=MyDB"
* require("sql.phl");
* ?>
*
* Once you have included this file, a database connection will
* be opened for you once it is needed, and this connection will
* be used for all queries. Memory is freed automatically for
* all functions except sql().
*
* Example use of all functions:
*
* // returns a result identifier
* $res = sql("SELECT * FROM table");
*
* // returns one column:
* $name = sql_one("SELECT name FROM table WHERE id = $id");
*
* // returns one row in an array
* $row = sql_onerow("SELECT * FROM table WHERE id = $id");
*
* // returns an array of arrays with all the result data
* $data = sql_all("SELECT * FROM table");
*
* // returns an array with data from column 0 only:
* $names = sql_col("SELECT name FROM table", 0);
*
* // returns an associative array with the first column as the
* // key and the second column as the value:
* $name_by_id = sql_assoc("SELECT id, name FROM table");
*
* // enable auto-commit
* sql_autocommit(true);
*
* // disable auto-commit
* sql_autocommit(false);
*
* // commit transaction
* sql_commit();
*
* // roll back transaction
* sql_rollback();
*
*/

require( "db-$db_type.phl");

$db_connection = 0;

/* debugging defaults */
if (empty($sql_debug)) {
$sql_debug = false;
}
if (empty($sql_log_facility)) {
$sql_log_facility = 2; /* debug through TCP */
}
if (empty($sql_log_dest)) {
$sql_log_dest = "127.0.0.1:1400"; /* destination */
}

/*
* Function: sql_debug
* Description: sends debug information somewhere if
* the global variable $sql_debug is true.
*/
function sql_debug($message) {
global $sql_debug, $sql_log_facility, $sql_log_dest;
if ($sql_debug) {
error_log( "[SQL] $message", $sql_log_facility, $sql_log_dest);
}
}

/*
* Function: assert_db_connection
* Description: makes sure we have a database connection
*/
function assert_db_connection()
{
global $db_connection;
if ($db_connection) {
return;
}
global $db_dsn, $db_user, $db_pass;
$db_connection = db_connect($db_dsn, $db_user, $db_pass);
if (!$db_connection) {
die( "Failed to connect to database.");
}
}

/*
* Function: sql
* Arguments: $query (string) - SQL statement
* Description: executes an SQL statement
* Returns: (int) result identifier
* returns 0 upon error
*/
function sql($query) {
global $db_connection, $PHP_SELF;
assert_db_connection();
sql_debug($query);
return db_query($db_connection, $query);
}

/*
* Function: sql_onerow
* Arguments: $query (string) - SQL statement
* Description: executes an SQL statement and returns the first
* row of the result
* Returns: (array) the first row
* returns 0 upon error
*/
function sql_onerow($query) {
$res = sql($query);
$row = db_fetch_row($res);
db_free_result($res);
return $row;
}

/*
* Function: sql_one
* Arguments: $query (string) - SQL statement
* Description: executes an SQL statement and returns the first
* column of the first row of the result
* Returns: (mixed) the first column of the first row
* returns false upon error
*/
function sql_one($query) {
$row = sql_onerow($query);
if (gettype($row) == "array") {
return $row[0];
} else {
return false;
}
}

/*
* Function: sql_all
* Arguments: $query (string) - SQL statement
* Description: executes an SQL statement and returns an array
* of arrays with the rows and columns of all the
* result data from the query.
* Returns: array of arrays or false on error
*/
function sql_all($query) {
$res = sql($query);
if ($res) {
$all = array();
while ($row = db_fetch_row($res)) {
$all[] = $row;
}
db_free_result($res);
return $all;
}
return false;
}


/*
* Function: sql_col
* Arguments: $query (string) - SQL statement
* $column (int) - returned result column number
* Description: executes an SQL statement and returns an array
* with the results from a specific column in the result.
* Returns: array or false on error
*/
function sql_col($query, $column) {
$res = sql($query);
if ($res) {
$all = array();
while ($row = db_fetch_row($res)) {
$all[] = $row[$column];
}
db_free_result($res);
return $all;
}
return false;
}


/*
* Function: sql_assoc
* Arguments: $query (string) - SQL statement
* Description: executes an SQL statement and returns an associative
* array. The indices of this array are taken from the
* first column of the result, while the values are taken
* from the second column. If there are more than two
* columns the remaining ones are ignored.
* Returns: associative array or false on error
*/
function sql_assoc($query) {
$res = sql($query);
if ($res) {
$row = db_fetch_row($res);
if (!is_array($row) || count($row) < 2) {
db_free_result($res);
return false;
}
while ($row) {
$assoc[$row[0]] = $row[1];
$row = db_fetch_row($res);
}
db_free_result($res);
return $assoc;
}
return false;
}


function sql_autocommit($enabled)
{
global $db_connection;
if (!$enabled) {
sql_debug( "transaction starting");
}
return db_autocommit($db_connection, $enabled);
}


function sql_commit()
{
global $db_connection;
sql_debug( "transaction complete");
return db_commit($db_connection);
}


function sql_rollback()
{
global $db_connection;
sql_debug( "transaction aborted");
return db_rollback($db_connection);
}


?>



This program allows you to upload an ODBC ressource - i.e. an MS-Access database to a MySQL server.
Categories : Databases, MySQL, Complete Programs, PHP, Databases
Dynamic WHERE CLAUSE depending on number of FORM FIELDS
Categories : ODBC, General SQL, PHP, Complete Programs, Databases
Modification of Shane Caraveo's guestbook. Uses ODBC...some code modifications
Categories : ODBC, Databases, Complete Programs, PHP
Point and Click Interface ala MS Access for creating SQL statements.
Categories : MySQL, Complete Programs, General SQL, PHP, Databases
This is Yet Another Sql Abstraction Library. Include it in your script and you can use the most important SQL functions without worrying about the SQL backend.
Categories : Databases, PHP, ODBC, MySQL, PostgreSQL
Tropicalm Genetree Family (MySQL based family tree)
Categories : PHP, Interfaces, Databases, MySQL, Complete Programs
phpAds, a complete banner and ad management system with detailled tracking and stats.
Categories : MySQL, Complete Programs, Ecommerce, PHP, Databases
Accepts a database & hostname from a user and then HTTP username and password. Uses this to connect to a MySQL database. Produces a form based on the tables it finds there to allow the user to do SELECTs, INSERTs, and DELETEs.
Categories : Databases, PHP, MySQL, Complete Programs
Shopping Basket On-Line Ordering System.
Categories : Complete Programs, MySQL, PHP, Ecommerce, Databases
Example voting script. Lets people enter suggestions and vote for existing ones.
Categories : MySQL, PHP, Cookies, Complete Programs, Databases
Logs hits to any page which includes it. Automatically utilises page access information left behind by PHP/FI2.0.
Categories : Databases, PHP, mSQL, Databases
Simple Mini Poll class library (SimPoll)
Categories : PHP, PHP Classes, Databases, MySQL, Complete Programs
Browse a MySQL database & draw a tree view & load final items into a template page.
Categories : MySQL, Complete Programs, Algorithms, PHP, Databases
free, search engine, indexing, system, information, web, ftp, http, free, software, cgi, php, MySQL, database, php3, FreeBSD, Linux, Unix, UdmSearch
Categories : MySQL, Complete Programs, PHP, Databases, Search
Shopping Cart e-Commerce Solution
Categories : Complete Programs, PHP, MySQL, Databases