Created
May 28, 2025 23:43
-
-
Save odahcam/5ae22e27aebc77c838f71aa83c2f3949 to your computer and use it in GitHub Desktop.
AMP MySQL vs. PDO MySQL comparison
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once __DIR__ . '/vendor/autoload.php'; | |
use Amp\Mysql\MysqlConfig; | |
use Amp\Mysql\MysqlConnectionPool; | |
$config = MysqlConfig::fromString( | |
"host=localhost user=myuser password=mysecret db=mydatabase" | |
); | |
$pool = new MysqlConnectionPool($config); | |
$statement = $pool->prepare("SELECT * FROM my_table"); | |
$result = $statement->execute(); | |
foreach ($result as $row) { | |
// $row is an associative-array of column values, e.g.: $row['column_name'] | |
print_r($row); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once __DIR__ . '/vendor/autoload.php'; | |
try { | |
$dsn = "mysql:host=127.0.0.1;port=3306;dbname=mydatabase;charset=utf8mb4"; | |
$username = "myuser"; | |
$password = "mysecret"; | |
$pdo = new PDO($dsn, $username, $password, [ | |
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, | |
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, | |
PDO::ATTR_EMULATE_PREPARES => false | |
]); | |
$statement = $pdo->prepare("SELECT * FROM my_table"); | |
$statement->execute(); | |
while ($row = $statement->fetch()) { | |
// $row is an associative-array of column values, e.g.: $row['column_name'] | |
print_r($row); | |
} | |
} catch (PDOException $e) { | |
echo "Connection failed: " . $e->getMessage(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment