Archives

July 2016
December 2015
December 2013
April 2013
January 2013
December 2011
November 2011
September 2011
December 2010
November 2010
March 2010
February 2010
January 2010



Connection to MySQL database 17-1-2010

Now we are going to know how to connect to MySQL database using PHP script. First of all, you have to know that PHP is working well and your first PHP page is ok. Second, you have to check if MySQL server is working well also, and that could be easily by using any DB browser such as MySQL Front or PHP MyAdmin. Third you can right the following code and see the result:



<?php

mysql_connect("servername","username","password");

mysql_select_db("database_name");

?>




If you get no error, and you get a blank white page then the connection is ok. And if you need to get an error message you can use the following code:



<?php

mysql_connect("servername","username","password") or die ("Server Error");

mysql_select_db("database_name") or die ("Database Error");

?>




While if you need a successful connection message, you can use the following code:



In this example, my server is "localhost" and the username is "root"



<?php

$con = mysql_connect("localhost","root","");

if($con){

print "Successfully connected to the server";

}

// My database name is "abc"

$db = mysql_select_db("abc");

if($db){

print "Successfully connected to the database";

}

?>




Notes:



- You can use mysql_pconnect() instead of mysql_connect()

- Do not forget to close the connection at the end of the page using

mysql_close($con);

mysql_free_result($results);



Regards,

Wail Shalabi.


 

Leave your comment
* Name:
* Email:
* Subject:
* Comment:
* Confirm: Please write the code above
 

Contacts

Mob: +4915753779176
E-Mail: wail(at)wailshalabi.com
Web: www.wailshalabi.com

Feed back form

 
Tags

PROJECT WEBSITE ABILITY LIKE SEARCH NIZAR VALUE QABBANI USERNAME NAME RESULTS USING WHERE ONE MYSQL TEXT APPLICATION NEW FOLLOWING DATA POST CODE TITLE EXAMPLE KNOW SEE SERVER KASS TIME WORDS COM OUT TYPE PRINT FILE HEAD VIDEOS NOT HOPE INTERNET WOULD TAG EMAIL ANY FRIENDS FORM AFTER PROGRAM GOING WHILE WORLD MORE SAME HTTP LAST ASP WILL PASSWORD CLIPS WRITE DEAR AGAIN MOBILE FINALLY NUMBER EVERY SENT COOKIES CSS PUT OPEN WORD SEND BACK WWW THEM SCRIPT TELL NOW PHRASE INPUT DATABASE CONNECT GOOGLE FTP TOP VARIABLE QUIZ SHOOF JSP