If you want to hone your skills in the field of web development and become an avid PHP programmer, then you must have the knowledge of certain things without any doubt. One of the most important things among these certain things is how to deal with database connections, especially with MySQL database platform.
If you are having the knowledge of working with MySQL database in PHP, then you can create almost every type of websitewith dynamic content. You can perform tasks like handling user registration, commenting, posting blogs and many more. Once you have acquired the knowledge of using MySQL database, then no one can stop you from excelling in this field. The foremost thing you must know is how to connect to a MySQL database in PHP. Following is the PHP code you should follow to connect:
$host = “localhost”;
$username = “db_username”;
$password = “db_password”;
$database = “db_name”;
$dbc = mysql_connect($host, $username, $password);
I know you must be wondering what a piece of crap this code is if you are not familiar with the language of coding. If you have already worked in PHP, then you already know that $host, $username, $password and $database are all variables. Each of these corresponds to what a variable is actually called. Like, $host indicates the name of host databaseis located on(99% of the times on localhost), $username indicates the username to access database, $password is the one associated with username and $database indicates the name of database we are connecting to.
Moving further, next two lines in the code comprise PHP functions. The first function, mysql_connectis used to make connection to MySQL on your host. If you are working with multiple connections, then it returns a link identifier. It takes various parameters like database, username, host and password. The next function, mysql_select_db, selects the database we will be using. Two databases taken as parameters are database name and link identifier(link identifier is optional, but if you do it, it’s a good practice).
This PHP Code forms the basic access to a database. Once you are done with this, you can do tasks like retrieving data using mysql_query function. This query also uses two parameters viz. query string and a link identifier. If you want an example, then have a look at the following code:
$query = “INSERT INTO table (column1, column2,…) VALUES (‘data1’, ‘data2’,…)”;
$result = mysql_query($query, $dbc);
These ellipsis in the code are just to show that you can insert n number of columns in it. These two lines of code will comprise for inserting data into database. The first line of code is for assigning a query to $query variable. The second line is used for running the query with associated link identifier.
Once you master all these functions, you will become an expert in PHP programming with databases. This is not that only these three steps are enough for programming, but they would suffice for the beginning. You should focus on other steps as well to work well with databases. As you proceed learning more and more functions, you will ve able to monitor your progress by yourself.
That’s all for the day and you can also go for a PHP Course for more details.