Quick links: Content - sections - sub sections
EN

Trace: database-config

Wiki: Sitemap - Recent Changes - Back link

Before going further with code, you have to configure Jelix to be able to access a database, and feed this database a bit. We will indeed store our news in a table.

Jelix can handle mysql, postgresql, sqlite and pdo. Through its driver system, it is possible to add other types of databases (if you don't want to use pdo).

Access configuration file

The access to a database is configured in the news.org/var/config/dbprofils.ini.php file. In this file, you can specify one or more connection profiles, then one or more connections.

Each profile has a name and are specified like this :

[ProfileName]
driver="mysql"    ; name of the driver to use
database="foo"    ; name of the database to use
host= "localhost" ; hostname or ip to database server
user= "john"      ; connection user
password="doo"    ; password
persistent= on    ; indicating if the connection is persistent
force_encoding = off;

Except the driver parameter which is mandatory, the other parameters depends on the driver which are used. In general, you will have the database, host, user and password parameters.

Now change the content of this file according to your configuration. We will take as profile name “news.org” for example, and we will define this profile as the default one by specifying it with the “default” parameter.

default = news.org
 
[news.org]
driver="mysql"
database="news"  
host= "localhost"
user= "admin"   
password="news"
persistent= on 

Note: It is indeed sometimes interesting to have several entry points, then several settings sharing the same connection profiles file, or that each one has its own connections file. If you want to have different configuration files for the database access, create other ini files, and push their name in the dbProfils option in the configuration file of the entry point.

 dbProfils = other_dbprofils.ini.php

Creation of the table

In the “news” database, we will now create a “news” table. Execute this sql script (adapt it if you are not using mysql):

CREATE TABLE `news` (
`news_id` INT NOT NULL AUTO_INCREMENT ,
`subject` VARCHAR( 255 ) NOT NULL ,
`text` TEXT NOT NULL ,
`news_date` DATE NOT NULL ,
PRIMARY KEY ( `news_id` )
);

And feed this table with two news (we will make a form later).

INSERT INTO `news` VALUES (1, 'first news', 'This is a first news. In commodo, neque sit amet laoreet accumsan, neque velit rutrum augue, a fringilla nibh lorem nec est. Cras eleifend eros. Sed vehicula. Donec vel enim at nunc tincidunt pellentesque. Donec malesuada. Praesent volutpat orci ut leo. Donec dictum tortor quis odio. Aliquam pulvinar justo eu eros.', '2006-01-15');
 
INSERT INTO `news` VALUES (2, 'Lorem Ipsum', 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Suspendisse enim elit, luctus in, rhoncus quis, facilisis in, nulla. Nam eu dolor vel erat semper porta. Phasellus pellentesque nulla a urna. Phasellus nonummy diam id risus. Donec faucibus mi sed nisi. Sed et lectus at ligula scelerisque tempus. Proin justo nibh, consectetuer porta, accumsan ac, consectetuer id, dui. Morbi at mi auctor urna elementum convallis. Etiam et massa porta risus imperdiet ullamcorper. Aenean a metus at tortor ultrices accumsan. Mauris luctus.', '0000-00-00');

We are now ready to use this data in our application.

en/tutorials/main/database-config.txt · Last modified: 2008/11/19 21:42 by laurent
Recent changes RSS feed Creative Commons License