topical media & game development
alt-bot-lib-mysql.php / php
<?php
/*
#################################################################
Copyright 2007, Michael Schrenk
This software is designed for use with the book,
"Webbots, Spiders, and Screen Scarpers", Michael Schrenk, 2007 No Starch Press, San Francisco CA
W3CŪ SOFTWARE NOTICE AND LICENSE
This work (and included software, documentation such as READMEs, or other
related items) is being provided by the copyright holders under the following license.
By obtaining, using and/or copying this work, you (the licensee) agree that you have read,
understood, and will comply with the following terms and conditions.
Permission to copy, modify, and distribute this software and its documentation, with or
without modification, for any purpose and without fee or royalty is hereby granted, provided
that you include the following on ALL copies of the software and documentation or portions thereof,
including modifications:
1. The full text of this NOTICE in a location viewable to users of the redistributed
or derivative work.
2. Any pre-existing intellectual property disclaimers, notices, or terms and conditions.
If none exist, the W3C Software Short Notice should be included (hypertext is preferred,
text is permitted) within the body of any redistributed or derivative code.
3. Notice of any changes or modifications to the files, including the date changes were made.
(We recommend you provide URIs to the location from which the code is derived.)
THIS SOFTWARE AND DOCUMENTATION IS PROVIDED "AS IS," AND COPYRIGHT HOLDERS MAKE NO REPRESENTATIONS OR
WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO, WARRANTIES OF MERCHANTABILITY OR FITNESS
FOR ANY PARTICULAR PURPOSE OR THAT THE USE OF THE SOFTWARE OR DOCUMENTATION WILL NOT INFRINGE ANY THIRD
PARTY PATENTS, COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS.
COPYRIGHT HOLDERS WILL NOT BE LIABLE FOR ANY DIRECT, INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT
OF ANY USE OF THE SOFTWARE OR DOCUMENTATION.
The name and trademarks of copyright holders may NOT be used in advertising or publicity pertaining to the
software without specific, written prior permission. Title to copyright in this software and any associated
documentation will at all times remain with copyright holders.
#################################################################
*/
#################################################################
#
# LIB_mysql.php MySQL database Routines
#
#-----------------------------------------------------------------------
# FUNCTIONS
#
# insert()
# Inserts a row into database,
# as defined by a keyed array
#
# update()
# Updates an existing row in a database,
# as defined by a keyed array and a row index
#
# exe_sql()
# Executes a SQL command and return a result set
#
#################################################################
*********************************************************************
MySQL Constants (scope = global)
----------------------------------------------------------------------
define("MYSQL_ADDRESS", ""); // Define the IP address of your MySQL Server
define("MYSQL_USERNAME", ""); // Define your MySQL user name
define("MYSQL_PASSWORD", ""); // Define your MySQL password
define("DATABASE", ""); // Define your default database
define("SUCCESS", true); // Successful operation flag
define("FAILURE", false); // Failed operation flag
if(strlen(MYSQL_ADDRESS) + strlen(MYSQL_USERNAME) + strlen(MYSQL_PASSWORD) + strlen(MYSQL_ADDRESS) + strlen(DATABASE) == 0)
echo "WARNING: MySQL not configured.<br>\n";
*********************************************************************
Database connection routine (only used by routines in this library
----------------------------------------------------------------------
function connect_to_database()
{
return(mysql_connect(MYSQL_ADDRESS, MYSQL_USERNAME, MYSQL_PASSWORD));
}
*********************************************************************
insert(table, database Name of database (where table Table where row insertion occurs
data_array['column_name'] = data)
RETURNS
SUCCESS or FAILURE
*********************************************************************
function insert(table, mysql_connect = connect_to_database();
mysql_select_db (mysql_connect);
# Create column and data values for SQL command
foreach (key => tmp_col[] = tmp_dat[] = "'columns = join(",", data = join(",", sql = "INSERT INTO ".columns.")VALUES(". result = mysql_query(mysql_connect);
# Report SQL error, if one occured, otherwise return result
if(mysql_error(mysql_connect);
result;
}
}
*********************************************************************
update(table, key_column, database Name of database (where table Table where row insertion occurs
data_array['column_name'] = data)
RETURNS
SUCCESS or FAILURE
*********************************************************************
function update(table, key_column, mysql_connect = connect_to_database();
database, setting_list="";
for (xx<count(xx++)
{
list(value)=each(setting_list.= value."\"";
if (data_array)-1)
sql="UPDATE ".setting_list." WHERE ". id . "\"";
sql, mysql_connect))
{
echo "MySQL Update Error: ".mysql_error(result = "";
}
else
{
return
*********************************************************************
exe_sql(database, database Name of database to operate on
database
RETURNS
An array containing the results of sql operation
*********************************************************************
function exe_sql(sql)
{
# Connect to MySQL server and select database
database, result = mysql_query(mysql_connect);
# Report SQL error, if one occured
if(mysql_error (mysql_connect);
xx=0; result); result_set[result);
}
# If the result set has only one row, return a single dimension array
if(sizeof(result_set=result_set;
}
}
?>
(C) Æliens
20/2/2008
You may not copy or print any of this material without explicit permission of the author or the publisher.
In case of other copyright issues, contact the author.