We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

How to create DATABASE with phalcon

Hello friends, I am trying to create an open source project with phalcon. I am trying to create a database with phalcon codes like wordpress and drupal, that how on installation page they create even database something like 'CREATE DATABASE IF NOT EXISTS database' or like that. So when I try to fire code

$this->db->execute('CREATE DATABASE IF NOT EXISTS database');

its not working so can anyone tell me how can I create database within phalcon controller?

Thank you.

See point #2 > https://codex.wordpress.org/Installing_WordPress#Famous_5-Minute_Install

But I don't know why Wordpress asks for manual database creation like that.

But there is also automatic wp installation which creates wordpress database and installs it. Thtat is what I wanna do with Phalcon. ;) Thanks for help.

See point #2 > https://codex.wordpress.org/Installing_WordPress#Famous_5-Minute_Install

But I don't know why Wordpress asks for manual database creation like that.

edited Dec '14

Just tried this in a controller, it worked, database can be seen from PhpMyAdmn

$re = $this->db->query("CREATE DATABASE `testtest`");
if ($re) {
    echo 'CREATED';
} else {
    echo 'COULD NOT CREATE';
}

But this will not:

$query = new Phalcon\Mvc\Model\Query("CREATE DATABASE `testtest2`");
$result = $query->execute();

With error:

CREATEDSyntax error, unexpected token IDENTIFIER(CREATE), near to ' DATABASE testtest2', when parsing: CREATE DATABASE testtest2 (27)

Okay, I will try this caz I was doing execute insted of query. ;) thanx for heads up @Edward

Just tried this in a controller, it worked, database can be seen from PhpMyAdmn

$re = $this->db->query("CREATE DATABASE `testtest`");
if ($re) {
   echo 'CREATED';
} else {
   echo 'COULD NOT CREATE';
}

But this will not:

$query = new Phalcon\Mvc\Model\Query("CREATE DATABASE `testtest2`");
$result = $query->execute();

With error:

CREATEDSyntax error, unexpected token IDENTIFIER(CREATE), near to ' DATABASE testtest2', when parsing: CREATE DATABASE testtest2 (27)