Hi friends to day i am going to tell you about something about PHP and MySQL. We all know that PHP is a one of best scripting language for web development.We can build awesome dynamic web pages using PHP.But we must build something valuable other than produce unimportant web sites. So today i am going to tel you how to write and test the OOP based connection class to MySQL database using PHP.
<?php /** * @author damith * @copyright 2011 */ class createConnection //create a class for make connection { var $host="localhost"; var $username="username"; // specify the sever details for mysql Var $password="password"; var $database="database name"; var $myconn; function connectToDatabase() // create a function for connect database { $conn= mysql_connect($this->host,$this->username,$this->password); if(!$conn)// testing the connection { die ("Cannot connect to the database"); } else { $this->myconn = $conn; echo "Connection established"; } return $this->myconn; } function selectDatabase() // selecting the database. { mysql_select_db($this->database); //use php inbuild functions for select database if(mysql_error()) // if error occured display the error message { echo "Cannot find the database ".$this->database; } echo "Database selected.."; } function closeConnection() // close the connection { mysql_close($this->myconn); echo "Connection closed"; } } ?>
OK.. Now you have create the connection class and now you can call this class inside your project. To test the connection i will get another PHP file and create a object from my existing connection class.. see below code how i am going to do it..
<?php include ('connection.php'); $connection = new createConnection(); //i created a new object $connection->connectToDatabase(); // connected to the database echo "<br />"; // putting a html break $connection->selectDatabase();// closed connection echo "<br />"; $connection->closeConnection(); ?>
I think you know how to test this.Start wamp server and goto your local host’s folder and test it.. If you success you can see the below message on a browser
Now you have connected to the MySQL friends.. see you in next post
[…] more here: How to write a Connection class to MySQL database using PHP … Category: TipsTags: […]
how can i insert the record into the database
Please refer
thank you! it was very useful but how can you make a function that will insert, delete, update data in a certain table. waiting for your response 🙂 thanks a lot
I will upload those tutorials very soon..
Not even show anything. I don’t know why.
Post your code here..
it’s very helpful thank u
Very nice representation
Thank you
You are welcome.
thanx mr, daith it,s for help but the question how i can connect mysql database with barcode
I am sorry. I didn’t get your question….
Very well article. This is very helpful to a newbie like me. 🙂 Keep it up 🙂
Can I ask some question, How can I insert/update/delete using this method? Can you give me some example related to this article?
Thanks in advance
Thank You
its working
very nice
Thank you ..Was very useful but how can I insert/update/delete using this method?
Could you give an example in this case?
I need help…
I will publish later…
Thanks you very much……It is very useful for beginners.Please add some tutorial for beginners.
Thanks you so mach .really this was more helpful to me…
It’s actually a cool and useful piece of information. I’m satisfied that
you shared this useful information with us. Please stay us up to date like this.
Thank you for sharing.
Merci pour ce bluffant billet sur referencement outils.
Ton Site How to write a Connection class to MySQL database using PHP
| Damith’s Blog est une grande source d’infos .
I am thinking of implementing the same with PHP PDO, do you have the same tuts for that, if so please update..
Hi damith,
i want the code which inserts the form data by using php oops
fine thanks ,it very helpful……
i Try It Good Code For New Php Use thanx
http://ani2nil.com
very cute website
var keyword is now not in use. please update the post with new api mysqli.
sir ? for selectDatabase() function it doesn’t require Sql Connection ?
My Code is the same for you but there is no Database Selected ?
Watch this video.It will defiantly help you…
this is same with mySqli
class createConnection
{
var $host = “localhost”;
var $username = “root”;
var $password = “”;
var $database = “hostnile”;
var $myConn;
function connectToDatabase()
{
$db = new mysqli($this->host, $this->username, $this->password, $this->database);
if (mysqli_connect_errno()) {
printf(“connect failed : %s\n”, mysqli_connect_errno());
} else {
$this->myConn = $db;
}
return $this->myConn;
}
}
and my include file
$connection = new createConnection(); //i created a new object
$connection->connectToDatabase(); // connected to the database
I have a question why I should use all this, usually I do this
$host=”localhost”;
$username=”root”;
$password=””;
$dbname=”dbName”;
$db=new mysqli($host, $username, $password, $dbname);
if(mysqli_connect_errno()){
printf(“connect failed : %s\n”, mysqli_connect_errno());
exit();
}
and I include this file config.php in my index file and I have connection to database ready and running normally why I should use class and all this things here
Thanks
perfect…. thanks a lot..!
very very good..
Good Work !
can you tell me why “$my_conn ” variable is used.?
hey nice tutorial……….can u plz provide link for insertion , updation and deletion using php and oop
Unfortunately i don’t have.