jublonet / monty
PHP 中的简单 MySQL/MariaDB 数据库包装器。
2.4.2
2017-09-23 12:11 UTC
This package is not auto-updated.
Last update: 2024-09-25 02:07:37 UTC
README
PHP 中的简单 MySQL/MariaDB 数据库包装器。
版权 (C) 2011-2017 Jublo Solutions support@jublo.net
本程序是自由软件:您可以重新分发和/或修改它,前提是遵守自由软件基金会发布的 GNU 较小通用公共许可证的条款,许可证版本为 3 或(根据您的选择)任何更高版本。
本程序分发时希望它能有用,但没有提供任何保证;甚至没有关于其适销性或特定用途适用性的暗示保证。有关更多详细信息,请参阅 GNU 通用公共许可证。
您应该已经随本程序收到了 GNU 较小通用公共许可证的副本。如果没有,请参阅 https://gnu.ac.cn/licenses/。
要求
- PHP 5.5.0 或更高版本
第一步
// load monty require 'monty/loader.php'; // get the MySQL 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