SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast tipiyanos's Avatar
    Join Date
    Jun 2001
    Location
    NY, New York
    Posts
    77
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Invalid argument error in mysql

    I want to run the following query through a php script.
    PHP Code:
    SELECT sum(price) as sum FROM products 
    I execute the query after connection as follows:

    PHP Code:
    <?
    $query 
    mysql_query("SELECT sum(price) as sum FROM products");
       while (
    $sql mysql_fetch_array($query)) {
        
    $inventory $sql["sum"];
    echo(
    $inventory);
    }
    ?>
    However, I get the following error

    Warning: Supplied argument is not a valid MySQL result resource in templates/header.inc on line 20

    However running the same query directly from mysql works fine.

    Can't find anything wrong with the simple query.
    Any help would be appreciated.
    Thanks in advance

  2. #2
    SitePoint Enthusiast tipiyanos's Avatar
    Join Date
    Jun 2001
    Location
    NY, New York
    Posts
    77
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The mysql query is
    PHP Code:
    SELECT sum(price) as sum FROM products 
    not

    PHP Code:
    SELECT sum(price) as sum 
    sorry for the mistake.

  3. #3
    SitePoint Enthusiast tipiyanos's Avatar
    Join Date
    Jun 2001
    Location
    NY, New York
    Posts
    77
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is there something wrong with the forum. I type something and something else gets posted.

    I meant to display the query as

    SELECT sum(price) as sum FROM products

    However for some reason it is truncating the FROM products part. Dont know whats wrong. Sorry for the multiple posts.

  4. #4
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    For debugging purposes print out the mysql_error() and see if it gives you any clues.
    PHP Code:
    <?
    $query*
    =*mysql_query("SELECT*sum(price)*as*sum*FROM*products");
    echo 
    mysql_error();

    while*($sql*=*mysql_fetch_array($query))*{
    ****$inventory*=*$sql["sum"];
       echo(
    $inventory);
    }
    ?>
    I would be inclined to use a different alias name than "sum" - it could be causeing probelms ?!? Try:

    SELECT*sum(price)*as*sumPrice*FROM*products

  5. #5
    SitePoint Enthusiast tipiyanos's Avatar
    Join Date
    Jun 2001
    Location
    NY, New York
    Posts
    77
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Now that worked... I guess there was some problem with the "sum" word. Thanks for the suggestion freakysid.

  6. #6
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah - I suspected as much. you see, SUM is the name of a function, so it is going to confuse things having an alias with the same name as a function.


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
  •