php - does querying mysql by using function takes more time to load? -


I was thinking that to query a specific row by using the function, each row calls the function userinfo ($ table, $ line, $ data, where $) {/ p>

query from a normal mysql query at least once. Global $ db; $ Users_sql = $ db- & gt; Query ("SELECT $ row FROM $ table $ WHERE $ where = '$ data'"); $ users_row = $ db- & gt; Fetch_assoc ($ users_sql); Return $ Users_row [$ row]; } Echo userinfo ('user', 'first name', $ _SESSION ['username'], 'username'); Echo userinfo ('user', 'avatar', $ _SESSION ['username'], 'username');

Or do the same without the function

  $ users_sql = $ db- & gt; Query ("SELECT $ row FROM $ table WHERE $ where = '$ data'"); $ Users_row = $ db- & gt; fetch_assoc ($ users_sql); Echo $ users_row ['firstname']; Resonate $ users_row ['avatar'];   

I think the other way would be better, but I have to check only twice.

Change it first:

  function userinfo ($ table, $ Line, $ data, $ where) {global $ db; $ Users_sql = $ db- & gt; Query ("SELECT $ row FROM $ table $ WHERE $ where = '$ data'"); $ Users_row = $ db- & gt; Fetch_assoc ($ users_sql); $ Users_row Return; } $ Row = userinfo ('user', 'first name, avatar', $ _SESSION ['username'], 'username'); Echo ['first name'] per line; Echo ['avatar'] per line;   

This will make it more meaningful and avoid an additional database query; But your second "function without" counterpart is not exactly equivalent because you are thinking that $ users_row will return both first name and avatar , while your first example would be 't (Unless you use the logic shown above)

Even more common will be

  function userinfo ($ table, $ data, $ Where) {Global $ Database; $ Users_sql = $ db- & gt; Query ("SELECT * FROM $ table $ WHERE $ where = '$ data'"); $ Users_row = $ db- & gt; Fetch_assoc ($ users_sql); $ Users_row Return; } $ Row = userinfo ('Users', $ _SESSION ['Username'], 'Username'); Echo ['first name'] per line; Echo ['avatar'] per line;   

But your attitude is not good; You should actually use the prepared statement and variable binding; And prepare your questions accordingly

Comments

Popular posts from this blog

Java - Error: no suitable method found for add(int, java.lang.String) -

java - JPA TypedQuery: Parameter value element did not match expected type -

c++ - static template member variable has internal linkage but is not defined -