0

Possible Duplicate:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result

I am getting this error while trying to run a function. I am sending a value $id to this function and trying to return the associated array. But I get a warning supplied argument is not a valid MySQL-Link resource.

But if i run the same code by eliminating the function part and give a static value to $id it returns the result correctly. This is the function

<?php
    mysql_select_db($database_spyware, $spyware);
    function get_product($id)
    {
        $query = sprintf("select product.descr as product_descr, product_id as Product_id,
                          category.descr as category from sp_url
                          inner join product
                          on product.id = sp_url.product_id
                          inner join category
                          on product.category_id = category.id
                          where sp_url.id = $id");

        $query_result = mysql_query($query, $spyware) or die(mysql_error());
        $row_rs_query = mysql_fetch_assoc($query_result);
        $totalRows_rs = mysql_num_rows($query_result);
        return($row_rs_query);
    }
?>
Community
  • 1
  • 1

1 Answers1

0

That happens because your second parameter spyware in function "mysql_query" is declared outside the function and is unreadable on your function scope.

try

global $spyware;

at the beginning of the function.

or leave the second parameter empty.

Tukaz
  • 72
  • 1
  • 4
  • Alternatively to avoid using a global, you could feed `$spyware` to your function as the second parameter. Ex. `function get_product($id, $spyware)`. – kjones Apr 03 '12 at 19:49