PHP’s Built-In MYSQL Functions
The MySQL database server is one of the most popular open source databases in the world. Most of php application developed using mysql database. PHP has many built-in MySQL functions which works with MySQL to execute queries. these functions help you to manage your database and speed-up development.
Below is the list of most used PHP/MYSQL built in functions in php.
1. mysql_affected_rows
The mysql function mysql_affected_rows() will return the number of rows or records affected by any update, insert or delete query. By using this function, you can check the success of ny updating. for example if you want to check whether the password changed or not, you can simply use this function after update action. if it returns one then updation is successful otherwise not.
- <?php
- /* mysql connection is required here to run this script */
- /* Update records */
- mysql_query(“UPDATE user SET password=’xyz’ WHERE id=’101′”);
- printf (“Updated records: %d\n”, mysql_affected_rows());
- /* Output; Updated Records: 1 */
- ?>
2. mysql_fetch_field
You can get all the details about the fields used in a table by mysql_fetch_field() function. This function returns an object and using that we can get required information about the field.
- <?php
- /* mysql connection is required here to run this script */
- $result = mysql_query(“SELECT id,email FROM people WHERE id = ’42′”);
- echo mysql_num_fields($result);
- /* OUTPUT: returns 2 because id,email === two fields */
- ?>
3. mysql_fetch_lengths
This function mysql_fetch_lengths() returns an array and all elements of this array stores the length of data in each field of that row
- <?php
- /* mysql connection is required here to run this script */
- $result = mysql_query(“SELECT id,email FROM people WHERE id = ’42′”);
- $lengths = mysql_fetch_lengths($result);
- print_r($row);
- print_r($lengths);
- ?>
- Output:
- Array
- (
- [id] => 42
- [email] => user@example.com
- )
- Array
- (
- [0] => 2
- [1] => 16
- )
4. mysql_fetch_row
The function mysql_fetch_row() returns a record by taking a result identifier. it returns the set of data as an array. To get the value from the array you have to use array offset staring from 0. Each call to this mysql_fetch_row function returns the next record.
- <?php
- /* mysql connection is required here to run this script */
- $result = mysql_query(“SELECT id,email FROM people WHERE id = ’42′”);
- $row = mysql_fetch_row($result);
- echo $row[0]; // 42
- echo $row[1]; // the email value
- ?>
5. mysql_field_flags
This function is used to collect flags associated with a field in a table. It takes a result set and field offset value of the field and returns all the flags separated with single space. Here all returned flags are separated by a single space so we can use explode function of PHP to create an array of flags.
- <?php
- /* mysql connection is required here to run this script */
- $result = mysql_query(“SELECT id,email FROM people WHERE id = ’42′”);
- $flags = mysql_field_flags($result, 0);
- echo $flags;
- print_r(explode(‘ ’, $flags));
- ?>
- Output:
- not_null primary_key auto_increment
- Array
- (
- [0] => not_null
- [1] => primary_key
- [2] => auto_increment
- )
6. mysql_field_type
The function mysql_field_type will return the type of field associated with the field. Different field types are varchar, char, int, blob, text, datetime etc… This command takes one result set as input along with a field identifier or an offset. It returns a string holding field type details.
- <?php
- mysql_connect(“localhost”, “mysql_username”, “mysql_password”);
- mysql_select_db(“mysql”);
- $result = mysql_query(“SELECT * FROM people”);
- $fields = mysql_num_fields($result);
- $rows = mysql_num_rows($result);
- $table = mysql_field_table($result, 0);
- echo “Your ’” . $table . “‘ table has ” . $fields . “ fields and ” . $rows . “ record(s)\n”;
- echo “The table has the following fields:\n”;
- for ($i=0; $i < $fields; $i++) {
- $type = mysql_field_type($result, $i);
- $name = mysql_field_name($result, $i);
- $len = mysql_field_len($result, $i);
- $flags = mysql_field_flags($result, $i);
- echo $type . “ ” . $name . “ ” . $len . “ ” . $flags . “\n”;
- }
- mysql_free_result($result);
- mysql_close();
- ?>
Output:
Your ‘people’ table has 4 fields and 1 record(s)
The table has the following fields:
string name 64 not_null primary_key binary
int age 11 not_null
string address 100 not_null
string country 100 not_null enum
7. mysql_pconnect
This function is used to make persistence connection to MySQL database from PHP script. Mysql_pconnect function keeps the connection live even the script execution is over. It does not close the connection like mysql_connect case where it closes the connection once the script execution is over. The other difference is it tries to find any existing connection exists and return a identifier if exists.
The main purpose of using mysql_pconnect function is to maintain a persistence connection to the mysql server. Function mysql_close() can’t close persisitence connection.
- <?php
- mysql_pconnect (“$servername”,“$dbuser”,“$dbpassword”);
- ?>
8. mysql_connect
mysql_connect function is used to connects Mysql databas. We can check the success of the function by checking the result. We will get a true result in case connection is established. Based on this we can even print a message saying the details. This function takes three parameters, first one is hostname then username and then password. We can also give the port number along with the hostname also.
Here is the function to connect to mysql database
- <?php
- mysql_connect (“$servername”,“$dbuser”,“$dbpassword”);
- ?>
9. mysql_create_db
You can create database in mysql server by using mysql_create_db function. If sufficient permission is there for the user then this function will create the database in MySQL Server.
Example:
- <?php
- /* mysql connection details*/
- $servername=‘localhost’;
- $dbusername=‘username’;
- $dbpassword=‘password’;
- $link=mysql_connect (“$servername”,“$dbusername”,“$dbpassword”)
- or die ( “ Not able to connect to server ”);
- /* Create new database*/
- if (mysql_create_db (“new_db”)) {
- print (“Database created successfully
- “);
- } else {
- print (“Error creating database:
- “. mysql_error ());
- }
- ?>
10. mysql_db_name
By using this function, you can get list all the databse hosted on the mysql server. Below is the example code to get list of all mysql database.
- <?php
- /* mysql connection is required here to run this script */
- $list_of_dbs = mysql_list_dbs();
- $i = 0;
- $total = mysql_num_rows($list_of_dbs);
- while ($i < $total) {
- echo mysql_db_name($list_of_dbs, $i) . ”
- “;
- $i++;}
- ?>
11. mysql_num_rows
The function mysql_num_rows return total number of rows in a table. This function is to be used along with mysql select query. This function is widely used in different php scripts.
Example :
- <?php
- /* mysql connection is required here to run this script */
- $query = mysql_query(“SELECT * FROM people”);
- $number=mysql_num_rows($query);
- echo “Total records in people table= ”. $number;
- ?>
12. mysql_query
This function is used to execute a sql query to mysql database. You can write any sql query like insert , select , update , delete etc and pass the query to mysql database. The result of the execution of the query will be known by monitoring the status. We will get true or false based on the success or failure status of the query.
Example :
- <?php
- /* mysql connection is required here to run this script */
- $query=“select name, address, country from people”;
- $status=mysql_query($query) or die( “sqlquery failed”);
- if($status){echo “sqlquery is successful”;}
- else {echo “sqlquery failed”;}
- ?>
13. mysql_errno
This function is used to display error number after execution of a sql query.
Example :
- <?php
- /* mysql connection is required here to run this script */
- $querry = mysql_query(“SELECT test_field FROM people”);
- echo “Error Number= ”.mysql_errno(); // will print 1054
- ?>
In the above table people test_field is a non existence field or column. That’s why it will show errorno.
14. mysql_error
This function is used to display error message after execution of a sql query.
Example :
- <?php
- /* mysql connection is required here to run this script */
- $querry = mysql_query(“SELECT test_field FROM people”);
- echo “Error message = ”.mysql_error();
- // Displaye error message after executing a query
- ?>
15. mysql_close
The function mysql_close() is used to close mysql connection. It can takes a optional parameter as link and closes it. If no link identifier is specified then last opened connection is closed. It is not necessary to use mysql_close() function as all connections are closed at the end of the script execution.
Example :
- <?php
- /* mysql connection is required here to run this script */
- mysql_close()
- ?>
Note: mysql_close() function will not close persistent connection created by using mysql_pconnect()
16. mysql_fetch_array
This function fetch a result row as an associative array, a numeric array, or both
Example Code:
- <?php
- /* mysql connection is required here to run this script */
- $result = mysql_query(“SELECT id, name FROM people”);
- while ($row = mysql_fetch_array($result)) {
- echo $row[0];
- echo $row[1];
- }
- ?>
17. mysql_result
The function mysql_result() is used to get large result data. You can pass numeric offset for the field argument to get records much quicker.
Example Code:
- <?php
- /* mysql connection is required here to run this script */
- $result = mysql_query(‘SELECT name FROM work.employee’);
- echo mysql_result($result, 2); // outputs third employee’s name
- ?>
18. mysql_fetch_assoc
This function retuns an associative array that corresponds to the fetched row.
Example Code:
- <?php
- /* mysql connection is required here to run this script */
- $result = mysql_query(‘SELECT name FROM work.employee’);
- $my_assoc_array=mysql_fetch_assoc($result);
- print_r($my_assoc_array); // print entire associative array
- ?>
Follow @phpzag

It’s a nice post.
little long but really happy to see it coz its very helpful for me and also for other.