// Connect to database $db = new PDO('mysql:host=localhost;dbname=mydatabase', 'username', 'password'); // Prepare SELECT statement $stmt = $db->prepare("SELECT * FROM mytable WHERE id = :id"); // Bind parameters and execute query $stmt->bindParam(':id', $id); $stmt->execute(); // Fetch data $data = $stmt->fetchAll(PDO::FETCH_ASSOC);
// Configure database connection $config = new \Doctrine\DBAL\Configuration(); $connectionParams = array( 'dbname' => 'mydatabase', 'user' => 'username', 'password' => 'password', 'host' => 'localhost', 'driver' => 'pdo_mysql', ); $conn = \Doctrine\DBAL\DriverManager::getConnection($connectionParams, $config); // Prepare SELECT statement $stmt = $conn->prepare("SELECT * FROM mytable WHERE id = :id"); // Bind parameters and execute query $stmt->bindValue(':id', $id); $stmt->execute(); // Fetch data $data = $stmt->fetchAll(\PDO::FETCH_ASSOC);In summary, retrieving data from a database in PHP can be done using various libraries and packages such as PDO or Doctrine. The code examples provided show how to execute a SELECT statement, bind parameters, and fetch data from a MySQL database.