[nycphp-talk] Printing from arrays.
FB`
fb at intldef.org
Wed Sep 11 14:00:38 EDT 2002
i am not sure if this is the *right* way to do it, but off the cuff, this
should work:
$query= "SELECT
images.id as images_id,
format.id as format_id,
format.name as format_name
FROM images, format
WHERE images.id=$id";
while ($row=mysql_fetch_array($result)){
$images_id = $row["images_id"];
$format_id = $row["format_id"];
$format_name = $row["format_name"];
echo "$images_id, $format_id, $format_name";
echo "<br>";
}
i added the "a.b as a_b" bits because this is what i generally do, and while
referring back to $row["a.b"] could work, if your select has just "a.b", i'm
not sure it would work in exactly that format...
HTH
FB`
Filips Baumanis
----- Original Message -----
From: "Matthew Zimmerman" <mz34 at nyu.edu>
To: "NYPHP Talk" <talk at nyphp.org>
Sent: Wednesday, September 11, 2002 1:27 PM
Subject: [nycphp-talk] Printing from arrays.
> Hello,
>
> Sorry if this is a RTFM question, but this list has been such a great
> resource I wanted to start here.
>
> Just a question about printing from an array using PHP/MySQL
>
> My real database and code is a little more complex then this, but to
> illustrate my problem let me say
>
> 1. I have two tables: "images" and "format". They each have two fields
> "id" and "name".
>
> 2. I have a query that says
>
> $query= "SELECT images.id, format.id, format.name
> FROM images, format
> WHERE images.id=$id"; /* $id is a passed from a from*/
>
> 3. Then I assign the results to an array using
>
> $result= mysql_query($query);
>
> while ($row=mysql_fetch_array($result)
>
> {
> extract($row);
>
> }
>
> 4. Then I want to print the results which I would think would go like
> this:
>
> echo "$images.id, $format.id, $format.name";
>
> But it seems these are not the keys in the array, but instead there is
> just one $id key and the value of that is whatever "id" came last in
> the query. In other words, if the query read "Select format.id,
> images.id" then there would be a value in the array for $id equal to
> "images.id" and if query read "Select images.id, format.id" then there
> would be a value in the array for the key $id equal to "format_id".
>
> I expected there would be two keys in the array: images.id and format.id
>
> Am I wrong to expect that?
>
> Thanks for any help. I am new to PHP and databases and this code I am
> using I got from a book, so maybe it is the wrong technique.
>
>
> Matt Zimmerman
> NYU
>
>
>
> --- Unsubscribe at http://nyphp.org/list ---
>
>
>
More information about the talk
mailing list