I have a class where I collect all the comments. then I need to insert these comments into another page, but since I make a template, I have a request to the database itself and the result of the request is stored in a class function, and then I just call it on the page and insert the values ​​where necessary (two-dimensional array).

in class

public function GetComments(){ $result = mysql_query("SELECT * FROM comments"); return mysql_fetch_row($result); } 

and already on the page I received and remembered the result of the function in a variable and output

 <?php foreach ($allComments as $value) { ?> <tr> <td><?=$value;?></td> <td><?=$value;?></td> <td><?=$value;?></td> </tr> <?php } ?> 

but only 1 query string is passed to $ allComments. if I do not insert mysql_fetch_row in the function, then the resource is returned (and I would have an array). how would i fix this whole thing. broke my head

UPD. the issue is resolved

  • Sorry for offtopic but Kostya is sure you are? Well, show the solution? - Naumov

2 answers 2

For those who answer the question "I return only one result line from a function. Why does the function always return me only one line?" still not obvious, and wants to know the mysterious solution, below is a session of exposing black magic.

For the most despised mysql ext:

 $return = array(); $result = mysql_query("SELECT * FROM comments"); while($row = mysql_fetch_row($result)) { $return[] = $row; } return $return; 

for nobody understand mysqli

 return $mysqli->query("SELECT * FROM comments")->fetch_all(); 

for recommended PDO

 return $pdo->query("SELECT * FROM comments")->fetchAll(); 
  • better remove mysql_query from sight. That did not copy bad examples for old versions of php. - Nepster

It is strange to see this in 2016. Please use PDO or look towards frameworks. (For example LARAVEL5 or Yii2).

  • But this is a comment and not an answer. - Naumov
  • "UPD. Question resolved" right at issue. And I gave advice that people dig in the right direction. - Nepster
  • Voted for, but so that he would dig in the right direction, I would still give a specific example of use. Well, a фрамворк as something else can be better than the фреймворк - Naumov
  • Perhaps you `re right. Corrected the answer. - Nepster