WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDESPHP CLASSESCODE SEARCHARTICLES SEARCHPHP FORUMSPHP MANUALPHP FUNCTIONS LISTWEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Join us on FaceBook
Submit a code Example / Snippet Submit Your Code
Poker Tournaments Poker Tournaments
Poker Guide for Developers Poker Guide for Developers
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
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 Resources
Web Development Content
Internet Security Software
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
פרייסז - הכח לקנות עובר לידיים שלך
Texas Holdem Poker Evangelists

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 : DB Connection Function with error handling and email failure notices
Categories : PHP, MySQL, Errors and Logging, Databases, Errors and Logging
bastien koert
Date : Aug 27th 2004
Grade : 3 of 5 (graded 3 times)
Viewed : 12671
File : No file for this code example.
Images : No Images for this code example.
Search : More code by bastien koert
Action : Grade This Code Example
Tools : My Examples List

Submit your own code examples  Submit your own code examples 
 

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
SQL / PHP based Integrated Authentication
Categories : PHP, Authentication, Databases, MySQL
PhpLens PHP Application Server
Categories : Content Management, Databases, PHP, MySQL
Mimic ASP's GetString function with PHP
Categories : PHP, Databases, MySQL, Strings
Simple Mini Poll class library (SimPoll)
Categories : PHP, PHP Classes, Databases, MySQL, Complete Programs
Powerful php/mysql Pagination for up to 6 URL Params
Categories : PHP, PHP Classes, Databases, MySQL, Navigation
This functions makes it easy to use session-variables known from ASP. With one Cookie the array "session" will save and restore from a db-record. In this version MySQL is used but it's should very easy to change
Categories : PHP, Arrays, Cookies, MySQL, Databases
Finding the day of the week for a specific date.
Categories : PHP, Databases, MySQL, Date Time
A Simple Script to randomly select a CSS from MySQL
Categories : PHP, Databases, MySQL, CSS
MySQL or SQL Query to XML Output
Categories : PHP, MySQL, XML, Databases
PHP and MySQL scripting for Muyltiple CheckBoxes
Categories : HTML and PHP, MySQL, Databases, PHP
complete, simple, working example of a login screen/system using php functions, cookies, and a mysql database for begginers.
Categories : Authentication, Complete Programs, PHP, MySQL, Databases
Making a simple Hit-Log using PHP and MySql
Categories : PHP, Log Files, Beginner Guides, Databases, MySQL