PHP and MySQL Connectivity

We know that there are lots of tutorials (some official too) on the internet about PHP and MySQL connectivity. Then why am i posting it here ? i am posting because most of those articles are not simple and easy to understand and this is why we are doing it here. We assume that you have already installed XAMPP in your System and running it using XAMPP control panel.

To connect PHP to MySQL database you need to know following important things:

Host name.

MySQL user name.

MySQL password.

If you have installed XAMPP in your system (not web server) then host name is localhost. By default MySQL user name and password is “root” and blank ( “” ) respectively. Let us create one simple project and try to connect our PHP code to MySQL.

If you are on Windows then there is “htdocs” folder in “C:/xampp/htdocs/” (if installed on default location). If you are on Linux (most probably Ubuntu) then it is located on “/opt/lampp/htdocs” (you should switch to root user before creating folder in it.).

In any case create any folder, lets say “test-db-connection” and create simple PHP file with following code.

$host="localhost";$user="root";$password="";$con=mysql_connect($host,$user,$password);if(!$con){echo'<h1>Connected to MySQL</h1>';}else{echo'<h1>MySQL Server is not connected</h1>';}

Save it in the project folder and go to localhost/test-db-connection/filename.php and see what it is giving.

mysql_connect() is PHP inbuilt function to connect to MySQL database with the parameter shown above.

To perform SQL queries, you need to select database. You can do that using mysql_select_db(“database_name”,optional connection variable). Once Database is selected you can perform queries using mysql_query(“SQL query”). Here is sample code.

$host="localhost";$user="root";$password="";$con=mysql_connect($host,$user,$password);if(!$con){echo'<h1>Connected to MySQL</h1>';//if connected then Select Database.$db=mysql_select_db("YOUR_DATABASE_NAME",$con);$query=mysql_query("YOUR_MYSQL_QUERY",$db);}else{echo'<h1>MySQL Server is not connected</h1>';}

I hope you have understood the concept. You can ask any doubt in comment.

The function “mysql_query” is responsible for executing the query within the brackets on the database. But how does this function know which database to use? $conn and $db have been defined, but they are never used within the code.

Yes. Let’s say you are building Registration form where user will submit and their information will be store in Database , in that kind of scenario you need to connect to your database ( assuming MySQL here) in order to do further database operations.

In this particular case, it doesn’t make any difference whatsoever whether you use mysql_connect or mysqli_connect. The former is deprecated and will be removed at some point in the future, but as of PHP 5.5 it still works.

I did try this procedure on my XAMPP but it not working .
if anyone could help me please .
i got this error in my browser.
Fatal error: Uncaught Error: Call to undefined function mysql_connect() in C:\xampp\htdocs\conn.php:5 Stack trace: #0 {main} thrown in C:\xampp\htdocs\conn.php on line 5

I’m very new to PHP, but not programming. The following code from the example would seem to read that if it is NOT connected then echo ‘Connected to MySQL’. Maybe it’s different in PHP, but in other languages the ! sign means NOT. (I’m not shouting with capital letters just trying to be clear)

Hi,
I am not able to select database only
I just wasted more than 15 hours everytime it says connection problem
Code:
$con is my connection string
$con=mysql_connect(“localhost”,”root”,””);
mysql_select_db(“test”,$con) or die(“connection problem”);

Fatal error: Uncaught Error: Call to undefined function mysql_select_db() in C:\xampp\htdocs\download\upload.php:50 Stack trace: #0 {main} thrown in C:\xampp\htdocs\download\upload.php on line 50
Below is the entire code.Please show me what is wrong: