[RESOLVED] code doesn't work when put in function
Results 1 to 3 of 3

Thread: [RESOLVED] code doesn't work when put in function

  1. #1
    Junior Member
    Join Date
    Jun 2003
    Posts
    22

    resolved [RESOLVED] code doesn't work when put in function

    Hi,
    I have a store and I wanted to display a sold out graphic when the stock level reaches zero.

    If I put this code in each table cell it works just fine (adjusting the product_model for each) I get the graphic when the stock level is 0
    Code:
    <?php $query = " SELECT products_quantity FROM products WHERE products_model='JLP.1A.4' ";
    $result = mysql_query($query);
    while($row = mysql_fetch_assoc($result))
    if ( $row['products_quantity'] == 0 ) 
    {echo '<img src="images/SoldOut.jpg" width="32" height="32" alt="" >' ;} ?>
    Now I attempted to put most of that in a function so the function
    Code:
    <?php
    function checkStockLevel() 
    {
    $query = " SELECT products_quantity FROM products WHERE $stockModel ";
    $result = mysql_query($query);
    while(list($products_quantity)= mysql_fetch_row($result))
    if ( $products_quantity == 0 ) 
    {
    echo '<img src="images/SoldOut.jpg" width="32" height="32" alt="" >' ;
    }
    }
    ?>
    and then in each table cell I put this (adjusting each product) and calling the function
    Code:
    <?php echo checkStockLevel ($stockModel='JLP.1A.4') ;?>

    However when I do that I get this error instead of the graphic -
    Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource in /home/wan/public_html/junior2.html on line 47

    line 47 is the line -
    while($row = mysql_fetch_assoc($result))

    Thanks in advance for any ideas,
    jim

  2. #2
    Pedantic Curmudgeon Weedpacket's Avatar
    Join Date
    Aug 2002
    Location
    General Systems Vehicle "Thrilled To Be Here"
    Posts
    21,906
    You define the function's parameters when you define the function, not when you call it.
    PHP Code:
    function checkStockLevel($stockModel)  {
    ...
    }

    ...

    echo 
    checkStockLevel('JLP.1.A.4'); 
    THERE IS AS YET INSUFFICIENT DATA FOR A MEANINGFUL ANSWER
    FAQs! FAQs! FAQs! Most forums have them!
    Search - Debugging 101 - Collected Solutions - General Guidelines - Getting help at all

  3. #3
    Junior Member
    Join Date
    Jun 2003
    Posts
    22
    Thanks Weedpacket.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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