php - Is it best to make fewer calls to the database and output the results in an array? -


I'm trying to make a more concise way to call hundreds of DB calls. Every time I wanted to output a field, instead of typing the whole query, I tried to close that code in a class that used to work all the queries. This is the category so far:

class mysql = new mysqli (DB_LOC, DB_USER, DB_PASS, DB) or die ('Could not connect'); } Function getListingInfo ($ l_id = "", $ category = "", $ subcategory = "", $ username = "", $ status = "active") {$ condition = "` status` = '$ status' "; If ($! Below ($ l_id)) $ bet = "And` L_ID` = '$ l_id' "; If ($ ($ category) empty down! = "And` category` = '$ range'"; If (below! ($ Subcategory)) $ bet = "And` subcategory` = '$ subcategory'"; If (empty! ($ Username)) $ bet = "AND`Username '=' $ username ''; $ result = $ IT- & gt; MYSCL- & gt; query (" Select where $ a bet is from ") Choose or die ('Error fetching the value '; $ Info = $ result-> fetch_object () or die (' could not create object '); return $ info;}}

Information is easy to access.

  $ listing = new listing; counterpart $ listing-> getListingInfo ('', 'books') -> title;  

This first category in the "Books" category But if I want to produce the price of that listing, I will have to make another call to get the listing (). It creates another query on DB and then returns only the first line. / P>

It is more concise than writing the whole query every time, but I think I can also call DB too often, is there a better way to output data to my class and yet It is a concrete way to output all rows in brief (perhaps an array of access and return array)? If yes, then how?

You should be able to store the whole object in a variable in a query, then from that object Access:

  $ object = $ listing-> GetListingInfo ('', 'Books'); $ Title = $ object- & gt; Title; $ Value = $ item-> value;  

But you can also use fetch_assoc () and return the entire associative array:

  $ array = $ listing-> GetListingInfo ('', '' Books ''); $ Heading = $ object ['title']; $ Value = $ object ['value'];  

This will give you the same results and

Edit with DB only one query: if getListingInfo () is the only function You should think about the following:

  1. Rename the listing to rename the listingInfo () and just create the query within the function and store it in a square variable.
  2. Add a getNextListingInfo () function, which will return an object or associate array to the next line.

Using this new function, you can get every row that matches your query.


Comments

Popular posts from this blog

windows - Heroku throws SQLITE3 Read only exception -

lex - Building a lexical Analyzer in Java -

python - rename keys in a dictionary -