/*
########################################################################
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 Routine per database MySQL
#
#-----------------------------------------------------------------------
# FUNZIONI
#
# insert()
# Inserisce una riga (definita da un array) in un database
#
# update()
# Aggiorna una riga esistente di un database,
# dati un array e l'indice della riga
#
# exe_sql()
# Esegue un comando SQL e ritorna l'insime dei risultati
#
########################################################################
/***********************************************************************
Costanti MySQL (scope = global)
----------------------------------------------------------------------*/
define("MYSQL_ADDRESS", ""); // Definisce l'indirizzo IP del server MySQL
define("MYSQL_USERNAME", ""); // Definisce lo username per MySQL
define("MYSQL_PASSWORD", ""); // Definisce la password per MySQL
define("DATABASE", ""); // Definisce il database di default
define("SUCCESS", true); // Flag che indica un'operazione terminata con successo
define("FAILURE", false); // Flag che indica un'operazione fallita
if(strlen(MYSQL_ADDRESS) + strlen(MYSQL_USERNAME) + strlen(MYSQL_PASSWORD) + strlen(MYSQL_ADDRESS) + strlen(DATABASE) == 0)
echo "WARNING: MySQL not configured.
\n";
/***********************************************************************
Routine di connessione al database (usata solo dalle routine di questa libreria)
----------------------------------------------------------------------*/
function connect_to_database()
{
return(mysql_connect(MYSQL_ADDRESS, MYSQL_USERNAME, MYSQL_PASSWORD));
}
/***********************************************************************
insert($database, $table, $data_array)
-------------------------------------------------------------
DESCRIZIONE:
Inserisce una riga (definita da un array) in un database
INPUT:
$database Nome del database (dove si trova $table)
$table Tabella su cui inserire la riga
$data_array Un array con chiavi che definisce i dati da inserire
(i.e. $data_array['column_name'] = data)
OUTPUT:
SUCCESS o FAILURE
***********************************************************************/
function insert($database, $table, $data_array)
{
// Si connette al server MySQL e seleziona il database
$mysql_connect = connect_to_database();
mysql_select_db ($database, $mysql_connect);
// Crea i valori di colonna e dati per il comando SQL
foreach ($data_array as $key => $value)
{
$tmp_col[] = $key;
$tmp_dat[] = "'$value'";
}
$columns = join(",", $tmp_col);
$data = join(",", $tmp_dat);
// Crea ed esegue il comando SQL
$sql = "INSERT INTO ".$table."(".$columns.")VALUES(". $data.")";
$result = mysql_query($sql, $mysql_connect);
// Riporta l'eventuale errore SQL, altrimenti ritorna il risultato
if(mysql_error($mysql_connect))
{
echo "MySQL Update Error: ".mysql_error($mysql_connect);
$result = "";
}
else
{
return $result;
}
}
/***********************************************************************
update($database, $table, $data_array, $key_column, $id)
-------------------------------------------------------------
DESCRIZIONE:
Aggiorna una riga esistente di un database,
dati un array e l'indice della riga
INPUT:
$database Nome del database (dove si trova $table)
$table Tabella su cui inserire la riga
$data_array Un array con chiavi che definisce i dati da inserire
(i.e. $data_array['column_name'] = data)
$id Identificativo (indice) della riga da aggiornare
OUTPUT:
SUCCESS o FAILURE
***********************************************************************/
function update($database, $table, $data_array, $key_column, $id)
{
// Si connette al server MySQL e seleziona il database
$mysql_connect = connect_to_database();
mysql_select_db ($database, $mysql_connect);
// Crea i valori di colonna e dati per il comando SQL
$setting_list="";
for ($xx=0; $xx