Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > PHP Arrays

PHP Arrays
Thread Tools
selowitch
Mac Elite
Join Date: Nov 2003
Location: Rockville, MD
Status: Offline
Reply With Quote
May 11, 2006, 09:56 AM
 
I would like to take the results of the MySQL query and insert the entire thing into a multi-dimensional array. The purpose of this is so I can work on it like a temporary local version of my database without making more than one call to the database.

I imagine this could be done using the foreach() construct. Does anybody have an appropriate code sample or a suggestion for how to go about this?
     
genevish
Mac Enthusiast
Join Date: Jan 1999
Location: Marietta, GA, USA
Status: Offline
Reply With Quote
May 11, 2006, 10:14 AM
 
There are a number of articles I found relating to this. This one seems to be fairly clear. It shows a one dimensional array, but the idea should be the same for a multidimensional array.
Scott Genevish
scott AT genevish DOT org
     
registered_user
Dedicated MacNNer
Join Date: Nov 2001
Location: Are Eye
Status: Offline
Reply With Quote
May 11, 2006, 07:07 PM
 
$array = array();

while($row = mysql_fetch_assoc($result_from_query)) {
$array[] = $row;
}
     
selowitch  (op)
Mac Elite
Join Date: Nov 2003
Location: Rockville, MD
Status: Offline
Reply With Quote
May 11, 2006, 08:16 PM
 
Originally Posted by registered_user
$array = array();

while($row = mysql_fetch_assoc($result_from_query)) {
$array[] = $row;
}
This produces the following error:
Code:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource
     
SirCastor
Professional Poster
Join Date: Jan 2001
Location: Salt Lake City, UT USA
Status: Offline
Reply With Quote
May 11, 2006, 08:27 PM
 
What's your MySQL query?
2008 iMac 3.06 Ghz, 2GB Memory, GeForce 8800, 500GB HD, SuperDrive
8gb iPhone on Tmobile
     
registered_user
Dedicated MacNNer
Join Date: Nov 2001
Location: Are Eye
Status: Offline
Reply With Quote
May 11, 2006, 08:34 PM
 
You need to query the database first. Like so:

$result_from_query = mysql_query($sql, $database_connection);

where $sql is your query, and $database_connection is the connection to the database.

php.net has a good manual describing all the various mysql functions, including mysql_query():
http://us3.php.net/manual/en/function.mysql-query.php
     
selowitch  (op)
Mac Elite
Join Date: Nov 2003
Location: Rockville, MD
Status: Offline
Reply With Quote
May 11, 2006, 08:43 PM
 
Good suggestions. Yes, I'm aware that I need to do a query first, it just wasn't in the snippet I posted, and yes, I refer to php.net quite frequently:

Code:
<?php mysql_connect("localhost","barridoff","********"); @mysql_select_db("barridoff") or die( "Unable to select database"); $result = mysql_query("SELECT artists.first_name, artists.last_name, artists.nationality, artists.lifespan, items.artist_id, items.item_id, items.leading_article, items.title, items.medium, items.dimensions, items.lot_number, items.low_estimate,items.high_estimate, items.notes, items.ebay_id, items.hammer_price, items.sold_after FROM items, artists WHERE items.artist_id = artists.artist_id AND items.hidden = 'no' AND items.item_id = $id"); $array = array(); while($row = mysql_fetch_array($result)) { $array[] = $row; } ?>
Actually, now that I look at it, maybe it should be:
Code:
<?php mysql_connect("localhost","barridoff","********"); @mysql_select_db("barridoff") or die( "Unable to select database"); $sql = "SELECT artists.first_name, artists.last_name, artists.nationality, artists.lifespan, items.artist_id, items.item_id, items.leading_article, items.title, items.medium, items.dimensions, items.lot_number, items.low_estimate,items.high_estimate, items.notes, items.ebay_id, items.hammer_price, items.sold_after FROM items, artists WHERE items.artist_id = artists.artist_id AND items.hidden = 'no' AND items.item_id = $id"; $array = array(); while($row = mysql_fetch_array($sql)) { $array[] = $row; } ?>
instead. Or perhaps what is being proposed is PHP 5 syntax, and I'm using PHP 4 with this particular server?
( Last edited by selowitch; May 11, 2006 at 08:51 PM. )
     
registered_user
Dedicated MacNNer
Join Date: Nov 2001
Location: Are Eye
Status: Offline
Reply With Quote
May 11, 2006, 10:16 PM
 
Your syntax looks right to me in the first one.

The error suggests that mysql_fetch_array() isn't receiving a proper argument, so I'm inclined to think that there is a problem with the query. after all, it says that $result is not a valid result resource.

you can echo mysql_error() after you run the query to see if there was a problem, and you can print_r($result); to see if it's a resource. (though that's not terribly informative, it'll at least say if it's a resource or not)
     
selowitch  (op)
Mac Elite
Join Date: Nov 2003
Location: Rockville, MD
Status: Offline
Reply With Quote
May 11, 2006, 10:43 PM
 
Well, this revision of code does not produce an error:
Code:
<?php mysql_connect("localhost","barridoff","********"); @mysql_select_db("barridoff") or die( "Unable to select database"); $result = mysql_query("SELECT artists.first_name, artists.last_name, artists.nationality, artists.lifespan, items.artist_id, items.item_id, items.leading_article, items.title, items.medium, items.dimensions, items.lot_number, items.low_estimate,items.high_estimate, items.notes, items.ebay_id, items.hammer_price, items.sold_after FROM items, artists WHERE items.artist_id = artists.artist_id AND items.hidden = 'no'"); $array = array(); while($row = mysql_fetch_array($result)) { $array[] = $row; } ?>
So now it seems I need to print the contents of the array to prove it's really there.
     
registered_user
Dedicated MacNNer
Join Date: Nov 2001
Location: Are Eye
Status: Offline
Reply With Quote
May 12, 2006, 06:39 AM
 
I would switch mysql_fetch_array() with mysql_fetch_assoc() if I were doing it. Because I'd rather reference $array[0]['first_name'] than $array[0][0]. But that's personal preference.

print_r($array); is the easiest way I know to check if an array is really there.
     
selowitch  (op)
Mac Elite
Join Date: Nov 2003
Location: Rockville, MD
Status: Offline
Reply With Quote
May 12, 2006, 05:04 PM
 
Originally Posted by registered_user
I would switch mysql_fetch_array() with mysql_fetch_assoc() if I were doing it. Because I'd rather reference $array[0]['first_name'] than $array[0][0]. But that's personal preference.

print_r($array); is the easiest way I know to check if an array is really there.
So what would the complete code be using your technique including print_r() and $array[0]['first_name']?
     
registered_user
Dedicated MacNNer
Join Date: Nov 2001
Location: Are Eye
Status: Offline
Reply With Quote
May 12, 2006, 07:47 PM
 
<?php

mysql_connect("localhost","barridoff","********");
@mysql_select_db("barridoff") or die( "Unable to select database");

$sql = "SELECT artists.first_name ... AND items.hidden = 'no'";
$result = mysql_query($sql);

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

print_r($array);
?>

NOTE: I did truncate your query to save space.
     
SirCastor
Professional Poster
Join Date: Jan 2001
Location: Salt Lake City, UT USA
Status: Offline
Reply With Quote
May 13, 2006, 12:52 PM
 
I recommend you make all your Mysql functions report errors automatically.

if(!mysql_select_db("barridoff")) print mysql_error();

that way you always know if something is failing.
2008 iMac 3.06 Ghz, 2GB Memory, GeForce 8800, 500GB HD, SuperDrive
8gb iPhone on Tmobile
     
   
 
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Top
Privacy Policy
All times are GMT -4. The time now is 07:35 PM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,