|
|
|
|
|
|
| |
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
|
|
| 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 | | | email new items in db Categories : PHP, Email, Databases, MySQL, Beginner Guides | | | 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 | | | 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 | | | Linked comboboxes with php-mysql & javascript Categories : PHP, Java Script, Databases, MySQL | | | 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 | | | usercounter class Categories : PHP, PHP Classes, Databases, MySQL, Environment Variables | | | This script is a contact form between users of a
website (kinda like the PM function on the forums)
Categories : PHP, Databases, MySQL, Regexps | |
| | | | 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...
| |
|
|
|