Storing ONE global value in php [closed] - php

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I need to store one global decimal value.
Would the best way to approach this be to store the value in a .txt file, or make an entire table for it in MySQL which will contain a single row for the value, or is there a different way?

It deppends on how often you will access it and how performance critical your application is. In brief DataBase should be your default unless you have a strong reason not to.
The reasons to keep the data in the database it's because it makes everything simplier and neater, backups don't need to worry about random .txt files that may be overlooked (And this a incomplete backup). If you need to set up a cluster it's a real PITA to keep files real-time synched while most databases support it easily, etc.
Why wouldn't you keep something in the database?
It's a enviroment value and thus in each installation of your
software the value may be different while using the same DB (In this
case people use File storage like the usual config .yml)
It's really performance critical to
have a top notch access-time to this data, for this people use
specialized engines to store such data (Redis, memcached, etc)
Now, answering your question, how to store it in your own db, that's up to you, but I would think something like a table named "GlobalConfigurations" with a column key and value could be a good approach. So that if you find any other variable with the same behavior you can put it there.

Related

PHP - Managing A Lot of Data Without Database [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
The Problem
I have an app that scrapes data and presents it to the user, directly, because of lack of disk space.
This data is very volatile, it can change within minutes. Much like the stock market.
Since the data changes so often, and it varies from user to user, it is useless to save it in a database.
The question
I need to sort the data presented to the user, compare it, link it etc. A lot of functions that a database provides. Yet I cannot save it in said database because of the above conondrums, what should I do?
What I've Thought of Doing So Far
I've tried organizing the data presented to each user using just PHP but seems troublesome, fragile and inefficient.
Should I just create some sort of virtual table system in MySQL just for data handling? Maybe use a good database engine for that purpose?
Maybe I can save all data for each user but have a cron job remove the old data in the database in a constant fashion? Seems troublesome.
The Answer
I'd like some implementation ideas from folks who have encountered a similar problem. I do not care for "try all of the above and see what is faster" type of answers.
Thanks all for your help.
If the data is of the type you would store in a db and you would benefit from being able to query it in ways that are more difficult in PHP, but you just don't want to keep it, you can still use a database. You can create temporary tables, insert raw data, and query it to get what you want. When you close the db connection, the tables disappear. Even though the script names them the same, the database will actually create a unique set per connection so each user will have unique data. This solution may not perform as well as you need so do some testing to see if it's suitable for your situation.

PHP variable for all users on server [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
It is possible to define variable in PHP and call it for all users connected on server?
I need variable, or object for store informations in RAM of the server without using database or server file system.
Save the data to the variable in one computer, and call them back in another connected computer.
What is the best practice, is it possible?
Roughly - yes, it is possible.
In order to do that you need to have access to RAM which I haven't seen in PHP done directly, not sure if is possible or not, you can research this yourself.
What you can do is, however, since PHP uses memory to run, you can take advantage of that and create a php script that will run forever and act as a server, that is going to use it's ability to write and read memory and is going to be an amazingly simple job since PHP handles that for you automatically and you would not have to bother with addresses and stuff ( describing a simple variable declaration ). In order to access this running script you will need to examine how sockets work and how to establish a server-client connection. That is very well explained in this article.
However, I do not mean to be rude, but by the way you form your question I can make an assumption that this may be too much for you, so I guess what you can do is use MemcacheD or any other in-memory caching mechanism that is already built by people better at coding than me and you. There is plenty of information out there, just search for in-memory caching mechanisms.
Good luck!

virtual pages using PHP only? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am working on a project that I need to let users to create pages on my server. however, I do not want to let users clutter my mysql database by storing the stuff in there so I cannot use mysql database for creating the pages.
I did research this topic and there seem to be a some sort of a plugin for WP that will allow virtual page creation.
is this possible using pure php WITHOUT the use of any database ?
It's possible, but wrong.
You can use php to write a html file to your web directory, sure. But that
solution is in no way cleaner or less cluttered than putting stuff in your
database, for a few reasons:
It's easier to have structured information in the database
It's a good thing conceptually to separate user data from your program
It's easier to control access to your database in a safe way, compared
to writing user data to the file system
"I really do not want to use mysql database" is not a good reason to give this
up. You might have a good reason, but it's not easy to guess what that is, which
makes suggesting alternatives very difficult.

Multilingual website, translations stored in a local.php file or in the database? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I have a simple question! I want to create my website with multilingual content, but i have a doubt about which is the right way to do this.
Option 1: Create a .php file called for example lang.en.php (where there is an array of all the strings I use on the page) and include it in the header.php file, so that to add a new language i only have to create new files like lang.es.php
Option 2: Store the strings in a database table and take them with a query.
Now my question is this: when i will upload the website on a real server, which one is the faster method? which one slows down less?
There is also an option 3 use mo/po files which are specialized for the task and also kinda nicely handle singulars / plurals for you.
However being sanely able to edit those files you would need to provide a GUI. But that should also be the case when you are using the array in a php file approach.
It's pretty hard to tell which way is the way ™ so I think I might close your question as too subjective.
When you are going for the database approach I would personally have a way to export the texts to either a po file or a array in a php file to prevent having to query the database for those mostly static texts.
To answer you question po files might be faster, however with opcache it might be pretty close to eachother. If you really want to know it the best thing you can do is do some tests with either approach because I just simply pulled the speed of po vs static array in a file out of my arse :-)
P.S. when you are benchmarking both methods also please keep in mind that whichever method you are going to choose performance is most likely not going to be the bottleneck so choosing one or another for performance is probably a bullshit reason.

Thinking about the efficiency of MySQL+PHP, which is better? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
I think of creating an administrative panel where all the inputs/textareas/selects that run the 'site' are created by MySQL.
What's better, create a row for each input/textarea/select referring to a page, or put the information of all inputs/textareas/selects each page in a single field and use the php explode in order to use the data ?
I advise to use case 1. If you will need to change PHP to some another technology (i.e. Java or Python), then it may have no "unserialize" function.
Also, it is easier to read data if you will need to analyse it manually.
Case 1 -- you're going to be storing that information in a relational database, so you may as well get the advantages that offers (for querying, udpating etc.) It will allow you to more easily manage your fields and change attributes of those fields.
I'm inclined to say Case 1 is better - keeps data better organized. Also, SQL is FAST, much faster than PHP, anyway, so if there is one less task for PHP to do (i.e. explode), the better.
Finally - it will be so much easier to maintain case 1. If you wanted to update the name of a field of a certain form, for example, you can...do just that, update only the name of that field. Otherwise you'll have to potentially do a find and replace, which may mean having to select, read, find, replace, update, a much more cumbersome operation.
Just my opinion, but I would go with case 1.

Categories