MySQL tutorial on frequently used functions Free Web Design Tutorials
Home   HTML CSS JavaScript PHP MySQL Usability Glossary
MySQLBasicsCreate and DropShow and DescribeInsert, Update and DeleteQueryingJoin QueriesFunctionsTable LockingPHP/MySQL FunctionsAccessing a DatabaseQuerying with PHPCreate and Drop with PHPInsert & Update with PHPMySQL Frequently Used FunctionsFrequently Used Functions

Home > MySQL > Frequently Used PHP Functionsprinter version

MySQL Frequently Used Functions

The PHP MySQL library functions covered in the previous tutorials are probably enough to build a simple web application.

Below are some additional functions that are frequently used in web applications.

The mysql_num_rows() Function

This function can be used after a SELECT query to find the number of rows that the query returned.

It takes only one argument - the result set returned from the previous SELECT query and returns the number of rows.

Example of a Row Count after a Select Query
<?php
$query = "SELECT * FROM people";
$result = mysql_query($query, $connection) or exit(mysql_error());
$num_rows = mysql_num_rows($result);
echo($num_rows);
?>


Note: If you only require the number of rows in a table and not the contents then the code above is inefficient. Instead you should use SELECT COUNT(*) as shown below.


Finding the Row Count using SELECT COUNT(*)
<?php
$query = "SELECT COUNT(*) FROM people";
$result = mysql_query($query, $connection) or exit(mysql_error());
$row = mysql_fetch_array($result);
$num_rows = $row[0];
echo($num_rows);
?>


The mysql_num_fields() Function

This function can be used after a SELECT query to find the number of attributes in a row that the query returned.

It takes only one argument - the result set returned from the previous SELECT query and returns the number of attributes.

Example of an Attribute Count after a Select Query
<?php
$query = "SELECT * FROM people";
$result = mysql_query($query, $connection) or exit(mysql_error());
$num_fields = mysql_num_fields($result);
echo($num_fields);
?>


The mysql_data_seek() Function

This function is used to return a result that starts at a row other than the first row. The function is run after the SELECT query but before fetching a row.

It requires two arguments, the result set from SELECT query and the offset from the first row.

Example of mysql_data_seek()
<?php
$query = "SELECT * FROM people";
$result = mysql_query($query, $connection) or exit(mysql_error());
mysql_data_seek($result, 2);
$row = mysql_fetch_array($result);
$lastname = $row["lastname"];
echo($lastname);
?>


The mysql_close() Function

This function is used to close a connection opened with mysql_connect() and takes as an argument the connection handle returned by that function.

If no argument is supplied it assumes the most recently opened connection.

You're unlikely to use this function since connections are closed automatically when a PHP script terminates.

Previous - MySQL Insert and Update with PHP Previous - Insert and Update with PHP


Privacy | Terms | Contact | Links | Sitemap | RSS Feeds RSS and JavaScript Feeds
©2009 www.webdesignworkmate.co.uk all rights reserved 
Design and Production by smallbizonline website design © 2000-2009
Valid HTML 4.01! Level Double-A conformance icon, W3C-WAI Web Content Accessibility Guidelines 1.0Valid CSS!
Recommended Reading
PHP6 and MySQL5

PHP and MySQL Web Development

Build Your Own Database Driven Website using PHP and 

MySQL

How to do Everything with PHP and MySQL