mysql_resultSynopsis
Descriptionmysql_result() fetches a single field from a MySQL result set. The function accepts two or three arguments. The first argument should be a mysql result handle returned by mysql_db_query() or mysql_query() . The second argument should be the row from which to fetch the field, specified as an offset. Row offsets start at 0. The optional last argument can contain a field offset or a field name. If the argument is not set, a field offset of 0 is assumed. Field offsets start at 0, while field names are based on an alias, a column name, or an expression. If an alias is present in the queryas name in the following query), the alias will be used as the field name. In other cases, either the column nameagein the following query) or expression birthday+0 in the following query) is used as the field name. SELECT age, birthday+0, CONCAT(first, ' ', last) as name FROM table
CautionCalls to mysql_result() reset the mysql result handle's internal row pointer. This behavior means that calls to mysql_result() and mysql_fetch_array() , mysql_fetch_assoc() , mysql_fetch_row() , or mysql_fetch_object() should not be used on the same result handle. ExampleExample 827. Use mysql_result() to fetch a single value <?php // Included code that connects to a MySQL server and sets a default database // See the MySQL Functions chapter introduction for the source code for the file include ('mysql_connect.inc.php'); // Find out how many user logins are palindromes $query = "SELECT count(login) FROM user WHERE login = reverse(login)"; echo 'Login Name Palindrome Count: ', mysql_result (mysql_query ($query), 0); ?> Example 828. Use mysql_result() to fetch a couple of values <?php // Included code that connects to a MySQL server and sets a default database // See the MySQL Functions chapter introduction for the source code for the file include ('mysql_connect.inc.php'); // Get the current time and UNIX timestamp $query = "SELECT NOW(), UNIX_TIMESTAMP() as stamp"; $mysql_result = mysql_query ($query) or die ("Query '$query' failed with error message: \"" . mysql_error () . '"'); // Note how we use the function call for the first field name // ...and the field alias for the second field name echo 'The current date and time is: ', mysql_result ($mysql_result, 0, 'NOW()'), '.<br />', 'The current UNIX timestamp is: ', mysql_result ($mysql_result, 0, 'stamp'), '.<br />'; ?>
PHP Functions Essential Reference. Copyright © 2002 by New Riders Publishing
(Authors: Zak Greant, Graeme Merrall, Torben Wilson, Brett Michlitsch).
This material may be distributed only subject to the terms and conditions set forth
in the Open Publication License, v1.0 or later (the latest version is presently available at
http://www.opencontent.org/openpub/).
The authors of this book have elected not to choose any options under the OPL. This online book was obtained
from http://www.fooassociates.com/phpfer/
and is designed to provide information about the PHP programming language, focusing on PHP version 4.0.4
for the most part. The information is provided on an as-is basis, and no warranty or fitness is implied. All
persons and entities shall have neither liability nor responsibility to any person or entity with respect to
any loss or damage arising from the information contained in this book.
|