Jump to content

[SOLVED] How to read mysql off as an array?


ted_chou12

Recommended Posts

Do you mean row as an array, or all as a multidimensional, hmmm me thinks not... (i'd like it too!)

mysql_fetch_array — Fetch a result row as an associative array, a numeric array, or both

mysql_fetch_assoc — Fetch a result row as an associative array

mysql_fetch_object — Fetch a result row as an object

mysql_fetch_row — Get a result row as an enumerated array

http://uk3.php.net/manual/en/ref.mysql.php

Sorry, I wasnt clear, I want to read off each row as an array, and the elements of each row are elements of the array, therefore, a mutidimensional array:

so:

$read = mysql_query("SELECT * FROM forum WHERE threadid='$threadid' ORDER BY id");

I want to echo out elements in the 15th row, how should I do that?

Something like this, probably.

 

<?php

$sql = "SELECT * FROM table";
$result = mysql_query($sql);

$table = Array();

while($row = mysql_fetch_assoc($result))
{
  $table[] = $row;
}

print_r($table[4]); // displays the fourth (or fifth?) row of the table

?>

 

I would suggest against this, though.  You're going to waste a lot of time and memory loading all the sql results into an array.  Just grab them as you need them.

Not that I know of.

 

If you want to get at a specific row of a query, there are functions to change which row the resource points to.  You'd really be much better off using that.  The code I've posted does what you want, but will probably bog down your script.

I can't use the row function because I am not pointing to a specific row, however, I am ordering them first and then find the row number that I want in relation to the other rows, so for eg. the row i want is 15th, the id for that row may not necessary be 15, so the row function wouldnt work.

Thanks.

You want this function.

 

http://www.php.net/manual/en/function.mysql-data-seek.php

 

It doesn't move to a row based on ID.  ID is a field in your table.  This function moves to the nth row of a query and is independent of the value of any field in your table.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.