Friday, March 24, 2006

mysql_fetch_array

(PHP 3, PHP 4, PHP 5)mysql_fetch_array -- Fetch a result row as an associative array, a numeric array, or both

Descriptionarray mysql_fetch_array ( resource result [, int result_type] )
Returns an array that corresponds to the fetched row and moves the internal data pointer ahead.

Parameters
result
The result resource that is being evaluated. This result comes from a call to mysql_query().
result_type
The type of array that is to be fetched. It's a constant and can take the following values: MYSQL_ASSOC, MYSQL_NUM, and the default value of MYSQL_BOTH.

Return Values
Returns an array that corresponds to the fetched row, or FALSE if there are no more rows. The type of returned array depends on how result_type is defined. By using MYSQL_BOTH (default), you'll get an array with both associative and number indices. Using MYSQL_ASSOC, you only get associative indices (as mysql_fetch_assoc() works), using MYSQL_NUM, you only get number indices (as mysql_fetch_row() works).
If two or more columns of the result have the same field names, the last column will take precedence. To access the other column(s) of the same name, you must use the numeric index of the column or make an alias for the column. For aliased columns, you cannot access the contents with the original column name.
mysql_result

(PHP 3, PHP 4, PHP 5)mysql_result -- Get result data

Descriptionstring mysql_result ( resource result, int row [, mixed field] )
Retrieves the contents of one cell from a MySQL result set.
When working on large result sets, you should consider using one of the functions that fetch an entire row (specified below). As these functions return the contents of multiple cells in one function call, they're MUCH quicker than mysql_result(). Also, note that specifying a numeric offset for the field argument is much quicker than specifying a fieldname or tablename.fieldname argument.

Return Values
The contents of one cell from a MySQL result set on success, or FALSE on failure.

Examples

Example 1. mysql_result() example
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
$result = mysql_query('SELECT name FROM work.employee');
if (!$result) {
die('Could not query:' . mysql_error());
}
echo mysql_result($result, 2); // outputs third employee's name
mysql_close($link);
?>
About New York PHPNYPHP is a non-profit organization committed to promoting and supporting the PHP programming language, helping New Yorkers share development experiences, and developing Open Source projects for the PHP community.Join UsSubscribe to our mailing lists and aid the community by sharing your experiences, leading discussions and offering support. Plus, we are always eager for energetic directors to join and help build the NY and worldwide PHP communities.Attend MeetingsOur free monthly meetings are open to the public and held on the 4th Tuesday of the month at IBM. Check our website for details.Acquire TechnologyWith PHP connecting many facets of the Internet, we support the AMP (Apache/MySQL/PHP) Technology suite, plus JSP, .NET, ColdFusion, Oracle and a variety of operating systems. Let the NYPHP community help you reach a new level of performance and functionality.Learn MoreYou'll find NYPHP's projects, mailing lists, announcements, job opportunities, and up-to-the-minute information about meetings, news and events at http://nyphp.org.
This simple example shows how to connect, execute a query, print resulting rows and disconnect from a MySQL database.

MySQL extension overview example

// Connecting, selecting database
$link = mysql_connect('mysql_host', 'mysql_user', 'mysql_password')
or die('Could not connect: ' . mysql_error());
echo 'Connected successfully';
mysql_select_db('my_database') or die('Could not select database');

// Performing SQL query$query = 'SELECT * FROM my_table';
$result = mysql_query($query) or die('Query failed: ' . mysql_error());

// Printing results in HTML
echo "\n";
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo "\t\n";
foreach ($line as $col_value) {
echo "\t\t\n";
}
echo "\t\n";
}
echo "
$col_value
\n";
// Free resultsetmysql_free_result($result);

// Closing connection
mysql_close($link);
?>
Installation on Windows Systems
PHP 4
The PHP MySQL extension is compiled into PHP.
PHP 5+
MySQL is no longer enabled by default, so the php_mysql.dll DLL must be enabled inside of php.ini. Also, PHP needs access to the MySQL client library. A file named libmysql.dll is included in the Windows PHP distribution and in order for PHP to talk to MySQL this file needs to be available to the Windows systems PATH. See the FAQ titled "How do I add my PHP directory to the PATH on Windows" for information on how to do this. Although copying libmysql.dll to the Windows system directory also works (because the system directory is by default in the system's PATH), it's not recommended.
StopBounce
StopBounce StopBounce StopBounce