Skip to content

berry.mysql.MySQLCommand

Nikos Siatras edited this page Oct 1, 2022 · 12 revisions

Description

public function __construct(MySQLConnection $mySQLConnection, String $query = "")

MySQLCommand represents a Transact-SQL statement or stored procedure to execute against a MySQL Server database.

Examples

The following example creates a MySQLConnection, a MySQLCommand, and a MySQLDataReader. The example reads through the data, printing it to the HTML output. Finally, the example closes the MySQLReader and then the MySQLConnection as it exits.

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, Grade FROM students WHERE Grade > ?";
$command = new MySQLCommand($connection, $sql);
$command->Parameters->setDouble(1, 5.5);
$reader = $command->ExecuteReader();
while ($reader->Read())
{
    $studentID = $reader->getValue(0);
    $firstName = $reader->getValue(1);
    $lastName = $reader->getValue(2);
    $grade = $reader->getValue(3);

    echo $firstName . " " . $lastName . " grade is " . $grade;
}
$reader->Close();

$connection->Close();