Mysql fetching data for select list - php

I have a situation for which I want best solution as I already know a way to complete that. Only problem is I am facing the similar requirement times and again.
Situation is, I have multiple select lists in a single form and every select list has couple of items, client wants translation for those items.
So if I create a table for every select list then if I have 10 select lists then I have to fire 10 queries, which I want to know is OK because I don't see it fine.
Or I can create language file where I can declare those array and include that file as per selected language.
Can anyone share their experience tackling with this particular solution, how can we solve this problem in the best possible way.
Thanks in advance.

Unless every ounce of performance is a concern to you right now, I don't think there's anything terribly wrong with having 10 separate queries (provided they reuse the same database connection).
If that leaves you uncomfortable, you could retrieve all of the select box options for a given language with a single query (either by using UNION, or normalising your data so that a single table contains all of your select box options and columns indicate to which list and language each record belongs); then PHP could split apart the results of that single query accordingly.
But, as your question specifically asked about best practice, you might like to read up on PHP's support for gettext() (as described in this question) or look into a framework that provides localisation features.

Related

What is more efficient, retrieving the whole MySQL table, or creating a database manager which can retrieve a variable number of fields?

So, the context is: I have a site in which many pages may need the information about one table, say for instance, 'films'. This table has many fields, like title, language, year, description, director... And perhaps in one page I need only the title and the id of some rows and in another I also need the description.
So the question is: should I code a database manager (I am using MySQL) that retrieves all the fields of the rows that satisfy a condition (I guess the WHERE clause should be passed as a parameter)? Or should I be able to specify which fields are needed? I thinks this cannot be done easily with mysqli (because prepared statements require to specify beforehand the number of fetched fields), so for this to work I would need to use PDO instead, which I haven't used yet. Is it worth it this last approach? Or there is not really a big difference in performance if I retrieve the whole information about those rows?
Thank you in advance.
Based upon the comments above, My answer to your question(s) is
Retrieving some fields vs all fields isn't a real performance consideration until you are dealing with one or more CLOB/TEXT columns which have a lot of text in them. Good database practice indicates you should always specify which fields are returned from a query.
Any query against any table should have a where clause to restrict the number of rows returned. Especially if you are looking to query exactly one row.
Your question implies you are writing a wrapper layer around the queries to hide this complexity. Don't do this. Get an existing PHP library that does this work for you. See for example: Good PHP ORM Library? . There are a number of subtle issues, like security, which you will overlook.

Autocomplete concept

I'm programming a search engine for my website in PHP, SQL and JQuery. I have experience in adding autocomplete with existing data in the database (i.e. searching article titles). But what about if I want to use the most common search queries that the users type, something similar to the one Google has, without having so much users to contribute to the creation of the data (most common queries)? Is there some kind of open-source SQL table with autocomplete data in it or something similar?
As of now use the static data that you have for auto complete.
Create another table in your database to store the actual user queries. The schema of the table can be <queryID, query, count> where count is incremented each time same query is supplied by some other user [Kind of Rank]. N-Gram Index (so that you could also auto-complete something like "Manchester United" when person just types "United", i.e. not just with the starting string) the queries and simply return the top N after sorting using count.
The above table will gradually keep on improving as and when your user base starts increasing.
One more thing, the Algorithm for accomplishing your task is pretty simple. However the real challenge lies in returning the data to be displayed in fraction of seconds. So when your query database/store size increases then you can use a search engine like Solr/Sphinx to search for you which will be pretty fast in returning back the results to be rendered.
You can use Lucene Search Engiine for this functionality.Refer this link
or you may also give look to Lucene Solr Autocomplete...
Google has (and having) thousands of entries which are arranged according to (day, time, geolocation, language....) and it is increasing by the entries of users, whenever user types a word the system checks the table of "mostly used words belonged to that location+day+time" + (if no answer) then "general words". So for that you should categorize every word entered by users, or make general word-relation table of you database, where the most suitable searched answer will be referenced to.
Yesterday I stumbled on something that answered my question. Google draws autocomplete suggestions from this XML file, so it is wise to use it if you have little users to create your own database with keywords:
http://google.com/complete/search?q=[keyword]&output=toolbar
Just replacing [keyword] with some word will give suggestions about that word then the taks is just to parse the returned xml and format the output to suit your needs.

MySQL - Is it wise?

I'm using MySQL as my main database for a simple "Social Network" I'm spending a few weeks on.
As with all social networks, the user requires a connection with their friends in order to make it social.
My theory was to either add another column onto my user database and name it connections. There, I would store a string of user id's separated by a comma, then split them when needed.
Another theory I had was to create a completely new table connections and use two columns "user_1" and "user_2". The database would then, when searching for friends, would perform a select looking for their id and so on.
The question is though: What would be the most efficient? If I'm to support large numbers of users, is it risky going with option 2?
Some advice would be greatly appreciated,
Thanks!
A normalized structure (option #2) is highly preferable for structuring the type of data that you describe. It will be far more efficient to query a narrow table with two integer columns than to split through an ever-growing list of IDs.
I would suggest reading about the different normalization forms: http://en.wikipedia.org/wiki/Database_normalization (see "Normal Forms")
The second approach is much better. You're creating relations between the user by using a table 'connections'. This way you can create 'n:m' relations. If you want to add some kind of connection type ('love interest', 'friend') you can easily add it in a table, but not in a string.
There's another benefit: you don't have to think about the number of connections a user has. What would you use for the connections? A varchar? A text? Do you really want to parse this mess each and every time? How do you make sure that you don't add a connection twice?
tldr;: Use a table to show relations.
Option 1 will not end well. Go with a separate table.
A separate table called connections without a doubt would be easier. having multiple values in one column defeats the purpose of a database, can you imagine searching for all friends of user1 with option 1?
MySQL can certainly deliver good performance with option 2. It's easier to select friends and to do calculations. There's a lot you can do with caching, multiple servers, load balancing and all that.
And realistically speaking: by the time you reach a large number of users, you'll be rewriting the system anyway to incorporate all the lessons you've learned along the way.

Do I have too many queries for lookup tables and can anyone suggest an alternative?

I am currently building a codeigniter application that handles a specific type of mammal. When a user is adding a new record (mammal), they are given lists of 'breed types', 'genders', etc. Those are stored in separate database tables.
Currently, to get these, I have separate functions such as:
$this->Mammal->get_list_of_breeds()
$this->Mammal->get_list_of_genders()
Each of these calls a query, there may be up to 7 or 8 more different lookups for me to query. Does anyone know if this will significantly slow down my application or cause too many queries on the database. For the most part, the max number of records in any individual table is under 300 records.
Is there a better way I can be doing this by consolidating the queries into a single function and using php to split the lookup fields?
Any ideas or thoughts are greatly appreciated.
One idea is to take some of the smaller sets of options and put them in arrays, especially if they cannot be changed by the user. Gender, for example, could probably just be in an array. As far as I know, there are only two options. If there are any other similar option sets you could make those arrays too.
But, even 300 records is not a huge amount of data. I take it you aren't building the next Facebook, so just making several clean queries to get the options you need probably won't be a big deal.
Personally, I wouldn't put it all in one table. Big generic tables just seem kind of hokey, and you would still be getting the same amount of data. You could have separate tables and accomplish the same thing by UNIONing the queries.
As you commented yourself, yes indeed you should put everything into one table...
So you'd have a table called mammals
And then you'd have the fields: gender, breeds etc...
Now this is a lot easier when programming in php since now you can do one query and then display everything, like this:
$query="SELECT * FROM `mammals`";
$query_exec=mysql_query($query);
while($result=mysql_fetch_array($query_exec))
{
print "gender: ".$result['gender']." breed: ".$result['breed'];
}
Little explanation:
The query gets everything from the table called mammals
Then the while just continues as long as there are still results in the array
The fetch array puts the data in the variable and every field can be read by $result[]
I know this is not a very clear explanation, but my mind also isn't the cleares at this late hour :/

What is the best strategy to store user searches for an email alert?

Users can do advanced searches (they are many possible parameters):
/search/?query=toto&topic=12&minimumPrice=0&maximumPrice=1000
I would like to store the search parameters (after the /search/?) for an email alert.
I have 2 possibilites:
Storing the raw request (query=toto&topicId=12&minimumPrice=0&maximumPrice=1000) in a table with a structure like id, parameters.
Storing the request in a structured table id, query, topicId, minimumPrice, maximumPrice, etc.
Each solution has its pros and cons. Of course the solution 2 is the cleaner, but is it really worth the (over)effort?
If you already have implemented such a solution and have experienced the maintenance of it, what is the best solution?
The better solution should be the best for each dimension:
Rigidity
Fragility
Viscosity
Performance
Daniel's solution is likely to be the cleanest solution, but I get your point about performance. I'm not very familiar with PHP, but there should be some db abstraction library that takes care relations and multiple inserts so that you get the best performance, right? I only mention it because there may not be a real performance issue. DO you have load tests that point to an issue perhaps?
Anyway, if it is between your original 2 solutions, I would have to select the first. Having a table with column names (like your solution #2) is just asking for trouble. If you add new params, you have to modify the table columns. And there is the ever present issue of "what do we put to indicate not selected vs left empty?"
So I don't agree that solution 2 is cleaner.
You could have a table consisting of three columns: search_id, key, value with the two first being the primary key. This way you can reconstruct a particular search if you have the ID of a saved search. This also allows you to expand with additional search keywords without having to actually modify your table.
If you wish, you can also have key be a foreign key to another table containing valid search terms to ensure integrity. Whether you want to do that depends on your specific needs though.
Well that's completely dependent on what you want to do with the data. For the PHP part, you need to process it anyway, either on insertion or selection time.
For really large number of parameters you may save some time with the 1st on the database management/maintenance, since you don't need to change anything about your database scheme.
Daniel's answer is a generic solution, but if you consider performance an issue, you may end up doing too many inserts on the database side for a single search (one for each parameter). Too many inserts is a common source of performance problems.
You know your resources.

Categories