SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Jul 2001
    Location
    cheshire
    Posts
    83
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Why can't I print the result set like this?

    $Query = "SELECT MAX(score) AS maxScore, MIN(score) AS minScore, AVG(score) AS averageScore FROM $TableName";
    $Result =mysql_query ($Query);
    $Row = mysql_fetch_array($Result);
    for ($n = 0; $n < count($Row); $n++)
    {
    $Line = each ($Row);
    print ("{ $Line[key]'s value is $Line[value].}<br>\n");
    }
    I get a message that the arqument supplied is not a valid mysql resource. I want to print the row of results with the column alias's.
    Thanks

  2. #2
    SitePoint Wizard
    Join Date
    Jul 1999
    Location
    Chicago
    Posts
    2,629
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Probably there is an error in your SQL statement. Try echoing $Query and mysql_error().

  3. #3
    SitePoint Enthusiast
    Join Date
    Jul 2001
    Location
    cheshire
    Posts
    83
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    for loop

    It is something to do with this for loop. If I comment it out the error
    goes away.
    Any ideas?

  4. #4
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes it definitely has to do with your for loop. Where do key and value come from? I think what you are trying to di is this. However remember mysql_fetch_array returns tow sets of results in array $row one with numercial indices and one with associative indices, so you are gonna get 6 rows returned. You can make it like this and it should work for what you are trying to do.

    PHP Code:
    $Query "SELECT MAX(score) AS maxScore, MIN(score) AS minScore, AVG(score) AS averageScore FROM $TableName"
    $Result =mysql_query ($Query); 
    $Row mysql_fetch_array($ResultMYSQL_ASSOC); 
    foreach(
    $Row as $key => $val) {
    print (
    $Row[$key]."'s value is ".$Row[$val].".<br>\n"); 

    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  5. #5
    SitePoint Enthusiast
    Join Date
    Jul 2001
    Location
    cheshire
    Posts
    83
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    My fault

    I did a mysql_error on the query and the message that came back
    told me what the problem was. I recycle and adapt scripts and the
    start of this script refered to the wrong table, so mysql_error basically
    said field 'score' does not exist - which is true for the table that was being inspected.
    Thanks for telling me why I got twice the number of results to what I was expecting - that had puzzled me.


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
  •