Questions about data listing!

Asked

Viewed 34 times

1

Hello I am developing a project using the Mysqli extension, and in this taking advantage to use some object-oriented functions! I would like to ask a few questions regarding the data listing, are two questions:

1) Use the foreach() function instead of a while() with the fetch_array() function inside, do you have a faster or slower listing? Because with both I can bring the data, At the moment I’m using the foreach for being faster to type.

2) I use many different table listings on the same page always calling a new SELECT, it would be better to create a function with a SELECT in it so I always reuse it there or it would not change much in terms of performance?

an example of function for queries:

https://www.codigofonte.com.br/codigos/funcao-para-consulta-a-banco-de-dados-mysql-no-php

I appreciate any information.

1 answer

1

Answering every question:

1) My suggestion is to use what makes the most sense for each context.

Example: if you have a list of objects and want to take attributes to expose, using while does not make this so clear in your statement, but it fits perfectly in what the foreach proposes.

2) I believe that taking the queries directly from the page can be good to clean and separates the work, but unify the queries since they are from different tables would have to think like a ORM, by the possibility of variety in a query.

  • Hello, I do not know if I could give you my question regarding question 2, I edited the post and put a link, you think if I always call a function (like the link) that makes a SELECT instead of always creating a SELECT manually without the function would be faster in terms of performance? Or theoretically I’m calling a new SELECT the same way within the function?

  • Opa @Caiolourençon you still continue performing the various SELECT and calling them again, this solution will leave the more organized code that I believe is an advantage depending on the context.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.