$con = mysqli_connect("localhost", "username", "password", "database_name"); if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); exit(); } $query = "SELECT * FROM users"; $result = mysqli_query($con, $query); if (!$result) { die('Error: ' . mysqli_error($con)); } while($row = mysqli_fetch_array($result)) { echo $row['username'] . '
'; } mysqli_close($con);
$dbhost = 'localhost'; $dbname = 'database_name'; $dbuser = 'username'; $dbpass = 'password'; try { $conn = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass); $stmt = $conn->prepare("INSERT INTO users (username, email, password) VALUES (:username, :email, :password)"); $stmt->bindParam(':username', $username); $stmt->bindParam(':email', $email); $stmt->bindParam(':password', $password); $username = 'john'; $email = 'john@example.com'; $password = 'password123'; $stmt->execute(); echo "New record created successfully"; } catch(PDOException $e) { echo "Error: " . $e->getMessage(); } $conn = null;Both examples are using PHP to connect to a MySQL database and retrieve or insert data using different packages/libraries: MySQLi and PDO.