|
|
|
|
|
|
| |
This is my standard approach to db connectin. I create a connection file called conn.php and place it outside the root folder (something like /myconfig/conn.php). Then i include this file into any file i need that has db access ... see code below.
This code does manage to fail a little more gracefully than others and will send out an email notification to you so that you can see what crashed and why.
Placing the code into a function will also make it more secure and simpler to call. There is some basic security in the file. The first time this is run, it will likely fail and present a message saying
"piss off, hackers".
Above that there is the line 'server is:' and some name, likely the website name (yourdomain.com / yourdomain.ca, whatever)copy the name and paste it back into the conn.php where it currently says 'localhost'...if no error comes up then the code is fine...then comment out the echo line.
Its easy to add further checks to prevent inclusion by other users. Sessions is one.
| <?php
//conn.php
bastien koert
Aug 2004
www.bastienkoert.net
this domain -- this check may take a little configuration to get the correct host name
use this the first time to get the host name...then comment it out or delete to complete the security check */
//echo "server is : ".$_SERVER['SERVER_NAME']."<br>";
if (($_SERVER['SERVER_NAME']!="localhost")){
echo "piss off, hackers!";
die();
}
function conn($sql)
{
$host = "myhost"; /* change these to match your situation */
$user = "myusername";
$pass = "mypassword";
$db = "mydb";
if (!($conn=mysql_connect($host, $user, $pass))) {
printf("Error connecting to DB");
email_error("connection",mysql_error());
}
if (!($db3=mysql_select_db($dbname,$conn))){
printf("<p>Error connecting to DB");
email_error("Connection Error",mysql_error());
}
$result = mysql_query($sql);
if (mysql_errno() == 0){
return $result;
}else{
email_error($sql,mysql_error());
$result = 'Null';
}
}//end function
function email_error($reason, $error)
{
$headers = "MIME-Version: 1.0\n";
$headers .= "Content-type: text/plain; charset=iso-8859-1\n";
$headers .= "X-Priority: 3\n";
$headers .= "X-MSMail-Priority: Normal\n";
$headers .= "X-Mailer: php\n";
$headers .= "From: \"".$from."\" <".$from.">\n";
$sendto = "me@mydomain.com";
$subject = "Db error notification";
$time = date("Y-m-d H:m");
$message = "Application db interface failed at $time.\n\n
Operation : $reason\n\n
MySQL Error: $error \n\n
User: ".$_SESSION['username']."\n\n
Regards,
System";
mail($sendto, $subject, $message, $headers);
die("<p>There was a problem with the database");
}
?> | |
To use this file, you include it and then simply call the function
CODE FOR IMPLEMENTAION IN ASSOCIATED FILES
| <?php
//include conn file
require("config/conn.php");
//more of your code
$sql = "select * from tableName [where][group by][order by]";
// get the regular listing results
$result = connect($sql);
if ($result){
//show the results
}else{
//handle failure in some way
}
?> | | |
|
| Visits-tracking Categories : PHP, Databases, MySQL, Errors and Logging, Functions | | | 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 | | | bookmarker - PHP, PHPLIB, MySQL WWW based bookmark manager Categories : MySQL, PHP, MySQL, Complete Programs, 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 | | | phpAds, a complete banner and ad management system with detailled tracking and stats. Categories : MySQL, Complete Programs, Ecommerce, PHP, Databases | | | Point and Click Interface ala MS Access for creating SQL statements. Categories : MySQL, Complete Programs, General SQL, PHP, Databases | | | Message of the Day - Random Message (Needs MySQL!) Categories : Databases, HTML and PHP, PHP, MySQL | | | Alternating background color for HTML table rows Categories : PHP, Databases, MySQL, HTML and PHP | | | color codes for positive and negative numbers Categories : PHP, MySQL, Databases, HTML | | | Authorize Me! An authentication script. Categories : MySQL, Databases, Authentication, PHP | | | A very simple way to build and do a hierarchical html categories browser without javascript , just using html php and mySql
Categories : HTML and PHP, Databases, Algorithms, PHP, MySQL | | | Tropicalm Genetree Family (MySQL based family tree) Categories : PHP, Interfaces, Databases, MySQL, Complete Programs | | | mysql_escape_string Categories : PHP, MySQL, Databases, Strings | | | Automatically printing the contents of an sql table in MySQL. Categories : MySQL, PHP, HTML and PHP, Databases | | | Email a user with out exposing email address Categories : PHP, Databases, MySQL, Email | |
|
|
|