ted_chou12 Posted September 21, 2007 Share Posted September 21, 2007 Hello, how to read mysql off as an array? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/70196-solved-how-to-read-mysql-off-as-an-array/ Share on other sites More sharing options...
rarebit Posted September 21, 2007 Share Posted September 21, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/70196-solved-how-to-read-mysql-off-as-an-array/#findComment-352572 Share on other sites More sharing options...
ted_chou12 Posted September 21, 2007 Author Share Posted September 21, 2007 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? Quote Link to comment https://forums.phpfreaks.com/topic/70196-solved-how-to-read-mysql-off-as-an-array/#findComment-352579 Share on other sites More sharing options...
MmmVomit Posted September 21, 2007 Share Posted September 21, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/70196-solved-how-to-read-mysql-off-as-an-array/#findComment-352586 Share on other sites More sharing options...
ted_chou12 Posted September 21, 2007 Author Share Posted September 21, 2007 thanks for your help, is there no function other than putting the array together manually? Quote Link to comment https://forums.phpfreaks.com/topic/70196-solved-how-to-read-mysql-off-as-an-array/#findComment-352589 Share on other sites More sharing options...
MmmVomit Posted September 21, 2007 Share Posted September 21, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/70196-solved-how-to-read-mysql-off-as-an-array/#findComment-352593 Share on other sites More sharing options...
ted_chou12 Posted September 21, 2007 Author Share Posted September 21, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/70196-solved-how-to-read-mysql-off-as-an-array/#findComment-352597 Share on other sites More sharing options...
MmmVomit Posted September 21, 2007 Share Posted September 21, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/70196-solved-how-to-read-mysql-off-as-an-array/#findComment-352602 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.