I've heard that SQLite can do this (to avoid synchronicity issues in heavy traffic scenarios) is this true? If so how would I do this with PDO in PHP?
Would you be looking for the ATTACH and DETACH sqlite commands? You can call these with a query to any SQLite PDO object.
The commands allow you to attach a separate database file to the current session. An example would be:
$connection->query('ATTACH DATABASE blog_entries.sqlite AS BlogEntries;');
You can then refer to the tables located in the attached database by their name (eg: SELECT * FROM entries) if there is no duplicate tables. If there is a conflict then they need to be namespaced with the database alias (eg: SELECT * FROM BlogEntries.entries)
Reference: SQLite Manual
You can open a DB in memory (I believe the DSN for PDO is sqlite:memory:) and attach the different databases.
Related
I have no CLI access on a server, but I want to run automatic deploy releases and backup/restore data contained by multiple pgsql databases. Is there a way to do this with PDO effortless?
Effortless(ly)
No.
You'd basically have to re-implement pg_dump. This would not be fun.
You can COPY out the tables using pg_copy_from. Unsure if there's a PDO equivalent. This will let you dump the data relatively easily, combined with a query against information_schema to get a table list.
Dumping the table definitions, users, constraints, indexes, etc, however ... good luck.
I am using multiple databases using CodeIgniter Active Records class. I had to disable persistent connection with MySQL, because CodeIgniter Active Records class can't make persistent connections using multiple databases.
I am looking at the load of my database, it seems like it's using most of it's queries calling "change database" and I am not sure if that's a good sign.
How can I optimize this without having to call "change database" all the time?
It's not as user friendly as most of the Active Record commands, but you can call the SQL directly like this:
$query = $this->db->query("SELECT * FROM database_a.some_table");
$query2 = $this->db->query("SELECT * FROM database_b.another_table");
Are you using queries that reference both databases? If not, it's not too difficult to load a new DB instance for the second database. I.e. you'd still use $this->db for the first, but you could have $this->db2 for the second. I honestly have no idea if that would trigger the "change database" command you're talking about, but it would be MUCH more sustainable code, at least. CI could keep its connections to each database open for the duration of the script (not a persistent connection), and it seems your problem would be fixed.
I've never needed multiple mysql databases in a single app, so this is entirely a guess based on what I've seen with, say, one mysql db and another being an sqlite db.
I am using one db file to connect to the database. However, i need to show something else from another db database on this site. I can't use two db connect files. So, how do i go about display one websites content on another website with two different connections.
For one thing, you don't need separate database connections to the same server. Simply specify the database name before each table name in your SQL statements. For example:
SELECT foo, bar FROM db.table;
rather than:
SELECT foo, bar FROM table;
For another, you shouldn't be storing the database connection in a global for just the sort of reason you're running into. I suspect you're using the outdated mysql extension, which uses an implicit DB connection resource whenever one isn't explicit. Switch to PDO and create a simple connection manager class. One big advantage to PDO is it supports prepared statements, which can be safer and more performant than what the mysql extension provides. If you need a PDO tutorial, try "Writing MySQL Scripts with PHP and PDO"
If it is a separate db server you might need to look into the mysql federated engine which allows you to link to databases on separate servers as if they were on the same host.
I have a site under drupal (using mysql) but need it to read from a external sql database and make quires/reports, maybe using views2?
Is is at all possible? I been looking for a solution to just read (not import, its thousands of entries...) from an external database for hours and not sure if its even possible.
Drupal can access external databases, assuming that they are one of the types Drupal supports.
I will refer you to this handbook page: How to connect to multiple databases within Drupal.
However, to actually access data in the external database, you will need to write custom code, probably in a custom module. Essentially, you will need to do something like:
// Set Database API to use the other database.
db_set_active('external_db');
// Query the database.
db_query("SELECT * FROM {your_table} WHERE condition = 'value'");
// Set the Database API back to the default db.
db_set_active('default');
Essentially, point the database to the external database, make your reads and writes, and switch back. If you forget to switch back, Drupal will crash as it's core functions will try to work with the non-Drupal database.
It's possible to define several database connections in your settings.php and use db_set_active to select which database to use.
If you want to do this with views, you might need to do some extra work and embed the view after setting the active database to the external one.
Without writing code, you should be able to create views of the secondary database using the Table Wizard module. However, given how Drupal 6 handles its database abstraction, this will require that the secondary database be the same type as the main Drupal database (eg, either both are MySQL or both are Postgresql).
I have a working PHP server. Now I want to use databases (MySQL or something similar). Is it possible to create a database from PHP?
I would like to emphasize that in my case I do not have any user-name and password which I can use to connect to MySQL server. I also do not have a control-panel where I could create a database or a table in an existing database.
ADDED:
I think SQLite is what I need. But if I try to create a new database from PHP I see that PHP tries to create a file in a directory which is different from the directory where my files are supposed to be. Then it reports that it is unable to create the database and I think it is because it tries to create in the directory to which I have no permission to write. Can I force PHP to create SQLite in a specific directory?
I'm assuming you want to create an SQL database without access to a stand-alone database server. You could use SQLite, which is a library that creates a lightweight database in a single file without separate processes.
It's not quite as efficient as a standalone database server, however, so if you need performance, use a proper database server. It's not an unreasonable requirement for a high-performance web app.
Mysql,no. SqlLite is a possibility, you only need write permissions on the filesystem.
www.sqlite.org/
I would like to emphasize that in my
case I do not have any user-name and
password to which I can use to connect
to MySQL server. I also do not have a
control-panel where I could create a
database or a table in an existing
database.
This question is confusing to me, because if you're using MySQL you should be able to create a database, with a username and password to connect, using their command line admin tool. That's also the way to create a database or a table as well.
Are you saying you don't have access to the admin tool? You don't have an account? If so, you'll need to ask for them from the person who does have such access.
An option is to set up an SQLite database in a directory outside of your htdocs folder. This is so people can't type in the name of the database file and download the entire database, severely compromising security.
If you are so inclined, you can even set up a layer of abstraction between PHP and the DBMS using PDO. Then, in order to create the database object, you would specify a DSN specific to SQLite write something like this:
$pdo_obj = new PDO('sqlite:/path/to/my_database.sqlite3');
and then query it like a normal PDO object using the PDO functions.
This method would better allow for easier migration to using a client-server DBMS once you can get one set up; it would just be a matter of copying the table structure and data over to the new database, and changing the DSN to something appropriate.
Creating a database through PHP is possible, but for that you need a connection to the database, which needs a username/password pair or some kind of authentication. Unless your database allows anonym logins or something similar, it's not possible.
Yes, just fire a create database sql query from PHP
http://dev.mysql.com/doc/refman/5.0/en/create-database.html