0

I want to pass a mySQL result from a function and print the data. How to do so ?

<?php
function showLatestItems()
 {
  $result = $this->query("SELECT aid,name,description,img,amount,strtdate,enddate FROM item WHERE enddate>now()");
  return $result;
/*      while($row=mysql_fetch_array($result))
   echo $row[0].' '.$row[3].' '.$row[1].' '.$row[2].$row[4].$row[5].$row[6].'<br>';
*/
 }
?>
0

1 Answer 1

1

Just trap the result resource and then fetch rows from it. Based on the commented out code, we assume you have wrapped mysql_query() with a class method called query().

$result = $yourclass->showLatestItems();
if ($result) {
  $rowset = array();
  while ($row = mysql_fetch_assoc($result)) {
    $rowset[]  = $row;
  }
}

var_dump($rowset);
Sign up to request clarification or add additional context in comments.

4 Comments

I hate var_dump, plz be nice :P
@Sourav var_dump() is only there to prove $rowset is an array.
Warning: Invalid argument supplied for foreach()
@Sourav What foreach? There is none in your code or in mine.

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.