[thelist] PHP arrays

Andrew Maynes andrew at humanbehaviour.co.uk
Thu Jan 23 08:17:01 CST 2003

I bet you didn't have to read this 4 times!  Spot on this is exactly what I am
trying to do... Am I aout of touch or do I need to buy a new MySQL book!  It
states that FOREIGN KEY  operation is currently not implemented?

I am working on your solution now :)

<SNIP>I assume you're saying that you are getting the category ID foreign key
instead of the category name, correct? If that's the case, you are going to
have to use a join.

But before we get to that, let's address a few other issues.

1. Unless you have mysql() defined as a function, it's not the usual method
for performing a query. That would be mysql_query().

2. As many people have pointed out countless times, you should always
specify your field names in your SELECT statement, and avoid SELECT * at
all costs.

3. Try to use more descriptive names for your variables, because when you
come back to your code in six months, you'll never figure out what you mean
by $IS, $IN, $IC, or $II.

4. Use mysql_fetch_array() instead of mysql_fetch_row(). It's much more
useful and is just as fast.

5. Since you're only getting one row back, you don't a while loop.

OK, let's go back to the join. You'll want something like:

SELECT     i.ItemID, i.CategoryID [whatever your field names are],
FROM       Items i
INNER JOIN Categories c
         ON i.CategoryID = c.CategoryID
WHERE      i.ItemID = '$II'

Then just extract your data:

$row = mysql_fetch_row($result);
$ItemID = $row["ItemID"];
$CategoryID = $row["CategoryID"];
$CategoryName = $row["CategoryName"];

Or you could do it in one line:

list($ItemID, $CategoryID, $CategoryName) = mysql_fetch_array($result);

HTH. </SNIP> thank you

More information about the thelist mailing list