note 90591 added to function.mysql-data-seek
| From: | webmail7 at suddenlink dot net | Date: | Wed, 29 Apr 2009 17:48:32 +0000 |
| Subject: | note 90591 added to function.mysql-data-seek | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-154012@lists.php.net to get a copy of this message | ||
The mysql_query() function using the SELECT, SHOW, DESCRIBE, EXPLAIN or other statements returning a
resultset, by default uses a TABLES INDEX, or simply the RECORD INSERT ORDER, in creating the
resultset.
When using the mysql_data_seek() function along with a mysql_query() function using the SELECT,
SHOW, DESCRIBE, EXPLAIN or other statements returning a resultset, and your desire is to SELECT A
SPECIFIC ROW based upon the CONTENTS of a SPECIFIC FIELD then be sure to use the ORDER statement in
your query or the mysql_data_seek() function may have unpredictable results.
Example (Find Last Record):
==============================================
<?php
$query="SELECT * FROM
team ORDER BY team.id ASC";
$result=mysql_query($query);
$last_row = mysql_num_rows($result) - 1;
if (mysql_data_seek($result, $last_row)) { //Set Pointer To LAST ROW in TEAM table.
$row = mysql_fetch_row($result); //Get LAST RECORD in TEAM table
$id = $row[0] + 1; //New Team ID Value
// more code here ...
} else { //Data Seek Error
echo "Cannot seek to row $last_row: " . mysql_error() . "\n";
}
?>
==============================================
** Note:
1. The above example code relates to a MYSQL table named 'team' similar to this:
+-ID-+ TEAM NAME-+
| 49 | Team 49 |
| 33 | Team 33 |
| 84 | Team 84 |
| 07 | Team 07 |
+----+-----------+
** Records shown in the order inserted (i.e. TABLE NOT INDEXED)
2. In the above example code the field named ID is NOT set to AUTO-INCREMENT (i.e. field value set
programmatically).
3. Wanting to insert a new team with a team number higher that the highest team number used, simply
using the following code snippet
==============================================
<?php
$query="SELECT * FROM team";
?>
==============================================
resulted in an ERROR when using the mysql_data_seek() function to select the record with the
highest ID value.
4. Use of the ORDER statement in the query corrected the error/problem.
Hope this helps someone.
----
Server IP: 69.147.83.197
Probable Submitter: 74.193.232.71
----
Manual Page -- http://www.php.net/manual/en/function.mysql-data-seek.php
Edit -- https://master.php.net/note/edit/90591
Del: integrated -- https://master.php.net/note/delete/90591/integrated
Del: useless -- https://master.php.net/note/delete/90591/useless
Del: bad code -- https://master.php.net/note/delete/90591/bad+code
Del: spam -- https://master.php.net/note/delete/90591/spam
Del: non-english -- https://master.php.net/note/delete/90591/non-english
Del: in docs -- https://master.php.net/note/delete/90591/in+docs
Del: other reasons-- https://master.php.net/note/delete/90591
Reject -- https://master.php.net/note/reject/90591
Search -- https://master.php.net/manage/user-notes.php