Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New Coder
    Join Date
    Nov 2002
    Posts
    45
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Referencing table fields

    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
    Last edited by WillowFae; 10-15-2003 at 02:59 PM.

  • #2
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    to get an associative array, use mysql_fetch_assoc ()
    http://be.php.net/manual/en/function...etch-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.

  • #3
    New Coder
    Join Date
    Nov 2002
    Posts
    45
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?

  • #4
    Regular Coder
    Join Date
    Jun 2002
    Location
    Canada
    Posts
    253
    Thanks
    0
    Thanked 0 Times in 0 Posts
    you have single quotes inside of single quotes which is confusing the code, thus the error. try escaping the inside single quotes.

  • #5
    New Coder
    Join Date
    Nov 2002
    Posts
    45
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Oops!

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

  • #6
    New Coder
    Join Date
    Nov 2002
    Posts
    45
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #7
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Try
    PHP Code:
    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>") ;


  • #8
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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) ?

  • #9
    New Coder
    Join Date
    Nov 2002
    Posts
    45
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •