[thelist] PHP arrays

Howard Cheng howcheng at ix.netcom.com
Wed Jan 22 18:10:01 CST 2003


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],
            c.CategoryName
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.

At 11:09 PM 1/22/2003 +0000, Andrew Maynes wrote:
>$result=mysql("$DBName","SELECT * FROM Items WHERE ItemID='$II'");
>fontFace("Arial","Select an item:<br><br>");
>echo "<ul>";
>while ($row  =  mysql_fetch_row($result)) {
>$IS=$row[0];
>$IN=$row[1];
>$ID=$row[2];
>$IC=$row[3];
>$Ca=$row[4];
>$SC=$row[5];
>$II=$row[6];
>}
>
>and I want to present $Ca=$row[4]; as the page title but I dont want the
>number
>1 appearing!  The $Ca=$row[4]; is also Category in another table
>(Category) but
>$Ca=$row[4]; s displaying the number not the description for the chosen
>$Ca=$row[4];
>
>Presumably I will have to write an array?  Is there anything I should
>watch out
>for that may cause problems with this?  As I am also going to (hopefully)
>writing a for loop to pull all the records for the chosen category and not
>just
>one as this query is doing?

::::::::::::::::::::::::::::::::::
Howard Cheng
http://www.howcheng.com/
howcheng at ix dot netcom dot com
AIM: bennyphoebe
ICQ: 47319315




More information about the thelist mailing list