CREATE and DROP TABLE using Zend Adapter - php

I'm trying to do some SQL queries using Zend adapter. The code that I'm trying to use is something like this:
$result = $this->$db->getConnection()->exec('CREATE TABLE TEST');
and I know that $db is set and works properly, because I can run other commands such as
$this->$db->listTables(); or
$result = $this->$db->fetchAssoc("SHOW COLUMNS FROM " .$schema);
As I was reading through Zend documentation, it was mentioned that some database transactions that are not prepared should be used through the first example (e.g. exec("...") ), but apparently I have problems running those.
Any thoughts?

This should work ...
$db = Zend_Db_Table::getDefaultAdapter();
$db->query('CREATE TABLE wolf (tag VARCHAR(9))');

Related

Laravel - Where Condition on Multiple Database

I'm using laravel with multiple DB connection. Oracle and PostgreSQL. I can do query from both of instance.
But the problem is I have to look inside my Oracle DB WHERE NOT EXISTS on my postgreSQL.
This is my current Query :
DB::connection('ora')->table('ora_purch')
->whereExists(function($query)
{
$query->select(DB::connection('pgs')->raw(1))
->from('pg_purch')
->whereRaw('ora_purch.id = pg_purch.id');
})
->get();
From this query, I get error "database pg_purch is not exists". It's just like laravel reads pg_purch as an oracle instance.
I also do simulation with the same query and the same instance (multiple database on postgreSQL only), the query is fine and produces correct data.
Is it possible to makes the laravel reads pg_purch refer to the connection?
Or maybe i've missing something?
Please advise, thank you.
Your code generates query like this:
SELECT * FROM ora_purch WHERE EXISTS (SELECT 1 FROM pg_purch WHERE ora_purch.id = pg_purch.id);
And make it with "ora" connection, ignoring "pgs" connection.
You can't use multiple connections inside one query.
Try to split them:
$ids = DB::connection('pgs')->from('pg_purch')->pluck('id');
$result = DB::connection('ora')->from('ora_purch')->whereIn('id', $ids)->get();

Can I export a Sqlite db using RedBean PHP ORM and Import to MySQL?

I have a simple web app that I've been building using redbean PHP which has a really simple structure with three bean types:
areas
buildings
persons
All relationships are exclusive 1:Many. So, a Person belongs to only 1 Building, and a Building belongs to 1 Area.
Area
BuildingList
PersonList
Currently, I have been developing it using Sqlite3 for ease of development, but I want to move the data to mySQL. I have a lot of data that I've already added.
Is there an easy way to use RedBean to Export ALL beans to the new MySql Database?
I know I can search for a sqlite -> MySQL/MariaDB converter, but I also potentially want to be able to use this in reverse to make migrating the site super easy to move/backup/change DBs.
What I've tried below:
R::setup('sqlite:/' . __DIR__ . '/data/database.db');
R::addDatabase('mysql', $MySqlConn );
$old_datas = R::findAll( 'area' );
R::selectDatabase( 'mysql' );
foreach ($old_datas as $bean) {
$new_area = R::dispense('area');
$new_area->importFrom( $bean );
$id = R::store( $new_area );
var_dump( $new_area ); // shows new data
}
var_dump( R::findAll( 'area' ) ); // returns empty array
I'll preface this by saying that I've not personally tried to do so, but since you can dump an entire database via the sqlite3 console, then my first approach would be to do so, and then try importing that file into MySQL.
Dumping a sqlite3 database from the sqlite console:
sqlite> output database_dump.sql
sqlite> .dump
sqlite> .quit
Importing a .sql file into MySQL (as described here):
mysql> use DATABASE_NAME
mysql> source path/to/file.sql

Lithium PHP framework - How to run db transaction on mysql?

I have been working on an e-commerce website in PHP Lithium framework, actually this one is upgraded from CakePHP, we have to use transaction operation on db in mysql. Just don't know how to do db transaction in PHP Lithium framework.
Since Lithium uses PDO, you can just get the PDO object and call the beginTransaction() method.
$foo = app\models\Foo::create();
$pdo = Connections::get('default')->connection;
$pdo->beginTransaction();
$foo->bar = 'Hello';
$foo->save();
$pdo->commit();
https://github.com/UnionOfRAD/lithium/issues/1004#issuecomment-23690165
http://www.php.net/manual/en/pdo.begintransaction.php
It doesn't seem like it is supported, unfortunately. See source of lithium/data/source/database/adapter/MySql.php.
An alternative may be to manually execute your queries.
Within your model, you can do:
static::connection->read($sql, $placeholders);
Where $sql is your raw SQL like:
$sql = 'SELECT * FROM users WHERE id = {:id}';
and $placeholders (optional) are your placeholders:
$placeholders = [
'id' => 5
];
Using that knowledge, you should be able to set up a transaction.

Symfony2 - Show all tables from database

I would like to retrieve all the tables of my database as a list.
i tried to do a "Show databases" on a query but as i'm not using a class I defined (entity) in symfony it's not working.
And with DQL :
$em = $this->getDoctrine()->getEntityManager();
$query = $em->createQuery(
'show databases');
$result = $query->getResult();
This error :
[Syntax Error] line 0, col 0: Error: Expected SELECT, UPDATE or DELETE, got 'show'
Any idea to help me ?
As mentioned in a different answer, you can use Doctrine\DBAL for that:
/** #type \Doctrine\DBAL\Connection $connection */
$connection = ...;
/** #type \Doctrine\DBAL\Schema\MySqlSchemaManager $sm */
$sm = $connection->getSchemaManager();
And then just list the tables as Array:
var_dump( $sm->listDatabases() );
My 2 cents:
getContainer()->get('doctrine.dbal.default_connection')->getSchemaManager()->listTableNames()
This will give you an array of table names.
i have a couple cases where I need to use complex sql statements/functions that I just couldn't do in DQL. Luckily, Symfony2/doctrine provide a method to grab the current database connection and bypass doctrine entirely.
//get connection
$conn = $this->get('database_connection');
//run a query
$users= $conn->fetchAll('select * from users');
Be very careful when using this method, however. Since you are bypassing doctrine, you need to handle any security concerns like SQL injection yourself.
You can add Doctrine DBAL to your project and it will give you the tools you need. You can list databases, tables from a database, columns from a table etc etc
More in Doctrine DBAL documentation: http://docs.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/schema-manager.html
Doctrine is a ORM, it is not intended to list all the databases. Beside that, usually for the current user you don't have the right to show all databases in the server, this can prove to be a big security breach.
Basicly, doctrine does not know how to interpret your query, you have to use a native query for this: Doctrine Native Query

how to work with multiple database

I am working on one company internal web software and in this I have given forum option for internal employee.Now I have to add second company to forum so that second company employee can also give some comment in forum.I have two database one for my company internal and second is second company database.
Please Help me how can I Implement this???
Always use resource returned by mysql_connect() so you know what database you use
// database 1
$db1 = mysql_connect();
// database 2
$db2 = mysql_connect();
// query db1
mysql_query($query,$db1);
Also try reading the docs about it
It depends on what database you are using..,,
and try to read some mysql function cause theres a function for linking databases..
You should use 2 (3, 4, 5 ...) db connections.
If your bases is not MySql only, you can use PDO extension:
$db1 = new PDO($param1, $param2, $param3);
$db2 = new PDO($param4, $param5, $param6);
And then you can use any of them, e.g.:
$db2->exec();

Categories