setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); echo "Connected successfully"; } catch (PDOException $e) { echo "Connection failed: " . $e->getMessage(); }
setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $stmt = $db->query('SELECT * FROM users'); $result = $stmt->fetchAll(PDO::FETCH_ASSOC); foreach ($result as $row) { echo $row['username'] . ' - ' . $row['email'] . "
"; } } catch (PDOException $e) { echo "Connection failed: " . $e->getMessage(); }
setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $stmt = $db->prepare("INSERT INTO users (username, email) VALUES (:username, :email)"); $stmt->bindParam(':username', $username); $stmt->bindParam(':email', $email); // insert a record $username = 'johndoe'; $email = 'johndoe@example.com'; $stmt->execute(); echo "Record inserted successfully"; } catch (PDOException $e) { echo "Connection failed: " . $e->getMessage(); }The PDO library is included in the default PHP installation, and you don't need to install any additional package or library.