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 : PHP Database connection function with security
Categories : PHP, Databases, MySQL
bastien koert
Date : Oct 22nd 2004
Grade : 3 of 5 (graded 12 times)
Viewed : 44961
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 
 

After using PHP for a while and getting really tired of maintaining db connections in each page, I abstracted the code into a function. This allows me to call the function from anywhere using a simple include. This include also provides some rudimentary security by checking to see where this is being called from.

Now I had a function that I only needed to maintain on one page. Life was simpler.



conn.php
<?php
//check if a current session is in place and the user is correctly logged in
//also check the calling page / domain to ensure the call only comes from
//this domain -- this check may take a little configuration to get the
//correct host name

//echo "server is : ".$_SERVER['SERVER_NAME'];     //use this the first time to get the host name...then comment it out or delete to complete the security check

//if (($_SERVER['SERVER_NAME']!="www.bastienkoert.net")){
   
if (($_SERVER['SERVER_NAME']!="localhost")){
     echo
"piss off, hackers!";
    die();
}
function
connect($sql)
{
   
/*
    $username     = "bastien_guest";
    $pwd                = "guest";
    $host             = "localhost";
    $dbname         = "bastien_showguest";
*/   
   
$username     = "bastienkoert";
   
$pwd                = "fantasi44ph";
   
$host             = "localhost";
   
$dbname         = "meds";
/*
    $username = "root";
    $pwd      = "rum31nt";
    $host       = "localhost";                                   
    $dbname     = "meds";
       
*/
   
if (!($conn=mysql_connect($host, $username, $pwd)))  {
       
printf("error connecting to DB by user = $username and pwd=$pwd");
       exit;
    }
   
$db=mysql_select_db($dbname,$conn) or die("Unable to connect to database1");
   
   
$result = mysql_query($sql, $conn)or die("Unable to query local database <b>". mysql_error()."</b><br>$sql");
    if (!
$result){
        echo
"database query failed.";
    }else{
        return
$result;
    }
//end if
}//end function
?>


Usage Example
<?php
include("conn.php");
$sql = "select * from tableName where....";
$result = connect($sql);
if (
$result){
   
//do something with the result
}//end if
?>


Enjoy,

Bastien



bookmarker - PHP, PHPLIB, MySQL WWW based bookmark manager
Categories : MySQL, PHP, MySQL, Complete Programs, Databases
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
Simple db results paging example
Categories : PHP, MySQL, Databases, Form Processing
A Complete table(ADD,EDIT,VIEW,DELETE) management System PHP,MYSQL, JAVASCRIPT
Categories : PHP, MySQL, Java Script, Databases
Convert a File database into MySQL
Categories : PHP, Filesystem, Databases, MySQL, Beginner Guides
for each record, do this to the first record, and do that to any subsequent record
Categories : PHP, Databases, MySQL, Beginner Guides
DBXML- A Class to backup databases in XML Format using web interface
Categories : PHP, PHP Classes, Databases, MySQL, XML
Dynamically generated pop-ups (Select items)
Categories : PHP, HTML and PHP, MySQL, Databases
DB Connection Function with error handling and email failure notices
Categories : PHP, MySQL, Errors and Logging, Databases, Errors and Logging
database,php,mysql,demo,example,php3,training,tutorial,codes,code
Categories : Databases, MySQL, PHP, PHP Options and Info
Displaying records of database in more than one page (paging)
Categories : Databases, MySQL, PHP
Inserting data to a MySQL Database using AJAX
Categories : AJAX, HTTP, PHP, Databases, MySQL
MySQL Class to ease Database connectivity
Categories : MySQL, PHP Classes, Databases, PHP
This is a very simple BBS that uses MySQL
Categories : MySQL, Databases, Complete Programs, PHP
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
 Yehuda Zadik wrote : 1222
Why not use a class ?
 
 bastien koert wrote : 1224
not everything has to be classes and OO
 
 Ilir Fekaj wrote :1634
This script will create database connection for every query...