How to use php with MySQL

→ Are you a new visitor? Please visit the page guidance for new visitors ←

How to use php with mysql

First, what is MySQL? Mysql is the most used open source database engine on the internet. At the current time I believe MySQL community is being owned by Oracle, however it is still maintain as an open source software. In this article I’m going to show you basically how to use php with MySQL. I will provide simple examples, nothing too complicated and very easy to understand.

What is a Database Table?

But first, a little understanding on what is MySQL and how it is structured. As you already know, MySQL is a database, the data in a database is stored in database objects named tables. These tables have columns and rows and contain the actual data from your database!

An example of mysql database (name “students”) would be similar to this:

LastName FirstName YearsOld
Andy Brute 20
Jorge Medy 20
Jason Credy 20
Chris Indy 20

What are SQL Queries ?

A query is similar to a question or request, when you create a query to your MySQL database, you request the information needed from the tables within your database.

For example, you can get specific information from a specific row or directly from a column:

The query will return the following information from the database:


How to create a connection to a MySQL Database

Contrary to how big it sounds, it’s really simple to create a connection using php, you just use the function mysql_connect() and the correct parameters:


  • servername – the server where you need to connect to, by default this is localhost:3036
  • username – the username that connects to the mysql server, by default this is the owner of the server process.
  • password – the password of the mysql user, by default this is blank, “”

How to close a mysql connection

This is done really simple, by using mysql_close(), for example, using the first example we now add:

Using the function mysql_close() will close the mysql connection automatically at the end of each query. It is really important to close mysql connections as these are not unlimited and you can have problems if you leave them open.

So this is basically how you create and close a mysql connections using php.

How to create a mysql database name

The SQL sintax to create a database is quite simple:

For example, doing this using php following the example so far would be:

How to create a table

The SQL sintax to create a database table is:

The same as the other code so far, we need to use mysql_query() to run the SQL statement:

It is important to not forget adding the mysql_select_db() function for you to select the database you’re going to work with.

How to insert data into table

In any case, the same would be for all mysql queries. The next SQL statement I’m going to explain is the INSERT INTO statement.

Bellow you will find an example of this:

You can use two options for inserting data into a table, first statement in the above example is not stating the fields where each row value should be inserted, however you need to use the exact same number of row_value and positions of the row_value as your table structure field is. It is not recommended to use this if your table has a lot of fields.

The second statement specifies which fields to insert into, this is the most used and really the safest as you can’t go wrong here. I could also show you an example of php code but it is the same as the examples so far, just replace the mysql_query() value with your insert statement.

How to select from a database

Using the SELECT statement you can select any data from your mysql database from any specific table.
The SQL synstax that is used here is:

In the above example, it will select all rows from the specified fields from your table TABLE_NAME. You can also select all row values from table using

A php code example would be similar to this:

The result would be:

Andy Brute
Jorge Medy
etc. …

How to update data in a table

The next statement I’m going to show you is the UPDATE statement. This is used when you want to alter a row from field in a table or even all rows from a specific field.

An example of UPDATE SQL code is the following:

So the above code, will update all rows from field1 and field2 with their specific values where field3 matches “specific_value”.
A php code will look similar to the bellow example:

The result will have Andy Brute have 36 in the field YearsOld of the table students.

How to delete from a database table

To delete records from a database table, you need to use the SQL syntax “DELETE FROM” along with the table name.

An example can be seen bellow:

The above means, that you delete from TABLE_NAME where a field1 has a specific value that mathes your queries. For example, you want to remove the student who is 36 Years old.

Since we already have Andy Brute with 36 years old from our last UPDATE query, the record containing this information will be removed.

That’s it for now, if you have anything to add or recomandations, please comment bellow.

Request an article ←