try { $pdo = new PDO('mysql:host=localhost;dbname=my_database', 'my_username', 'my_password'); $statement = $pdo->prepare('SELECT * FROM my_table WHERE id = :id'); $statement->bindValue(':id', 1, PDO::PARAM_INT); $statement->execute(); $result = $statement->fetch(PDO::FETCH_ASSOC); print_r($result); } catch (PDOException $e) { echo 'Error: ' . $e->getMessage(); }
$mysqli = new mysqli('localhost', 'my_username', 'my_password', 'my_database'); if ($mysqli->connect_errno) { echo 'Failed to connect to MySQL: ' . $mysqli->connect_error; exit(); } $result = $mysqli->query('SELECT * FROM my_table WHERE id = 1'); $row = $result->fetch_assoc(); print_r($row); $mysqli->close();
use Doctrine\DBAL\DriverManager; $connectionParams = array( 'dbname' => 'my_database', 'user' => 'my_username', 'password' => 'my_password', 'host' => 'localhost', 'driver' => 'pdo_mysql', ); $conn = DriverManager::getConnection($connectionParams); $sql = 'SELECT * FROM my_table WHERE id = ?'; $params = array(1); $result = $conn->executeQuery($sql, $params); $row = $result->fetchAssoc(); print_r($row);This code uses Doctrine DBAL to connect to a MySQL database, retrieve a single row from a table with an ID of 1, and then print the result.