Skip to content

Instantly share code, notes, and snippets.

@rhengles
Created November 26, 2024 18:52
Show Gist options
  • Save rhengles/e39cbf0df74babefdeda56bb59d03fde to your computer and use it in GitHub Desktop.
Save rhengles/e39cbf0df74babefdeda56bb59d03fde to your computer and use it in GitHub Desktop.
PHP PDO mysql access
<?php
$db = new PDO('mysql:host=localhost;dbname=your_database_name', 'your_username', 'your_password');
$query = "INSERT INTO clients (name, email, phone) VALUES (:name, :email, :phone)";
$stmt = $db->prepare($query);
$stmt->bindValue(':name', $args['input']['name']);
$stmt->bindValue(':email', $args['input']['email']);
$stmt->bindValue(':phone', $args['input']['phone']);
$stmt->execute();
$clientId = $db->lastInsertId();
$query = "SELECT * FROM clients WHERE id = :id";
$stmt = $db->prepare($query);
$stmt->bindValue(':id', $clientId);
$stmt->execute();
$client = $stmt->fetch(PDO::FETCH_ASSOC);
return $client;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment