-
Notifications
You must be signed in to change notification settings - Fork 0
berry.mysql
Nikos Siatras edited this page Oct 1, 2022
·
33 revisions
Every modern web application interacts with a database. PHP-Berry makes interacting with MySQL and MariaDB databases extremely simple using raw SQL.
The berry.mysql package includes all necessary tools to read, write and update data to a MySQL Server.
PHP-Berry does not need any special configuration to connect to a database. All you need to be able to exchange data with a database is to initialize a connection. The following example shows how you can initialize a MySQL/MariaDB connection
require_once(__DIR__ . "/berry/mysql.php"); // Include php-berry mysql package
// Establish a connection with MySQL server
$connection = new MySQLConnection('localhost', "database_name", "username", "password", "utf8");
require_once(__DIR__ . "/berry/mysql.php"); // Include php-berry mysql package
// Establish a connection with MySQL server
$connection = new MySQLConnection('localhost', "database_name", "username", "password", "utf8");
$sql = 'INSERT INTO `Employees` (`FirstName`,`LastName`,`Email`,`PhoneNumber`,`Salary`) VALUES (?,?,?,?,?)';
$command = new MySQLCommand($connection, $sql);
$command->Parameters->setString(1, "Nikos");
$command->Parameters->setString(2, "Siatras");
$command->Parameters->setString(3, "[email protected]");
$command->Parameters->setInteger(4, 2100000000);
$command->Parameters->setDouble(5, 1000);
$command->ExecuteQuery();
$recordID = $command->$command->getLastInsertID(); // This returns the Auto Increment ID
echo 'New employee inserted. Record ID is ' . $recordID . '<br>';
$connection->Close();
require_once(__DIR__ . "/berry/mysql.php"); // Include php-berry mysql package
// Establish a connection with MySQL server
$connection = new MySQLConnection('localhost', "database_name", "username", "password", "utf8");
$sql = "SELECT `ID`,`FirstName`,`LastName`,`Salary` FROM `Employees` WHERE `Saralary`> ?";
$command = new MySQLCommand($connection, $sql);
$command->Parameters->setDouble(1, 1000);
$reader = $command->ExecuteReader();
while ($reader->Read())
{
$employeeID = $reader->getValue(0);
$firstName = $reader->getValue(1);
$lastName = $reader->getValue(2);
$salary = $reader->getValue(3);
echo $firstName . " " . $lastName . " salary is " . $salary;
}
$reader->Close();