Error: SQLSTATE[HY000] [2002] No such file or directory
Here is the code for accessing the database and display the information.
<?php $servername = "localhost"; $username = "username"; $password = "password"; $dbname = "myDBPDO"; // Create connection try { $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password); // set the PDO error mode to exception $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // prepare sql and bind parameters $stmt = $conn->prepare("INSERT INTO MyGuests (firstname, lastname, email) VALUES (:firstname, :lastname, :email)"); $stmt->bindParam(':firstname', $firstname); $stmt->bindParam(':lastname', $lastname); $stmt->bindParam(':email', $email); // insert a row $firstname = "Top"; $lastname = "Cat"; $email = "tc@example.com"; $stmt->execute(); // insert another row $firstname = "Woody"; $lastname = "Woodpecker"; $email = "woody@example.com"; $stmt->execute(); // insert another row $firstname = "Mighty"; $lastname = "Mouse"; $email = "mightym@example.com"; $stmt->execute(); echo "New records created successfully"; } catch(PDOException $e) { echo "Error: " . $e->getMessage(); } $conn = null; ?>