Pages

Steps to communicate with MySql database using PHP

If your PHP web application is dealing with a database most commonly MySql,The knowledge about the following functions and their order as described is better.

  1. mysql_connect(hostname,databaseUserName,databasePassword):This is the first function which will establish a connection with the database.It returns false on connection failure.
  2. mysql_select_db("databaseName","connectionName"):This function selects the database.It takes database name and connection name as parameter, Note that connection name is optional.This function can also return false in case of any error.
  3. mysql_query("sql"):After the connection is established and the database is selected successfully now we can talk with the database by using structured query language via mysql_query() function.This function returns the dataset on success or it can return false if the query was not executed due to some error.
  4. mysql_fetch_assoc(datasetRetrieved):This function iterate through the dataset which is retrieved as a result of mysql_query() function and gets the record one by one in the form of array.The PHP mysql_fetch_assoc () function mostly used in a while loop where each record is required to be extracted.
  5. mysql_num_rows(datasetRetrieved)This function takes the mysql_query() resulted dataset and return the number of rows this dataset has.

 

0 comments:

Post a Comment