If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > PHP > please explain how fetching (eg mysql_fetch_row) works

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-22-12, 05:17
hello-world hello-world is offline
Registered User
 
Join Date: Dec 2011
Posts: 15
please explain how fetching (eg mysql_fetch_row) works

If I do:

Code:
$result = mysql_query("SELECT id FROM $table WHERE age=20");
$row = mysql_fetch_row($result);
$row = mysql_fetch_row($result);
This will select the the first two IDs of users who are 20 years old. And if I add more $row=... then it will keep fetching the IDs until no more rows are left.

My question is how does this actually work -- is the variable "$result" automatically being diminished by one row each time mysql_fetch_row is called? I'm just a bit confused because I'm not manually modifying $result. Is this behaviour due to the $result variable being a special "resource" variable?
Reply With Quote
  #2 (permalink)  
Old 01-22-12, 09:43
healdem healdem is offline
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 9,262
as per PHP: mysql_query - Manual

For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error.

Quote:
For other type of SQL statements, INSERT, UPDATE, DELETE, DROP, etc, mysql_query() returns TRUE on success or FALSE on error.

The returned result resource should be passed to mysql_fetch_array(), and other functions for dealing with result tables, to access the returned data.
$result is a variable that refers to whatever is returned by the call to the database object.
you'd iterate through the result to retrieve all the values (rows) of say a select.

usually that takes the form or
PHP Code:
while ($row=mysql_fetch_array($sqlr))
{  
//do somethign with the row retrieved from the db
    //  eg echo $row['columnname'];

__________________
I'd rather be riding my Versys or my Tiger 800 let alone the Norton
Reply With Quote
  #3 (permalink)  
Old 01-22-12, 20:33
hello-world hello-world is offline
Registered User
 
Join Date: Dec 2011
Posts: 15
Quote:
Originally Posted by healdem View Post
$result is a variable that refers to whatever is returned by the call to the database object.
you'd iterate through the result to retrieve all the values (rows) of say a select.

usually that takes the form or
PHP Code:
while ($row=mysql_fetch_array($sqlr))
{  
//do somethign with the row retrieved from the db
    //  eg echo $row['columnname'];

Yeah that's what I'm wondering: every time you do mysql_fetch_array, you iterate through the array and get the next row, which seems like fewer rows remain in sqlr. So it appears that $sqlr, the variable containing the SQL query results, is itself modified by the fetch command -- I mean, how else can $row get the next row each time? Is that how it works?
Reply With Quote
  #4 (permalink)  
Old 01-23-12, 03:26
healdem healdem is offline
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 9,262
as you are returning an associative array from the SQL query you should be able to reset the array pointer. retrieving the data from the array doesn't remove it, but merely moves an internal pointer

PHP: Array Functions - Manual
__________________
I'd rather be riding my Versys or my Tiger 800 let alone the Norton
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On