Skip to content

berry.mysql.MySQLDataReader

Nikos Siatras edited this page Sep 30, 2022 · 4 revisions

Provides a means of reading a forward-only stream of rows from a MySQL database.

 public function __construct($result = null)

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();