PHP code example of jublonet / monty

1. Go to this page and download the library: Download jublonet/monty 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/ */

    

jublonet / monty example snippets



// load monty
SQL connector
$connector = Monty::getConnector();

// connect to a database
$connector->open('youruser', 'fancypass', 'holydatabase');

// not running the database on localhost? add a 4th parameter like this:
// $db->open('youruser', 'fancypass', 'holydatabase', 'pentagon.example.com');

// need a custom port number? add a 5th parameter like this:
// $db->open(
//   'youruser', 'fancypass', 'holydatabase',
//   'pentagon.example.com', 3307
// );

// want a persistent connection? add a 6th parameter like this:
// $db->open(
//   'youruser', 'fancypass', 'holydatabase',
//   'pentagon.example.com', 3307, MONTY_OPEN_PERSISTENT
// );

// now there's two operation modes:
// the EASY one first
$table = $connector->table('themaintable');

// want multiple tables?
// $table->add('anothertable');

// set a condition
$table->where('field', '=', 'value');

// there are some shortcuts, like this one:
// $table->eq('field', 'value');

// switching to DISTINCT results is possible, too:
// $table->select(MONTY_SELECT_DISTINCT);

// you might also want to use ands/ors
// $table->or(
//   $table->eq('field1', 'value1'),
//   $table->like('field2', 'value2')
// );
// equals:
// ... WHERE field1 = "value1" OR field2 LIKE "value2"

// peek at the generated sql code without executing it
echo $table->sql() . '<br />';

// loop through the results and display them
for($i = 0; $i < $table->rows(); $i++) {
  $row_array = $table->next();
  echo $row_array['field'] . ' = ' . $row_array['value'] . '<br />';
}

// you could also have got an object instead, like this:
// $row = $table->next(MONTY_NEXT_OBJECT);
// echo $row->field;

// for setting the object return type as default, put this statement
// at the top of your code:
// $table->setReturnType(MONTY_ALL_OBJECT);


// you can also run raw SQL like this (the nerd mode):
$connector->query('SELECT * FROM themaintable WHERE field = "value"');
echo $connector->rows();

// check if a certain table exists at the moment:
if ($connector->tableExists('the_table')) {
  // do something
}

// update values
$values = [
  'column1' => 'Test',
  'column2' => 12345
];
$table->update($values);

// update a single value
$table->update('column1', 'Test');

// update by using the content of another field
// like: SET column2 = column1
$table->update('column2', ['column1']); // note the array syntax for value