It is very time consuming to copy and paste required parameters into every script. To save time create a class called mysql.php to store all the information for your database in one central location.
You can store usernames, passwords, and table names, and easily access them from any script.
In ASP.net it is very easy, database parameters are stored and accessed from one central place; however, PHP is different. There is really no central configuration file.
The best way to deal with this is to create a generic class that you can use anywhere. Here is an example of a generic database class;
class mysql_data
{
//** Configure connection to database.
public $host = "Your database server name.";
public $username= "username";
public $password= "password";
public $dbid="Enter your database name here.";
//** List all tables you are using.
public $tbl1="Buy"; // Table name 1
public $tbl2="Company"; // Table name 2
//*** List any select statements used.
// constructor
public function __construct() {
//Do not initialize anything.
}
}
Call the class using; require 'mysql_conf.php'. It should be the first line in your script. Then setup a pointer to the class, $mysql_class_object = new mysql_data; //Declare pointer to sql class.
After configuring a pointer to your class, you need to setup your variables to access the public variable names in your class object. Use this format.
//Setup variables for our class object. (To access public variables you must setup another variable.)
$host=host;
$dbid=dbid;
$user=username;
$pass=password;
$table1=tbl1; //Buy
$table2=tbl2; //Company
//*****************
To start use the variable names you setup, you must use this format;
$our_host=$mysql_class_object->$host;
See the php manual for further info on public variables, http://php.net/manual/en/language.oop5.php.
You can create generic class objects for almost anything you routinely do in php. By building up a library of class objects, you can speed up your development time, and cut down on errors. When creating a class object keep in mind the following;
1) Re-usability
Plan your class well. Think about what your using it for. After you create the class you should not need to modify it for any application.
2) Applications
Where is the class object going to be used in the future. It is important to understand where you are going to use the class object in your future projects. You want to keep things simple, and not go on a rampage, creating a hundred useless class libraries that you hardly use. Think about things that you do routinely from project to project, like updating a table in a database.
Simple is best. Don't over complicate. The more code you write, the longer it takes you to finish a project, and the more frustrating it makes it for anyone else who has to go through your code.
Master the basics of Photoshop. Step buy step video's reveal shortcuts on how to master Photoshop quickly and easily. In just two hours, learn how to create graphics quickly, easily and stress free.
Order, Learn Photoshop Now. Visit learnphotoshopnow.com.
No comments:
Post a Comment