开发者

(php) mysql_fetch_array - How does it know which row to access

I'm learning currently php/mysql and I'm confused about this bit.

After some heads scratching I have figured out that mysql_fetch_array remembers which row it last accessed and accesses the next one. (I was originally trying to work out how the c开发者_如何学Goode was communicating this to it in example code)

so for database:

parent | job
-------+-------------
mom    | receptionist
-------+-------------
dad    | taxi driver

the code

mysql_fetch_array($result)[job] 

returns 'receptionist' the first time and 'taxi driver' the second.

  • Where/how is it keeping track of this?
  • What happens if I don't want to access them in order?

thanks


  • internal implementation in PHP. Don't try to figure it out ;)
  • if you want a different order, then specify it in your database query.


Where/how is it keeping track of this?

The mySQL server has an internal result pointer. In some databases / wrappers / libraries you can rewind that pointer, but as far as I know, this is not possible in the mysql_* library of functions.

What happens if I don't want to access them in order?

You have to access them in some order. The only alternative to that is to fetch all rows into an array: Then you can access each row randomly.

If you want to change the order of records, do that in the query using the ORDER clause.

Some database wrappers like PDO have a fetchAll() method that does exactly that. For large result sets, this can be memory intensive and break the script's memory limit, which is why it's usually not done this way.


There is another way to attack this question.

If you want to know how YOU TOO can make functions that do what this one does. Here is how:

<?php
function count_off()
{
static $count = 1;
echo $count++;
}

count_off();
count_off();
count_off();
count_off();
count_off();
?>

the above will output 12345

I should mention. You shouldn't do this without a very good reason. It is SUPER hard to trace when debugging.


If you want to access them in a different order, use an ORDER BY clause in your SQL to change the order that the results are retrieved from the database.


The result of mysql_fetch_array is always the next result/row of the query (first the first row off course) Intern it will keep a pointer how for it has fetched.

If you want to get them in an alternate order, you have to define it in the query. Like said the result will always be in the order specified by the query (implicit or explicit)


If you wrote typical looking code like this:

$result = mysql_query("SELECT parent, job FROM table");

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    echo $row['parent'] . ' - ' . $row['job'];
}

Each time mysql_fetch_array() is called, if there is another row in the result, it will return the row as an associative array into the $row variable. Otherwise, it will return false, and the execution of the while loop will end.

Also, because you didn't specify an ORDER BY clause, it defaults to returning rows in the order they were inserted into the table.


The mysql_fetch_array() function grabs one row from the database, in the order that MySQL returns it from the query you gave.

To obtain all the rows, you can put the function in a loop.

while($row = mysql_fetch_array($result)) {
    echo $row["job"];
}

This will output:

receptionist
taxi driver

You can change the order by using the sql term order by, which can alphabetically or numerically order your results by a certain column

select * from parent order by job

The above query will order the results alphabetically by the parent job field (results closer to A will come first in the mysql_fetch_*

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜