Basics
Creating a Document
Head and Body Elements
Formatting Text
Creating Lists
Embedding Images
URLs Explained
Linking to Documents
Creating Tables
Forms Form Basics
Input and Textarea
Select Lists
Frames Framesets
The Frame Element
Nested Framesets
Targetting Frames
Inline Frames
Tips and Tricks Meta Tags
Transition Effects
HTML Generators Create a Document
Create a List
Create a Table
HTML
Basics
Implementing CSS
CSS Syntax
Pseudo Classes/Elements
CSS Classes
CSS Properties Font Properties
Color and Background
Text Properties
Border Properties
Margins and Padding
Size and Position
Tips and Tricks Menu Buttons
Special Effects
CSS
Basics
Running a Script
Variables
Expressions and Operators
Objects.Properties.Methods
The Date Object
Strings
Regular Expressions
Defining RegExp Patterns
Branches and Conditions
Loops
Arrays Array Basics
Array Methods
Sorting Arrays

User-Defined Functions
Cookies
Windows
Frames
Tips and Tricks Image Replacement
Using Includes
Form Validation
Debugging
JavaScript
Basics
Creating a Script
Running a Script
Variables
Expressions and Operators
Strings Strings Basics
Strings and Substrings
Replacing Substrings
Regular Expressions
Branches and Conditions
Loops
Arrays Array Basics
Array Functions
Sorting Arrays
User-Defined Functions
Include and Require
Uploading Files
File Functions
Session Variables
Tips and Tricks Page Templates
Form Reply Scripts
Form Validation
JavaScript to PHP
PHP
Basics
Create and Drop
Show and Describe
Insert, Update and Delete
Querying
Join Queries
Functions
Table Locking
PHP/MySQL Functions Accessing a Database
Querying with PHP
Create and Drop with PHP
Insert and Update with PHP
Frequently Used Functions MySQL
Basics
Layout and Navigation
Page Content Style
Web Page Copy
Graphics and Animation
HTML Forms
Accessibility
Legal Requirements
MySQL
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
©2010 www.webdesignworkmate.co.uk all rights reserved 
Design and Production by smallbizonline website design © 2000-2010
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