Jump to content

Creating An Array From Amysql Table


niche

Recommended Posts

I have a mysql table called example. It has 3 records and 5 fields. The field names are: id, firstname, lastname, address, and zip.I need to select all the records and place all the info from all the records into an array called myarray.How do I do that?

Link to comment
Share on other sites

Here's the short answer:$result = mysql_query($sql);while ($rows[] = mysql_fetch_assoc($result)) {// do something if you like}You can now access things like this:echo $rows[0]['my_field'];You'll want to add some error handling, though.

Link to comment
Share on other sites

Based on the last thread, I scripted this and got a blank screen:

$query = "SELECT * FROM example"; 	 $result = mysql_query($query);while($row = mysql_fetch_array($result)) {}$ray = is_array($row);echo $ray;

Shouldn't this script reurn a "1"Thanks.

Link to comment
Share on other sites

Edit: The Flash got there first..

Link to comment
Share on other sites

So, is it correct to think that the array $row only exists inside the while loop?If so, why did print r() work? Also, please explain the [] in $row[].Thanks-you.

Link to comment
Share on other sites

So, is it correct to think that the array $row only exists inside the while loop?
It's more correct to say that the $row variable gets overwritten each time through the loop, if the array that $row pointed to wasn't copied anywhere else then the array will be overwritten also. In that example we're saving the array that $row points to in another variable so that it still exists after the loop ends.
If so, why did print r() work?
print_r is printing the $rows array after the loop ends, inside the loop it just adds each $row array to the larger $rows array.Doing this:$rows[] = $row;is a shortcut for this:$rows[count($rows)] = $row;or this:$rows = array_push($rows, $row);In other words, it adds a new element to the end of the array.
Link to comment
Share on other sites

I simplified my table and produced an array called $rows with your help. Here's its dump:Array ( [0] => Array ( [0] => Timmy [name] => Timmy [1] => 23 [age] => 23 ) [1] => Array ( [0] => Renae [name] => Renae [1] => 52 [age] => 52 ) [2] => Array ( [0] => Jim [name] => Jim [1] => 51 [age] => 51 ) )I need to create another while loop that reads the elements of the first two fields into two variables so the info can be checked. Then, I want to re-use those two variables and redefine them with the elements from the next two fields and so on (let me know if I'm not using the terms fields and elements correctly).I'm searching the list of array functions and haven't recognized the functions that I think I need.Please help me by showing me how you would script the situation I described.Thank-you.

Link to comment
Share on other sites

Thank-you very much to Deirdre's Dad, justsomeguy, and chibineku. You've change the way I think and have helped me very much.Until Later,Niche

Link to comment
Share on other sites

Yeah, this was misleading:

while ($rows[] = mysql_fetch_assoc($result)) {
The last member of $rows will always be Boolean false. That could be a problem if you're not expecting it. Same problem here in niche's code:
while($row = mysql_fetch_array($result)) {}$ray = is_array($row);
Nothing wrong with the loop, but the final assignment to $row is Boolean false, so is_array($row) returns false also.Out of curiosity, is there a reason you can't generate your HTML while you loop through your table rows the first time? I mean, is it really necessary to build this big old object? One possible answer is building the array keeps the code from being too dense to read. Another is that the data needs extensive processing before it can be output.I ask because usually I can generate HTML (for immediate output or as a string for later printing) inside the fetch loop itself. Fewer steps, less code.EDIT: Oh. We're done. One for the archives, then. :)
Link to comment
Share on other sites

No reason. It's just that earlier in the day I wasn't thinking that way. Am now! Also, I needed to find out how to assign local variables from an array. Thanks to you and others, I now know how. I'm scripting better this evening than I was this morning.Thanks Again,Niche

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...