Building an application independent of the SQL engine used [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 5 years ago.
Improve this question
I wanted to ask that if and how is it possible to make a PHP-based application independent of the SQL engine used?
(Under that I mean that the application can be executed on PostgreSQL, MySQL and maybe also Oracle SQL.)
Current idea is to leave the PDO DSN into the config file, so that the queries etc everything else stay the same, but can I face some problems in regards of CRUD actions to the tables themselves?
If I don't add any complex stuff in it, then is it possible for me to make it so that it doesn't depend on the engine at all, only has the PDO DSN for the database and that it can query everything the same way on each of the SQL engines?
Best regards!

Read about Database Abstraction Layer. There are quite some libraries available for PHP. If you choose any of the popular frameworks, to mention Symfony or Laravel - you will have the DBAL out of the box - either Doctrine or Eloquent. Both offer similar query building functionality, IMO based on HQL.
Definitely don't try to write it "your way". Even if you do only simple queries the gramma is different, to mention just the types, incrementing etc...

If you stick to a common subset of SQL which is supported by all your targeted databases, then yes, you can get away with using the same PDO instance, the same queries, and just switching the PDO DSN.
Practically speaking however, for anything but the most trivial queries, you'll probably be using some database specific features which you'll have to implement slightly differently for different databases. Simply speaking that means your code will have to execute query A if connected to MySQL but a slightly different query B if connected to Postgres.
You certainly do not want to implement that using a lot of if..else, instead you want to be using database specific adapters/drivers. In your business code you'll be calling $database->getUserRecords(), and depending on whether $database uses the MySQL or Postgres adapter the query will be slightly different. (Also see dependency injection.)
You can either do this by implementing those adapters by hand and tweaking the queries for each database, or use a more abstracted ORM/DBAL which can assemble the correct query on the fly using database-specific query builders. There are many existing libraries for that.

Related

Should a PDO script written for MySQL work with Oracle? [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
I understand that in general PDO scripts are cross compatible i.e. generally changing the connection string should work.
In the past I've spent hours searching online after changing a PDO script connection string from MySQL to SQLite as this isn't the case, some things don't work the same (I remember an issue with row counting or something).
So should changing from MySQL to Oracle be generally simple, or are there things to watch out for as in the SQLite case?
So should changing from MySQL to Oracle be generally simple, or are there things to watch out for as in the SQLite case?
There are things to watch out.
More seriously, beside basic SQL query, each RDBMS has its own set of specific features that have to be taken into account. Just to give one example, if you want to limit the result set to one row only, MySQL provides the LIMIT clause. But for Oracle up to 11g, you need a sub-query for that purpose.
If you really need cross-vendor support, you probably should take a look at some library providing database abstraction layer whose job is to allow you to write database-agnostic code. PDO isn't such a library. But Doctrine DAL, Zend_db and many other are.
It is now considered as off-topic to request suggestions for a tool here, but take a look at this old question if you need few pointers: Best PHP DAL (data abstraction layer) so far

Lost in how to start learning databases with php [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I was developing a website and learning how to code at the same time. So far I just got the hang of PHP. And now i want to learn how to integrate databases with PHP. Do i start learning MySQL and its libraries? I've been reading that PDO are the way to go and I'm just lost on what order I should be learning things.
Mysql is a database. Completely different thing. One is supposed to learn basic Mysql separately, independently from PHP or any other language, practicing basic queries in console or whatever GUI client.
Once you make yourself familiar with basic SQL, you can turn to running basic queries from PHP using PDO. Only static queries without variable parts.
And finally, you may start for building dynamical queries using prepared statements. Basic info on PDO you can find here in the tag wiki
You have a very open-ended question.
However, typically with PHP the standard is MySQL so I would suggest you learn that. Furthermore, most SQL is similar in syntax. Typically when working in PHP you will use MySQL, PostgreSql or SQLlight. They all have very similar syntax.
I suggest you find a book on PHP/MySQL and study that. I typically don't endorse books, but there is a great book for beginners by Head First called, "Head First PHP & MySQL". You will learn a lot with that book, you can google it as I don't know the link.
Furthermore, php.net is your best resource for PHP-related programming.
One final tip, I suggest you learn the Object-oriented way to program in general including opening your database connection as procedural-style is going away.
Good luck! And I am happy you are going with PHP and aren't another .net drone.
(also, download cake.php or codeignighter to learn about building lightweight, well-designed frameworks)
I would start in following order:
Create basic table and input some value through phpmyadmin
example: do one for users. create table named "user" and columns "user_id","username","password", "email"
Use SELECT statements to select certain data through phpmyadmin
Build simple php page with connection and retrieve your db data
Learn about SQL injections and security issues learn Object Oriented Programming for PDO use (optional)
Start transferring your traditional mysql into PDO type
and then start learning mysql more in depth
Good Luck!

I need an introduction to MongoDB/NoSQL Database [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 11 years ago.
Improve this question
I use PHP/mySQL/CodeIgniter pretty heavily, writing sql statements to handle/manipulate data. I feel doing all that is primitive, and I've heard good things about MongoDB, schema-less database.
In MySQL, schemas helps me figure out the structure of the model. Usually, I draw out a class diagram with basic things like: id, title, description, date
What blows my mind is, MongoDB seems insanely simple, it's hard to grasp where to begin. From what I hear/read, it doesn't have a schema. How do I know what type will it return?
How do I build my models, how do I add relations between different "tables"?
What is the standard way to add relations and map out data? I've tried playing with it, but wasn't sure what I was doing was the correct way.
I've tried reading manuals and such, but couldn't find a good article helping me transition from mySQL to MongoDB.
Is there anyway I could see comparisons of Models with mySQL and MongoDB? Simple things like CRUD.
How do I start, where do I begin?
You could start here.
How do I build my models, how do I add relations between different "tables"?
Answer:
A non-relational approach is the best path to database solutions which scale horizontally to > many machines.
Answer:
MongoDB stores data in JSON documents (which we serialize to BSON). JSON provides us a rich data model that seamlessly maps to native programming language types, and since its schema-less, makes it much easier to evolve your data model than with a system with enforced schemas such as a RDBMS.
Check also What is NoSQL, how does it work, and what benefits does it provide?, I need an advice about NoSQL/MongoDb and data/models structure and Converting simple MySQL database to a NoSQL solution

PHP / MySQL Query Builder UI with jQuery? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 7 years ago.
Improve this question
So, I need to construct a front-facing query builder for a database, but I'm having trouble finding existing code for what I can't imagine is a rare requirement.
Basically, I have non-SQL fluent people needing to build queries on the fly and view the results.
I found this: http://plugins.jquery.com/project/SQL_QUERY_BUILDER (Demo: http://ksistem.com/jquery/sqlbuilderdemo.htm)
But it requires that the database schema be hardcoded, rather than be dynamically generated.
Is there a better solution that dyamically pulls the database schema into a jQuery-like UI for building and executing SELECT queries against a MySQL database?
Active Query Builder ASP.NET Edition's MS Access-like user interface is based on jQuery. It's a commercial component for ASP.NET 2.0 and higher. It can build SELECT statements for MySQL server.
Product page: http://www.activequerybuilder.com/product_asp.html
Demo: http://aspquerybuilder.net
Dynamically creating the database schema might be a problem, because of the relations between tables. You could let the users carry the burden of selecting the relations but if i look at the "non-SQL fluent people" around me.. i'd probably not do that..
Other than that, if you want to create/check the structure on the fly, you practically have to create/check it every time the script is called. Generating a maybe huge, not necessary, overhead. I`d probably rather have a script/db-admin generate/update the structure every time someone twiddles with the database.
I doubt that you'll find something that does exactly what your looking for.
Maybe pulling the structure out of a YAML file that's kept up to date by the db-admin might be better a better solution.

looking for a db abstraction/substitute that actually works [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 8 years ago.
Improve this question
i am looking for a form of data storage that will answer a few requirements. i realize these requirements are non-standard, and for now i'm using activerecord and ORM solutions like everyone else, but this is my "holy grail" - if you know of anything like this, i would be eternally grateful:
pure PHP
multiple repositories, preferably file based for portability, where i can instantiate by telling it "use repository [X]" - i don't want to pre-create repository [X], if i reference it, it exists.
zero database configuration - i don't want to create tables or export SQL dumps, if it's referenced in my code, it needs to be in the database, auto-created without any fuss, my code is my schema
hierarchical, not relational, ideal structure would be just a freeform, schema-less XML, but since XML performs horribly with large trees, it can't simply be an XML file.
i have experimented with flat XML storage (with xpath and xquery) but it gags on a mid-sized repository, and cripples the application.
i have also experimented with key=>value pairs dropped into a SQLite database with a single generic table, but that gags even faster, and re-forming even the simplest record from key=>value pairs is a performance decimator.
finally, i experimented with lucene as implemented in the zend framework, which was pretty close to ideal, apart from the no-update part.
any ideas, anyone?
I've been having great fun with RedBean, it's not quite designed for flatfiles, but runs on PDO, so it should be relatively easy to write a sqlite module for it. Not sure if it will work for your needs, but definitely worth taking a look at.
Here are some links you may find useful:
txtSQL
Gladius DB
Also, have you considered using Berkeley DB?
Some of the DB extensions listed in the PHP Manual are intended to be used on flat-file like databases.
From your description it seems like PHP arrays should work perfectly:
pure PHP
multiple arrays, file or memory based
your code is your schema
hierarchical
You could use serialize() or var_export() functions to enable file storage.

Categories