MYSQL function or PHP function, which one is better? [duplicate] - php

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Performing simple arithmetic in MySQL statement or in PHP code
Im wondering what is the best to use, from point of view performance:
PHP functions (to calculate a date in the future) and to input the date in the DB query
MYSQL function (to calculate a date in the future) e.g. TIMESTAMPADD(DAY,3,UTC_DATE()) directly inside the query

If you're manipulating dates in a table, it's definitely better to use MySql functions. This is particularly true if the mySql server might be accessed by different hosts (which might not have their clocks all synchronized with each other).

Related

Possible file_put_contents conflict? [duplicate]

This question already has answers here:
PHP multiple clients accessing same file
(2 answers)
Multiple users write to the same file at the same time using PHP
(4 answers)
Closed 1 year ago.
I am posting this not about code error but about possible conflict using this piece of code:
$logFile = 'data.log';
$result = (int) file_get_contents($logFile) + 1;
file_put_contents($logFile, $result);
This will read a file called data.log and get it's number and write this number +1. But, I will run this application with many users calling this in realtime (like 100 users per second).
So, I want to know if this will create some conflict, get outupdated number and store wrong data or something like this. Is it possible? Are there an way to I avoid it?
Thank you.
It will not work correctly because the data is not changed "atomically".
You can solve it by locking the file before accessing it - read about the flock function. But this could affect performance and possibly seriously limit your throughput - so you should benchmark it.
Generally speaking, this is not considered a "good" way to implement a counter. Look into using a proper database like MySQL or a NoSQL like MongoDB.

alternative to mysql_nd functions [duplicate]

This question already has answers here:
Call to undefined method mysqli_stmt::get_result
(10 answers)
Closed 3 years ago.
I moved my php code to a new hosting which missing mysql_nd and a have more than 150 functions that use stmt->get_result() and fetch_assoc.
Is there a function that can do the same work of stmt->get_result() and fetch_assoc and give the exact results so I can fix the problem without having to modify all the functions?
You have two choices.
The first is to contact your provider indicating that you need MySql Native Driver.
The second is to review all your code as a PDO or alternatively MySqli (PDO Council).
In terms of time, with the second option we invest a little more, but for sure you will make your application more stable and secure. I would personally use it for the second hypothesis.

Localization of strings in the database [duplicate]

This question already has answers here:
Schema for a multilanguage database
(10 answers)
Closed 8 years ago.
I have an app written in PHP with MySQL in the back. Originally the app was using custom language class that was quite ineffective so I changed everything to use gettext.
The problem is a lot of strings are in the database, stored in tables that are never changed just read from. The good thing is every installation is in only one language.
In order to provide the strings in different languages, different databases were used before. There is database_en, database_fr, etc. and every new installation is using the appropriate localized database. I want to change this - one database for all languages.
I am thinking of moving everything language specific from the database to config files where gettext can be used again.
Do you think this is good idea or you would continue using different databases for different installations? Can you recommend something better ?
I recommend using google translation api at front end and single database for all with unicode data types.

When to use views and when to use stored procedures [duplicate]

This question already has answers here:
MySQL: Views vs Stored Procedures
(4 answers)
Closed 9 years ago.
I am a application where number of reports are more. What i do for each report is that i create a mysql view and a mysql stored procedure. From front end php i give a call to stored procedure with where clause, based on this where clause i fetch results from the particular view. Recently i found out that it was causing performance issue. So i avoided views and wrote the same code in stored procedure and performance improved. So from that poit i am confused as to ideal situation when i should use Stored procedure and when i should use views.
And Does my scenario explained above really cause performance issue or was it problem at my end?
Views in mysql are mainly for readability. They enable you to hide a possibly complex query over multiple tables into something that appears to be a single table.
I would suspect that the most likely cause is not the use of views themselves (although not sure they would help you in any way, while a stored procedure might well be more efficient), rather a view you are using is poorly optimised (maybe ignoring keys).

How can I insert data into two mysql tables at once using PHP [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
mySQL - Insert into three tables
I want to know if I can insert data into two tables using one query in PHP/MySQL?
If it's possible, can I see an example?
No, it is not possible. Mysql INSERT syntax accepts one and only one table as a target
using one query
no
Using one query! I don't think so.
However, I believe that the most appropriate methodology is to utilise transactions. Using transactions, you assure to have data inserted in more than one table (successfully), otherwise any changes are rolled back. That is, either persist all changes or do nothing at all.

Categories