...

View Full Version : Referencing table fields



WillowFae
10-15-2003, 02:51 PM
Background : When I reference table fields in php I use $myrows[1] etc. An example of my code is :

printf("<a href='view.php?id=%s&gallery=%s'>%s</a>",$myrow[0],$myrow[8],$myrow[3]);


Specific problem : I am using a sql statement that has an inner join in it. What I cannot seem to do is reference the 2 additional fields from the joined table using the above system. I ran the query in phpAdmin and it produced a table with 13 columns but $myrows[12] is returning nothing.

How can I refer to the extra field that I need? When I do this in other languages I refer to all fields by their actual name. Now when I started doing php the only way I could find to do any of this is using the above method. Is there another way that DOES refer to field names?

Hope that is enough info. I posted it in here as it is a php syntax issue not sql

raf
10-15-2003, 03:03 PM
to get an associative array, use mysql_fetch_assoc ()
http://be.php.net/manual/en/function.mysql-fetch-assoc.php
You can then refer to the variables in the recordset like $myrow["variablename"]

I didn't quite understand the rest of your post + an indexed array is zero-based so if the recordset contains 13column, then $myrow[12] should return that last column. Strange.

WillowFae
10-15-2003, 03:23 PM
Thanks. I wasn't aware of that method.

However I'm getting a parse error. My code is

while ($myrow = mysql_fetch_assoc($resultGetTopPics))
{
echo "<td valign='top'>";
echo "<a href='view.php?id=$myrow['galleryID']&gallery=$myrow['galleryMainCatID']'>$myrow['imageCaption']</a>";
echo "</td>";
}

I'm getting the following error on the second echo line.

Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or `T_NUM_STRING'

Any thoughts?

pb&j
10-15-2003, 03:26 PM
you have single quotes inside of single quotes which is confusing the code, thus the error. try escaping the inside single quotes.

WillowFae
10-15-2003, 03:34 PM
Oops!

Just to check - it is a \ to escape, isn't it? Because that isn't making any difference :(

WillowFae
10-15-2003, 03:43 PM
Ah, found the problem with that. If you are not using {} after echo then you don't need the field names in quotes.

However, refering to the fields by their actual names, is still not returning anything for the exta field that is added by using the inner join statement

while ($myrow = mysql_fetch_assoc($resultGetTopPics))
{
echo "<tr>";
echo "<td valign=\"top\">";
echo "<a href=\"view.php?id=$myrow[galleryID]&gallery=$myrow[galleryMainCatID]\">$myrow[imageCaption]</a>";
echo "</td>";
echo "<td valign=\"top\">";
echo "$myrow[galleryMainCatDescription]";
echo "</td>";
echo "<td valign=\"top\">";
echo "$myrow[views] views";
echo "</td>";
echo "</tr>";
}

The only field name in this that belongs to the second table is galleryMainCatDescription and this is the only one coming up blank.

raf
10-15-2003, 03:45 PM
Try


while ($myrow = mysql_fetch_assoc($resultGetTopPics)) {
echo ("<td valign=\"top\">") ;
echo ("<a href=\"view.php?id=" . $myrow['galleryID'] . "&gallery=" . $myrow['galleryMainCatID'] . "\">" . $myrow['imageCaption'] . "</a>") ;
echo ("</td>") ;
}

raf
10-15-2003, 03:49 PM
Posts crossed.

Can you post the sql statement? Does this 13 field has the same name as one of the others (from the other table) ?

WillowFae
10-15-2003, 03:51 PM
Ah - it is now working (but without making those changes you suggested).

Thanks ever so much guys. I am pleased to have learnt the array way of refering to the field names - much less confusing than the other way, what with trying to remember which %s refers to which $myrow[] :)

Think I'm going to go through the rest of my code and change them to this new way!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum