SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Wizard samsm's Avatar
    Join Date
    Nov 2001
    Location
    Atlanta, GA, USA
    Posts
    5,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Beginner query problem

    Ok, just when I thought I knew what I was doing...

    The code below was my attempt to get a list of orchestra positions (violin, cello, etc.) with a list of the musicians that play that instrument below it. However, nothing is coming out on the page.

    The database connection is good and there are no PHP or mySQL errors. I was assuming that the problem comes here:

    if ($instrument == $musicianInstrument) {... ...}

    but even when that is commented out, there still isn't any display on the page.

    Any suggestions? I tried to include all the pertinent code below.

    *** Note: I've edited the code here to reflect changes I made after Cupid's post***

    $musiciansresult = mysql_query("select ID, name, email, instrument, position from musicians");
    if (!$musiciansresult) {
    echo("<p>Error performing query:" . mysql_error() . "<p>");
    exit();
    }
    $instrumentsresult = mysql_query("select ID, name, listOrder from instruments");
    if (!$instrumentsresult) {
    echo("<p>Error performing query:" . mysql_error() . "<p>");
    exit();
    }

    while ( $row = mysql_fetch_array($instrumentsresult)) {
    $instrumentID = $row["ID"];
    $instrumentName = $row["name"];
    $instrument = $row["listOrder"];
    echo("<p>" . $instrumentName . " </p>");
    while ($musiciansrow = mysql_fetch_array($musicianresult)) {
    $musicianID = $musiciansrow["ID"];
    $musicianName = $musiciansrow["name"];
    $musicianEmail = $musiciansrow["email"];
    $musicianInstrument = $musiciansrow["instrument"];
    $musicianPosition = $musiciansrow["position"];
    echo($musicianName);
    if ($instrument == $musicianInstrument) {
    echo("<p>" . $MusicianName);
    if ($musicianPosition) {
    echo(" , " . $musicianPosition);
    }
    if ($musicianEmail) {
    echo(" , " . $musicianEmail);
    }
    }
    }
    }
    Last edited by samsm; Nov 4, 2001 at 14:37.

  2. #2
    epsilon transition cupid's Avatar
    Join Date
    Aug 2001
    Location
    Kent, Ohio
    Posts
    367
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well I ran into a problem when I tried to run this.
    First of all, there was a parse error. You need a closing brace after

    if ($instrument == $musicianInstrument) {
    echo("<p>" . $MusicianName);

    You mentioned that commenting this line out did no good. I'd try find a mysql error then.

    Do something like this:

    $instrumentsresult = mysql_query("select ID, name, listOrder from instruments") or die(mysq_error());

    $musiciansresult = mysql_query("select ID, name, email, instrument, position from musicians") or die(mysq_error());

    And to answer your question, yes, you can avoid those else{} statements. They're not necessary.

  3. #3
    SitePoint Wizard samsm's Avatar
    Join Date
    Nov 2001
    Location
    Atlanta, GA, USA
    Posts
    5,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    cleaning up code...

    Thanks for the note and sorry about the missing bracket... a cut and paste error rather than coding. I am copying the code to the bottom of this post... hopefully I'll get it right this time.

    I will add that the page loads with no PHP errors and there is data in each referenced column.

    Also I have one of these just after each query (I didn't post it the first time to make the post shorter -- a mistake, I now realize):

    if (!$instrumentsresult) {
    echo("<p>Error performing query:" . mysql_error() . "<p>");
    exit();
    }

    Is "or die(mysq_error());" an abridged version of the same thing? Regardless, I tried it and no error.

    Here's the code which I've made a little more complete by fixing some stuff:

    $musiciansresult = mysql_query("select ID, name, email, instrument, position from musicians");
    if (!$musiciansresult) {
    echo("<p>Error performing query:" . mysql_error() . "<p>");
    exit();
    }
    $instrumentsresult = mysql_query("select ID, name, listOrder from instruments");
    if (!$instrumentsresult) {
    echo("<p>Error performing query:" . mysql_error() . "<p>");
    exit();
    }

    while ( $row = mysql_fetch_array($instrumentsresult)) {
    $instrumentID = $row["ID"];
    $instrumentName = $row["name"];
    $instrument = $row["listOrder"];
    echo("<p>" . $instrumentName . " </p>");
    while ($musiciansrow = mysql_fetch_array($musicianresult)) {
    $musicianID = $musiciansrow["ID"];
    $musicianName = $musiciansrow["name"];
    $musicianEmail = $musiciansrow["email"];
    $musicianInstrument = $musiciansrow["instrument"];
    $musicianPosition = $musiciansrow["position"];
    echo($musicianName);
    if ($instrument == $musicianInstrument) {
    echo("<p>" . $MusicianName);
    if ($musicianPosition) {
    echo(" , " . $musicianPosition);
    }
    if ($musicianEmail) {
    echo(" , " . $musicianEmail);
    }
    }
    }
    }

    I'm really just trying to make sure that I haven't done anything dumb before I continue using brute force experimentation. Thanks for looking!

    -Sam

  4. #4
    epsilon transition cupid's Avatar
    Join Date
    Aug 2001
    Location
    Kent, Ohio
    Posts
    367
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, you can say that "or die()" is analogous to what you've written.

    As far as I can tell, I don't see anything obvious that would lead to this. Are you positive that mysql_connect() is working? Also, did you select a database with mysql_select()?

  5. #5
    SitePoint Wizard samsm's Avatar
    Join Date
    Nov 2001
    Location
    Atlanta, GA, USA
    Posts
    5,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Think I have it straight now

    I issolated that section of code into it's own page and the first part of it worked and I got a mySQL error for the second part.

    That's when I realized that right in the middle of the code, "musiciansresult" becomes "musicianresult" (I suppose I was subconciously thinking that way since I was only getting results for one musician at a time... )

    Thank you for your help! I learned a couple things and will use "or die()" in the future (just as effective and less messy than "if (!$variable)...")

    -Sam
    Last edited by samsm; Nov 6, 2001 at 09:03.


Bookmarks

Posting Permissions

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