2
Posted November 17, 2012 by Victor in PHP Programming
 
 

How to Execute MySQL Functions in PHP

php-mysql
php-mysql

Once you have connected MySQL with PHP (if you don’t know how, then, you can read our guide on how to connect MySQL with PHP), you will need to use MySQL functions to execute the various MySQL events. There are many MySQL functions that are used in PhP and each function performs different tasks. Here are some of the most basic and important MySQL functions you will need in PHP and how they are used:

  • mysql_connect ()

This function opens a connection in the PhP program with the Mysql database. How the function works has been widely covered in our tutorial on how to use MySQL with PHP. However, the code works in the following way:

<?php
  $con = mysql_connect("localhost", "username", "password");
  if (!$con)
    die('Could not connect to the server: ' . mysql_error());
?>
  • mysql_query()

This is a very important MySQL function in PhP because it helps in manipulating queries to the MySQL database. This function has the ability to fetch and automatically buffer mysql database record sets.  Here are examples of how the mysql_querry () function works: //Creating a database:

<?php
  $con = mysql_connect("localhost","username","password");
  if (!$con)
   die('Could not connect: ' . mysql_error());
  mysql_query ("CREATE DATABASE victor_db", $con);
?>
  • mysql_select_db()

This function is used for the purposes of selecting a created MySQL database once the said database has been created. For instance, if you want to open a connection, create a database called victor_db and create tables within the said database, here is how it works:

<?php

 // open a connectiom to MySQL
 $con = mysql_connect("localhost","username","password");
 if (!$con)
   die('Could not connect: ' . mysql_error());

 // create a database named victor_db in MySQL once the connection is open
 mysql_query ("CREATE DATABASE victor_db", $con);

 // select the database victor_db so that you can use it

 mysql_select_db ("victor_db", $con);

 // create a table named personal_info  within the victor_db database
 mysql_query("CREATE TABLE personal_info (FirstName varchar (20), LastName varchar (20), YearofBirth int (4))", $con);

 // inserting  data into the table Personal_info in the victor_db database
 mysql_query ("INSERT INTO personal_info (FirstName, LastName, YearofBirth) VALUES ('Victor', 'Mony', '1988')", $con);

 // selecting all the data that has been inserted into the victor_db database

 mysql_query ("SELECT * FROM victor_db");

 // selecting specific information from the victor_db database

 mysql_query ("SELECT * FROM victor_db WHERE YearofBirth=1988");

 // close mysql connection to PhP

 mysql_close ($con);

?>

  • mysql_close()

This function closes a connection that had earlier on been opened by the mysql_connect() function. Again, it has been discussed on our tutorial How to use MySQL with PhP. This function is used after you have finished working on your MySQL database in PhP and you wish to exit connection as indicated above. An example of how it works is simple:

<?php

  mysql_close ($con);

?>

There is much more you can do with the MySQL functions but this gives you a basic understanding how the mysql functions work in PHP. One thing you must have discovered is that these functions are used progressively. This means that you have to open a connection before you can perform any other task. You also have to create a database, select the created database and add tables and then add the information to the tables in the database. Once this is done, you can now control the tables by renaming them, deleting them, adding more information and calling them. Later, we shall look at how to present tables in PHP but for now, try finding out the many MySQL functions in PHP and how these functions work. Using MySQL in PHP is an interesting and easy topic that is simple to execute with the right knowledge and interest at hand.


Victor