okay practical example:
functions_db.php:
<?php
/****************************************************************************
Description: General MySQL database connection
Written by: DJ Querner (jordy@querner.com)
Date: februari 2001
For: Wegener eMedia (www.wegeneremedia.nl)
****************************************************************************/
$dbhost = 'mysql.server.tld';
$dbusername = 'myuser';
$dbuserpassword = 'passw0rd';
$default_dbname = 'mydb';
/****************************************************************************
Flush the error messages
****************************************************************************/
$MYSQL_ERRNO = '';
$MYSQL_ERROR = '';
/****************************************************************************
This function connects the script to the database
Syntax:
db_connect();
****************************************************************************/
function db_connect($dbname='') {
global $dbhost, $dbusername, $dbuserpassword, $default_dbname;
global $MYSQL_ERRNO, $MYSQL_ERROR;
$link_id = mysql_connect($dbhost, $dbusername, $dbuserpassword);
if(!$link_id) {
$error_message("Connection failed to the host $dbhost.");
return 0;
}
else if(empty($dbname) && !mysql_select_db($default_dbname)) {
html_error(mysql_errno()." - ".mysql_error());
return 0;
}
else if(!empty($dbname) && !mysql_select_db($dbname)) {
html_error(mysql_errno()." - ".mysql_error());
return 0;
}
else return $link_id;
}
/****************************************************************************
This function performs a query on the mysql server and returns back the
results. error messages included.
Syntax:
QueryDb("SQL QUERY");
****************************************************************************/
function QueryDb($query)
{
global $dbhost, $dbusername, $dbuserpassword, $default_dbname;
if (!$link = @mysql_connect($dbhost, $dbusername, $dbuserpassword)){
$result = 0;
html_error("Error met het verbinden naar $dbhost!");
}
else {
if (!@mysql_select_db($default_dbname, $link)) {
$result = 0;
html_error("Error. kan $dbname niet vinden!");
}
else {
if (!$result = @($query, $link)) {
$result = 0;
html_error("Kan query $query niet uitvoeren");
}
}
}
return $result;
}
function html_error($error_message) {
global $error_status;
if ($error_status != 1) {
echo "<BR><BR>\n<TABLE CELLPADDING=3 CELLSPACING=1 BORDER=0 WIDTH=70%>\n";
echo "<TR><TD BGCOLOR='#BDC7DE' ALIGN=center><b>$error_message<b></TD></TR>\n";
echo "<TR><TD BGCOLOR='#BDC7DE' ALIGN=left><A HREF='javascript:history.go(-1)'> <-- Terug </A></TD>\n</TR>\n";
echo "</TABLE>\n";
$error_status = 1;
}
}
?>
myfile.php:
<?
include("functions_db.php");
// with the functions_db.php
$rows = QueryDb($sql);
if (!$rows)) {
echo "Error, cannot add record. Hit the back button and try again";
exit;
} else {
insert ok
echo "Table updated. The record of has been added.";
}
//your example
$row = mysql_query($sql,$connection);
if (!$row) {
echo "Error, cannot add record. Hit the back button and try again";
exit;
} else {
insert ok
echo "Table updated. The record of has been added.";
}
?>