0

I want to create a php function that can access the database just by writing a query. for example:

<?php
   require"connect.php";
   function querymyrow($query) {
      global $conn;
      return mysqli_fetch_array(mysqli_query($conn, $query));
   }
?>

<table border="1">
   <?php while ($row = querymyrow("SELECT * FROM product")) { ?>
      <tr>
         <td><?php echo $row['id']; ?></td>
         <td><?php echo $row['name']; ?></td>
      </tr>
   <?php } ?>
</table>

The $conn variable is loaded from connect.php so it's not a problem. The problem is that when the program executes, the loop is successful, but only returns the first value from the database until the end of the loop. Does anyone know how to make the correct function in this case?

  • You don't really need such a function because mysqli already has one. just write your code as `query("SELECT * FROM product") as $row) {` – Your Common Sense Jan 19 '21 at 08:47
  • also, as far as I can tell, this loop will run infinitely, for such an obvious reason that it runs the same query over and over again – Your Common Sense Jan 19 '21 at 08:50
  • But I think I need that function to work, because I use databases from two sources, Localhost and ODBC. So that when I want to retrieve from a different database source, it requires a lot of writing code on the same page. I just want to make the code simpler. But the function code above doesn't cause an infinity loop. The fetched data is only along the database rows. If there are 10 database rows, then the loop is also 10. So the problem is why the function only returns the first data from the database until the end of the loop. – Zeto Penzom Jan 19 '21 at 09:04
  • If you have a problem with whatever "too much code" issue, then you should ask a question about this exact problem, not about your attempted solution – Your Common Sense Jan 19 '21 at 09:20

0 Answers0