Marek Bochenek
Marek Bochenek

Reputation: 1

How do raw query in kohana freamwork

I want make package with list of query.

It is update query like as:

UPDATE table 
SET column = XXX 
WHERE column = XXX AND column2 = XXX;

UPDATE table 
SET column = XXX 
WHERE column = XXX AND column2 = XXX;

UPDATE table 
SET column = XXX 
WHERE column = XXX AND column2 = XXX;

I have these 1000 queries in one package. Now I want make do queries.

I try do it, but unsuccessfully, because every time I get an error like that:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near...

I copy this query and past to phpmyadmin and I do it. These queries work!

I have a variable with prepared queries and I trying to do it in different way.

Database::instance()->query(NULL,$sql);
DB::query(5,$sql)->execute();
DB::query(Database::UPDATE,DB::expr($sql))->execute();
DB::query(Database::UPDATE,$sql)->execute();

but it does not work ;/

Anyone know how do it?

Upvotes: 0

Views: 1780

Answers (1)

yan_kos
yan_kos

Reputation: 36

Just call method query from instance of database:

/** @var Database $db */
$db = Database::instance();
$db->query(Database::UPDATE, 'UPDATE table SET column = XXX WHERE column = XXX AND column2 = XXX;');

But if you want execute multiple SQL statements in one query it's impossible out of box. By default Kohana use old mysql API and mysql_query do not support multiple queries in a single call.

It you want use multiple SQL statements i know 3 way:

  1. Some times ago for Kohana i saw module for mysqli support, you can try to find him and modify: add to it new method which will be use http://php.net/manual/en/mysqli.multi-query.php mysqli::multi_query can execute multiple SQL statements in one query.
  2. Or you can switch to using PDO. For this you must make changes in your database config file, according Kohana's documentation, add map for columns in models.
  3. Or create new PDO connection in set up it only for this query (if you don't use transaction it will be more easy, than variant 2).

Also for variant 2 and 3 you must set up some PDO settings: https://stackoverflow.com/a/6461110/419665

P.S. For call database instance by config name:

Database::instance('config_name');

Upvotes: 1

Related Questions