Using MySql From php

Step 1 - Open a connection to MySQL

The first job is to actually connect to MySQL. As it's name suggests, mysql_connect( ) does exactly that. Here's the code we're going to be using. But this is just to get your started. It is recommended that you don't dash off and use this on the internet! This is for learning purposes only.
1
2
3
4
5
6
7
8
9
10
11
12
13
  
<?php
 
$user_name = "root";
$password = "";
$database = "addressbook";
$server = "127.0.0.1";
 
mysql_connect($server, $user_name, $password);
 
print "Connection to the Server opened";
 
?>
The first four lines are just setting up variables, and putting something in them:
1
2
3
4
$user_name = "root";
$password = "";
$database = "addressbook";
$server = "127.0.0.1";
The username we're trying here is "root" and the password is blank. These are the MySQL defaults. You don't need to change these, in most cases.

Hopefully, you won't have any errors. But the line that connects to MySQL is this:
mysql_connect($server, $user_name, $password);
So you type the name of the function first ( mysql_connect ), followed by the round brackets. In between the round brackets, you need three things: the name of your server, your MySQL username, and your MySQL password. These can be entered directly, like this:
mysql_connect( '127.0.0.1', 'root', '' );
Or as variables, like we did at first:
1
2
3
4
$user_name = "root";
$password = "";
$server = "127.0.0.1";
mysql_connect($server, $user_name, $password);

Step 2 - Specify the database you want to open

In our code, we set up a variable with the name of our database:
$database = "addressbook";
We now need to do something with this variable. So add this new line to your code (the new line is in blue text):
1
2
3
4
5
6
7
$user_name = "root";
$password = "";
$database = "addressbook";
$server = "127.0.0.1";
mysql_connect($server, $user_name, $password);
$db_found = mysql_select_db($database);
print "Connection to the Server opened";
You use the mysql_select_db( ) function to specify which database you want to open. The function then returns a true/false value. If it finds your database, a value of true is returned; if your database can't be found then a value of false is returned. You can use some logic to test if the database was found. Change the last two lines of your code to this:
1
2
3
4
5
6
$db_found = mysql_select_db($database);
if ($db_found) {
print "Database Found";
} else {
print "Database NOT Found";
}
Now change the database name from this:
$database = "addressbook";
to something like this:
$database = "addressbook2";
Run your code again, and you should see Database NOT Found printed out (unless you have a database called addressbook2). Change the database name back to addressbook.
But there's another option you can use for mysql_select_db - something called a resource link identifier. It's just a file handle that you used in an earlier section (opening text files). You use it like this:
1
2
3
4
5
6
7
8
9
10
11
12
$user_name = "root";
$password = "";
$database = "addressbook";
$server = "127.0.0.1";
 
$db_handle = mysql_connect($server, $user_name, $password);
$db_found = mysql_select_db($database, $db_handle);
if ($db_found) {
  print "Database Found " . $db_handle;
}else {
  print "Database NOT Found " . $db_handle;
}
So when we connect to the database, we're now using this:
$db_handle = mysql_connect($server, $user_name, $password);
It's just the same as before, except we're returning a value from the mysql_connect function, and putting it into a variable called $db_handle. When we connect to the database, we can use this file handle:
$db_found = mysql_select_db($database, $db_handle);
The resource link identifier (file handle) goes after the name of the database you want to open. You can then use this file handle to refer to your database connection.
Now that we've connected to MySQL, and connected to a database, it's time to close the connection.

 

Step 3 - Close the connection

Closing a connection to a database is quite easy. If you've used a file handle, as above, you just do this:
mysql_close( $db_handle );
Otherwise, you don't need to bother. It's recommended that you take the file handle approach, though. That's what we'll be doing from now on.
So, we'll add a line to close our connection. Here what your code should now look like:
1
2
3
4
5
6
7
8
9
10
11
12
$user_name = "root";
$password = "";
$database = "addressbook";
$server = "127.0.0.1";
$db_handle = mysql_connect($server, $user_name, $password);
$db_found = mysql_select_db($database, $db_handle);
if ($db_found) {
 print "Database Found ";
 mysql_close($db_handle);
} else {
  print "Database NOT Found ";
}

Post a Comment