PHP code example of jelix / database

1. Go to this page and download the library: Download jelix/database library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

jelix / database example snippets


use \Jelix\Database\AccessParameters;
use \Jelix\Database\Connection;

// parameters to access to a database. they can come from a configuration file or else..
$parameters = array(
  'driver'=>'sqlite3',
  "database"=>"/app/tests/units/tests.sqlite3",
);

// verify content of parameters and prepare them for the Connection object.
$accessParameters = new AccessParameters($parameters, array('charset'=>'UTF-8'));

// then you can retrieve a connector
$db = Connection::create($accessParameters);


// let's insert some values
$insertSql = "INSERT INTO ".$db->encloseName('myValues')." (
     ".$db->encloseName('id'). ",
     ".$db->encloseName('value')."
      ) VALUES ";

$value = 'foo';

// insert one value with a classical query
$db->exec($insertSql." (1, ".$db->quote($value).")");

// insert one value with a prepared query
$stmt = $db->prepare($insertSql."(:id, :val)");
$stmt->bindValue('id', 2, \PDO::PARAM_INT);
$myVar = 'bar';
$stmt->bindParam('value', $myVar, \PDO::PARAM_STR);
$stmt->execute();

// retrieve all records
$resultSet = $db->query("SELECT id, value FROM myValues");

// records are always objects
foreach ($resultSet as $record) {
    echo "id=".$record->id."\n";
    echo "value=".$record->value."\n";
}