I was reading this question:
The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead
And it got me thinking that I should make the change from mysql to mysqli. It is a one character change in CodeIgniter so it isn't very hard :)
But is there anything I should look out for to spot any errors that can happen? Are there certain queries that are treated differently?
Are there certain queries that are treated differently?
No.
The MySQL and MySQLi extension are “drivers” that take care of the communication between PHP and the MySQL database server;
they do not change the range of SQL commands that the MySQL server understands.
So as long as the DB abstraction layer takes care of what PHP functions are to use for what purpose for you (and a framework like CI should most certainly do that), there is nothing to worry about in regard to the actual queries.
Related
I have a database PHP application that can work with MySQL, PostgreSQL and SQLite. The application uses PDO and assumes the database content is Unicode.
I am adding the support for MS SQL SERVER using the pdo_sqlsrv driver and the only solution I have found is to modify all the queries (it's really a lot of work) adding the N prefix in front of the strings for the INSERT and UPDATE queries.
Is there any smarter solution to insert unicode by default? Like a PDO or SQL SERVER setting?
Since all the queries are executed by a function (it's my own layer over PDO), I have also thought about adding a regular expression that modifies the queries before the execution but I think it's quite tricky.
Thanks.
After you set up your connection to the DB you need to add the PDO Attributes:
$mssql_connection->setAttribute(PDO::SQLSRV_ATTR_ENCODING, PDO::SQLSRV_ENCODING_UTF8);
Reference:
https://learn.microsoft.com/en-us/sql/connect/php/pdostatement-setattribute?view=sql-server-2017
Can I somehow detect how much MySQL queries performed via old mysql and how much via mysqli connections.
My basic work is recode, refactor and support old projects. I migrate from procedural to OOP style and from mysql to mysqli. I need to know how much old requests left from old coders (code is messy and crazy as usual to calculate this in code directly).
Thanks!
mysqli has http://php.net/manual/en/mysqli.get-connection-stats.php
and for any driver you can run show status like 'Com\_%'; query.
I see no point in such numbers though, for the task provided.
You can override both methods with override_function and log whatever you need.
I'm creating a tiny activerecord library using PDO and I'm planning to support MySQL, Sqlite and PgSQL.
My question is how I can be sure that the query string works with all adapters? There will mostly be CRUD statements with some joins etc. Is there a standard I can follow that works for all of these?
Thanks
/ Tobias
EDIT: Thanks for all your answers but my question was more about the SQL 'syntax' differences between them.
If you want to write your own DB layer, I'd suggest you:
Use placeholders, if you aren't already. They add security too.
Use bindParam/bindValue with value type (e.g. BOOLEANS don't exist in SQLite but work if bound with PARAM_BOOL)...
Use stored procedures from MySQL, create matching names in PostgreSQL, and define them in SQLite with sqliteCreateAggregate/sqliteCreateFunction.
Do all parameter checking in PHP, because SQLite won't do any (e.g. validate date variables)...
Use InnoDB for MySQL to get transactions.
Note: By supporting these vastly different RDBMs, you're demoting the database to just a data store. Keep in mind that SQLite is very limited. It does not have native data types save from number/string. E.g. it's missing date handling and intervals, and so on. All three databases support transactions, which are essential for data integrity when the integrity is maintained outside the DB.
Edit: Removed mention of MySQL triggers, which are availabe for 5.0.
Here you have a simple introduction to zend_db_adapter - i think you want something similar (I posted this just as a example to see how others resolve the problem you have)
My choice for this kind of issues would be ADOdb. While I never actually used it with PostgreSQL, it just saved my sanity in a project that happened to be born with MySQL and then migrated to SQL Server, to SQLite and back to SQL Server.
I've pondered this for a long time, but I've never heard an explanation.
I'm guessing that either MySQL's C API is simply not constructed to hand over data in its original type (which sounds like a design flaw), or the original mysql driver (replaced by the mysqli driver) was simply poorly designed and the mysqli driver was trying to be backwards compatible.
I understand PostgreSQL casts types in PHP, for the most part.. why doesn't MySQL? Is it a common practice in other languages and RDBMSes to return all values as a string?
mysql is old. mysqli is better ('I' stands for 'improved' of course), PDO outclasses all, and at least knows about bin/int/char differences.
I think that the original MySQL driver was written around 2000/2001 (maybe earlier?). The PHP Data Objects (PDO) class wasn't written until 2004 -- however this goes to show you how much ground PHP has covered in a short amount of time.
When it originated (PHP) it wasn't necessarily well thought out or planned at all, however they've been sorting things out since then.
That and the fact that PHP isn't a strongly typed programming language might explain why...
Different databases have slight variations in their implementations of SQL. Does PDO handle this?
If I write an SQL query that I use with PDO to access a MySQL database, and later tell PDO to start using a different type of database, will the query stop working? Or will PDO 'convert' the query so that it continues to work?
If PDO does not do this, are there any PHP libraries that allow me to write SQL according to a particular syntax, and then the library will handle converting the SQL so that it will run on different databases?
From PHP manual :
PDO provides a data-access abstraction
layer, which means that, regardless of
which database you're using, you use
the same functions to issue queries
and fetch data. PDO does not provide a
database abstraction; it doesn't
rewrite SQL or emulate missing
features. You should use a
full-blown abstraction layer if you
need that facility.
So,you can not change the database and expect that everything works as before. It depends on the queries you have used. Are they "simple" SQL92 queries or do they use special features for a specific db...
Ex a mysql query with "LIMIT 10,20" must be rewritting to work with an Oracle DB or Sqlite. They use "LIMIT 20 OFFSET 10"
PHP doesn't have libraries that will automatically convert SQL for you. If you want that kind of functionality you should look at an ORM implementation like Doctrine. There is a price to pay of course, since there is a learning curve involved in using it in your project, plus writting SQL stops being as simple as churning out a string. You should ask yourself if you absolutely positively need code that's database independent.