In MySQL there are also functions. For example DAY(), ASCII(), DATEDIFF(), and so on, a lot of function. So it is possible to make complicated queries using these functions. And all computing/manipulation with data will be done still in MySQL by MySQL functions. And I can get the result of my query in PHP already prepared by MySQL as refined as only possible.
But it’s also possible to compute/manipulate data from DB using PHP function. I mean, I retrieve data from DB using as simple SQL query as possible, then get it into an array, fetch that array into variables and only now, using PHP functions, begin necessary manipulations with my data to refine what I need.
So my question which way is better to work with DB? Should I use MySQL’s sever strength as full as only possible to get as ready-to-use data as only possible, or should I use more PHP functions to manipulate raw data from DB?
If you use MySQL (or other non-distributed databases) you might consider doing this in code. This will lighten the load on the database server while webservers are perfectly scalable these days. You only have 1 MySQL server, and can scale up to X webservers.
The best would be to create a helper class that wraps the database native functions.
If you leave the performance issues for what they are, it would always be better to use available functions instead of creating them yourself.
Assuming that you'd be executing the same statements against the database in either case, it will always be faster to run your statements directly in MySQL (e.g. functions, stored procedures) than it will be to use any of PHP's MySQL extensions.
look ta my question Text proccessing via Mysql Query.
its better to store simple data into data base as each data one row.then get it to php and proccess them.
Related
So far I store all the relevant parameters that I need for my website as variables in a php file; then I point to this file through a requirestatement in each page that needs those parameters.
This is most of times good and easy to mantain, but sometimes I need to change those variables "on the fly" and I feel the need of some sort of web-panel where I can change them even more easily (I'm thinking to a web page with a form to update the parameters).
So I've created a table in my MySQL database to store parameters (basically, this table has two columns: ParamName and ParamValue; I've stored the parameter names as a varchar without the $ sign at the beginning), in order to create the web-panel I've in my mind.
I was thinking to extract all the parameters names and values using this query:
$query=$mysqli->query('select * from parameters');
while($row=mysqli_fetch_assoc($query)) {
$ParamName=$row["ParamName"];
$$ParamName=$row["ParamValue"]; }
Is this a good or a bad idea?
What are the main issues I could encounter doing so (in terms of security, too)?
How deprecable and dangerous is the use of $$ParamName?
I'd better change my mind or can I proceed this way?
EDIT: I changed mysql_ syntax into mysqli_ as suggested.
Using arbitrary values in a database as variable references is highly risky. What you'd want to do is fetch the data from your key/value store into a self-contained associative array and use that instead.
You also do not want to be using mysql_query for anything these days. Please, put that thing away.
If the parameters are all constants that will not be changed over time, it is faster to use a single file that stores them as constants in a simple associative array.
If these parameters will change, using a database that specializes in key-value stores might be more useful for you instead of using a standard relational database.
If you cannot change your database from a relational database, or you are not changing the values often, it would be faster to cache them using something like memcached.
So I have a PHP script that currently uses MySQL functions to connect and query MySQL database. I need to change the code so it will work with any kind database, and not just MySQL. What are examples or other ways to do that with non-mysql functions?
I would create generic functions to usual operations (connect, query, fetch,...) and use them as wrappers to the MySQL or other SQL dbms you want to use. But I think you should be more focused on what you want to use.
Take a look at PDO: http://php.net/manual/en/book.pdo.php
It provides a way to access different databases. If you've written a lot of MySQL specific code it might take a while to convert, though.
I have an Excel spreadsheet with data I would like to use to populate a variety of tables in MySql. To ensure that business logic is adhered to, I have developed a series of stored procedures. Each row can call one or more of these procedures - depending on the contents.
I have thought of two possible solutions - Either
a) Write a PHP script to do it;
or
b) Write an Excel Macro to do it.
It must be noted that the data is still in the stage of possibly being edited before going live.
So my question is, what is the best solution? Any possible advantages/disadvantages with either one? Any possible pitfalls? Are there any other possible solutions?
Go with PHP if you populate the DB with data from Excel. I recommend http://phpexcel.codeplex.com/ for data manipulation.
I recommend using a php script. With it you can have the logic externalized and easily reuse it later.
There are a lot of classes/libraries
(PHP excel reader for example)
that you can use to parse the spreadsheet, thus the script can contain only content-related logic.
I am working on a project that is being built with a standard LAMP stack. Currently, I merely output the results of the query onto the page - the results are not being stored in objects at all.
In the future, I would like to edit the results of the query. I imagine that this would much easier if the results were stored in PHP objects.
Would it be more beneficial to store the objects themselves in a DB (via serialization/deserialization), or to create the objects when need be (after executing the query) and then destroying them when they are no longer needed?
You'd be better off storing a copy of the results directly in your object, rather than a serialized result handle. Serializing the result handle will NOT preserve locks, server-side variables, table state, transactions, or the data in the result set. MySQL has no provision for storing a connection handle in this fashion, so it'd be seen as a regular disconnect and resulting in outstanding queries being cleaned up, variables destroyed, transactions rolled back (or committed), etc...
As well, the data retrieved by the query is not actually fetched across the connection until you do a fetch_row()-type call, so you'd not even have that in your serialized handle.
Always create the objects in php, and destroy them later. In order to serialize you will need to use longtext or like field, which are known to be slow and you cannot index on them. If you are always doing a Select All, then go ahead, but if you ever use conditions or advanced queries, you should have all data separated.
It depends on many factors. If you are running the exact same queries again and again, then yes, store the results in your database. But why serialise them? If you tried Object-relational mapping, you could have a much easier to maintain query object, that you could store in a well organised relational database.
If you are not running the same queries very often, I would recommend caching the output in another way.
Would it be more beneficial to store the objects themselves in a DB (via serialization/deserialization), or to create the objects when need be (after executing the query) and then destroying them when they are no longer needed?
No. Somebody somewhere has done this for you. What would be beneficial is for you to use an existing ORM. It doesn't matter which one, just pick one and use it. You'll be lightyears ahead and get your project out the in a fraction of the time.
You should use a PHP framework while you're at it, many of which come coupled to an ORM.
How do the different MySQL Cursors within PHP manage memory? What I mean is, when I make a MySQL query that retrieves a large result set, and get the MySQL resource back, how much of the data that the query retrieved is stored in local memory, and how are more results retrieved? Does the cursor automatically fetch all the results, and give them to me as I iterate through the resource with fetch_array or is it a buffered system?
Finally, are the cursors for the different drivers within mysql implemented differently? There's several MySQL drivers for PHP available, mysql, mysqli, pdo, etc. Do they all follow the same practices?
That depends on what you ask php to do, for instance mysql_query() grabs all the result set (if that's 500 megabytes, goodbye) ; if you don't want that you can use :
http://php.net/manual/en/function.mysql-unbuffered-query.php
PDO, MySQLI seem to have other ways of doing the same thing.
Depending on your query, the result set may be materialized on the database side (if you need a sort, then the sort must be done entirely before you even get the first row).
For not too large result sets it's usually better to fetch it all at once, so the server can free used resources asap.