1

I am trying to retrieve data that is collected in a function, that contains my query. I am returning the data back to the main page and printing it using a while loop however i am getting this error:

Warning: mysql_fetch_assoc() expects parameter 1 to be resource, string given

Below is my code:

Main page:

 <?php 
        $get5star = get5star(); 
        while($row = mysql_fetch_assoc($get5star)){
        echo $row['departmentid'] ;

        }
?>

function:

 function get5star(){

   $strSQL = "SELECT * FROM `5starproducts`";
   $query = mysql_query($strSQL);
   return mysql_result($query, 0);
     }
2
  • 1
    Avoid using the mysql library in PHP, use the mysqli library instead. Did you check the reference manual on php.net for this function? php.net/manual/en/function.mysql-fetch-assoc.php Commented Jan 22, 2015 at 13:00
  • 2
    Solution is simple: stop using mysql_* API ;) Commented Jan 22, 2015 at 13:01

2 Answers 2

8

try to return your result like

return $query;

cause you are returning string(your mysql_result($query, 0) returning 0th position column value only), need a query result for mysql_fetch_assoc()

Sign up to request clarification or add additional context in comments.

Comments

0

try the corrected function: function get5star(){ $strSQL = "SELECT * FROM5starproducts"; $query = mysql_query($strSQL); return $query; }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.