Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

result set, result pointer

Status
Not open for further replies.

rs51

Technical User
Oct 13, 2001
163
PT
i've no programming background whatsoever, but i like php+mysql, though i'm very newbie at both.
(i must confess i have a cms but was mostly copy&paste)
i learned how to connect to mysql from php and then wondered how to list the databases i've at mysql.
in the manual i found this:

[tt]<?php
error_reporting(E_ALL);
$db_list = mysql_list_dbs();
$i = 0;
$cnt = mysql_num_rows($db_list);
while ($i < $cnt) {
echo mysql_db_name($db_list, $i) . &quot;\n&quot;;
$i++;
}
?>[/tt]

of course this worked, but i want to understand what i'm doing, so:
there i read this:
&quot;mysql_list_dbs() will return a result pointer containing the databases available from the current mysql daemon&quot;
(i guess daemon is my mysql)
As i didnt understand what a pointer is, i googled and found this:
&quot;A pointer is a variable that holds a memory address&quot;
hmm... so now i know that that function returns a kind of a group of variables...
hmm...is this a kind of 'the' RECORDSET used in vb?
well... i made:

[tt]$cnt = mysql_num_rows($db_list);[/tt]

and got how many databases i had.
Next i read this (in the manual):
&quot;The row parameter is an index into the result set&quot;
Now i'm getting more and more confused!
What's the diference between result set and result pointer?
i really feel very confused!
dont get that diference and dont get this last piece of sentence:
&quot;...is an index into the result set&quot;
Can someone help me please?
thanks in advance

 
look at it a different way, sometimes odd angles help :)
Code:
// get an array of database names from the local mysql service
$db_list = mysql_list_dbs();

// while you have an array, assign each item or sub array to a pointer ($myrow)       
while ($myrow = mysql_fetch_array($db_list)) {

// print out the value of the first element of $myrow ([0])          
 echo &quot;$myrow[0]<br>&quot;;

}





______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
PHP's manual is a little unclear on nomenclature sometimes. This is particularly so in the database sections.

In general, a pointer points to something. In the case of PHP, when you use the fopen() function to open a file, what is returned is a &quot;handle&quot; to the file.

&quot;Handle&quot; is a good metaphor. It's like the handle on a suitcase. The clothes in the suitcase is the contents of a file. The suitcase itself is the file on your file system. The handle is the thing you use to manipulate the suitcase -- or the file.

The same is true of database access in PHP. mysql_query() returns a handle to a result set. It's not the actual data, just a way to manipulate the data. It requires special functions, like mysql_fetch_row() to get at the data.



The result set itself contains a data pointer. That pointer simply points to the &quot;current&quot; row of the data returned, so that if you read a row from the result, you know where to read from each time you access the data in the result set.


So, when you query a database to fetch data, the database server returns a result set. PHP holds on to that result set for you and maintains a result pointer, which keeps track of the last place you read from in the data. PHP then gives to your code a result handle, which you use to manipulate the data.

Some simple code with comments:

Code:
<?php
$DB_conn_handle = mysql_connect('localhost', 'test', 'test');
//$DB_conn_handle now contains a database connection
//handle.  Connection handles are different from result
//handles.

mysql_select_db ('test', $DB_conn_handle);

$query = &quot;SELECT foo FROM bar&quot;;

$result_handle = mysql_query($query, $DB_conn_handle);
//mysql_query() has passed my query to the server, and
//fetched the result set.  It returns a result handle,
//which I am storing in $result_handle

while ($the_row_of_data = mysql_fetch_assoc($result_handle))
{
   print $the_row_of_data['foo'];
   print '<br>';
}
//in each iteration of the loop, mysql_fetch_assoc() gets the
//current row of data from the result set.  It uses the result pointer,
//which is initialized to point to the first row of the result set.
//Each time mysql_fetch_assoc() fetches a row from the result set, the
//result set pointer is moved to the next row.  When there is
//nothing more to fetch, mysql_fetch_assoc() returns FALSE.
?>

Want the best answers? Ask the best questions: TANSTAAFL!!
 
for sure this way looks nice and clear!
let me see if i got it right (i'll transpose your code to a case where i want to obtain values from a select statment):

$sql = 'SELECT * FROM pai';

$result = mysql_query($sql, $cnx);
// i got an array that contains so to speak
// the 'recordset'/result set of my query

// now i'm gonna use a pointer (cursor?) called $row
// i guess each 'item'/piece of the array is gonna
// be put into my pointer

while ($row = mysql_fetch_array($result)) {
echo ('<p>'. $row['nome'].' '.$row['apelido'].'</p>');
}
// the loop prints every element of the pointer array

sorry about my ignorance, but i dont get what you mean when you say:
&quot;// print out the value of the first element of $myrow ([0])&quot;

i guess the loop prints ALL, not just the $row([0]) but all $row(), beeing i=0 till i='eof', or = number of array members.
i say this because i tested exactly your code and prints all databases - and that's what we want :)
 
sleipnir214:
what i wrote above was made before i had a chance to see your post
Thanks for your explanation.
I read it very carefully and fully apreciated it. Thanks a lot!
i just have a doubt:
at some point you say:
&quot;...The result set itself contains a data pointer...&quot;
after, you say:
&quot;...PHP holds on to that result set for you and maintains a result pointer,...&quot;
you mean data pointer the same as result pointer?
i guess yes
thanks a lot
 
Yes.

Sorry about that. I was trying to keep my nomenclature standardized. I tend to use &quot;pointer&quot; as my default term -- my &quot;organic&quot; programming language is c-language, where you do lots of things using explicit pointers.

But without trying to muddy the waters, everything we've been discussing up to now are all pointers: the result handles, the result pointers, the connection handles. It's just metaphorically useful to discuss them as very differeent things.

What you can do with a pointer is decided by what that pointer points to. PHP keeps pretty good track of what a pointer points to, and will keep straight what can be done with each type. Other languages, like c-language, will let you do all kinds of squirrely things with pointers.

But to keep all the metaphors straight:
[ul][li]mysql_connect() returns a connection handle.[/li][li]mysql_query() can (with a SELECT query) return a result handle.[/li][li]mysql_fetch_[row|array|assoc|object]() will return a row of the result set as a numeric array, a combination numeric and associative array, an associative array, or an object, respectively.[/li][li]PHP (or more correctly the MySQL communications libraries) maintains a result pointer that keeps track of the &quot;current&quot; row of a result set.[li][/ul]


Want the best answers? Ask the best questions: TANSTAAFL!!
 
that's perfect, thank you!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top