Help with using a loop to pull data from mysql recordset

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jim Hernandez

    Help with using a loop to pull data from mysql recordset

    This has set me back a couple of days now and I could really use some
    help or at least a shove in the right direction.

    What I'm trying to do is list reviews in a database on a page, by
    artist (byartist.php). The first step is pulling each artist and
    listing them alphabetically. If the artist has more than one album
    reviewed, they should still only show up once, so I need to check each
    row of the recordset, and check the artistname against a variable or
    something, and if it exists, to just skip it and move on to the next row.
    Then I need to list the albums reviewed under each respective artists
    name and use them as links with url parameters to the respective review
    pages.

    I've got the recordset down per some advice from another newsgroup, and
    I'm fairly certain its correct for what I need to do.
    <?php require_once('c onntest.php'); ?>
    <?php
    mysql_select_db ($database_test , $test);
    $query_byartist = "select artists.artisti d, artists.artistn ame,
    areviews.albumi d, areviews.atitle from artists join areviews on
    artists.artisti d = areviews.artist id";
    $byartist = mysql_query($qu ery_byartist, $test) or die(mysql_error ());
    $row_byartist = mysql_fetch_ass oc($byartist);
    $totalRows_byar tist = mysql_num_rows( $byartist);
    ?>

    The php example I was given for pulling the results was kind of
    half-effort and not enough for me to really see what was going on,
    understand it and build upon it. I'm (obviously) just starting out with
    php/mysql and I'm pretty good at building upon something in front of me
    and figuring it out, just need a little help because I've tried it over
    and over and to no avail, I wind up with syntax errors.
    Sorry for the wordy post, I didnt want to skimp. Thanks for any help or
    advice or any points in the right direction. I've got about 3 days of
    progress to catch up on
  • Peter Fox

    #2
    Re: Help with using a loop to pull data from mysql recordset

    Following on from Jim Hernandez's message. . .[color=blue]
    >This has set me back a couple of days now and I could really use some
    >help or at least a shove in the right direction.[/color]

    [color=blue]
    >php/mysql and I'm pretty good at building upon something in front of me
    >and figuring it out, just need a little help because I've tried it over
    >and over and to no avail, I wind up with syntax errors.[/color]
    The story in your post contradicts the assertion that you are good at
    adapting things. It may be tedious to RTFM, and you may still need to
    experiment and refine, but experimenting and refining will be a great
    deal quicker.


    --
    PETER FOX Not the same since the e-commerce business came to a .
    peterfox@eminen t.demon.co.uk.n ot.this.bit.no. html
    2 Tees Close, Witham, Essex.
    Gravity beer in Essex <http://www.eminent.dem on.co.uk>

    Comment

    • Jerry Stuckle

      #3
      Re: Help with using a loop to pull data from mysql recordset

      Jim Hernandez wrote:[color=blue]
      > This has set me back a couple of days now and I could really use some
      > help or at least a shove in the right direction.
      >
      > What I'm trying to do is list reviews in a database on a page, by
      > artist (byartist.php). The first step is pulling each artist and
      > listing them alphabetically. If the artist has more than one album
      > reviewed, they should still only show up once, so I need to check each
      > row of the recordset, and check the artistname against a variable or
      > something, and if it exists, to just skip it and move on to the next row.
      > Then I need to list the albums reviewed under each respective artists
      > name and use them as links with url parameters to the respective review
      > pages.
      >
      > I've got the recordset down per some advice from another newsgroup, and
      > I'm fairly certain its correct for what I need to do.
      > <?php require_once('c onntest.php'); ?>
      > <?php
      > mysql_select_db ($database_test , $test);
      > $query_byartist = "select artists.artisti d, artists.artistn ame,
      > areviews.albumi d, areviews.atitle from artists join areviews on
      > artists.artisti d = areviews.artist id";
      > $byartist = mysql_query($qu ery_byartist, $test) or die(mysql_error ());
      > $row_byartist = mysql_fetch_ass oc($byartist);
      > $totalRows_byar tist = mysql_num_rows( $byartist);
      > ?>
      >
      > The php example I was given for pulling the results was kind of
      > half-effort and not enough for me to really see what was going on,
      > understand it and build upon it. I'm (obviously) just starting out with
      > php/mysql and I'm pretty good at building upon something in front of me
      > and figuring it out, just need a little help because I've tried it over
      > and over and to no avail, I wind up with syntax errors.
      > Sorry for the wordy post, I didnt want to skimp. Thanks for any help or
      > advice or any points in the right direction. I've got about 3 days of
      > progress to catch up on[/color]


      OK, looks like you're off to a decent start. You're splitting the
      albums and reviews into separate tables, which is good.

      So exactly what have you tried which causes the syntax errors? What is
      the exact message you're getting?

      --
      =============== ===
      Remove the "x" from my email address
      Jerry Stuckle
      JDS Computer Training Corp.
      jstucklex@attgl obal.net
      =============== ===

      Comment

      • Funktopia

        #4
        Re: Help with using a loop to pull data from mysql recordset

        It would be cool if you could post a few more details, especially of
        the errors you're getting. What I gather from your post is that you
        are having difficulties getting data out of the db and you're not
        confident about how your example worked so here's an annotated typical
        sample query

        //first you connect and select the db
        $link = mysql_connect($ mysqlserverip, $mysqlusername, $mysqlpassword)
        or die("Could not connect");
        mysql_select_db ($databasename, $link) or die("Could not select
        ".$databasename );

        //then perform your query (your query looked OK xcept you may need to
        areviews.artist id in the first list)
        $result = mysql_query($qu ery_byartist, $link) or die("Query failed
        ".$query_byarti st);

        //now you've got a result set you need to got thru each result. they
        way this works can seem a little strange at first. each call to
        mysql_fetch_ass oc (or mysql_fetch_arr ay etc.) returns the next result
        if available or false otherwise

        while ($row = mysql_fetch_ass oc($result))
        {
        //now you have an array $row where the each item in the array
        represents a field in the db record. For example, to print out the
        title of the review you would write

        echo $row["atitle"];

        //Note that only the field name is used in the index (as opposed to
        $row["areviews.atitl e"]
        }

        //finally you should free up all the resources used. Obviously none of
        us ever forget this ;-)

        mysql_free_resu lt($result);
        mysql_close($li nk);

        Hope i've interpreted your problem correctly and that this helps you
        work it out.

        Comment

        Working...