Procedural vs. Object-Oriented Style Programming


I have stuck on php server programming… By any chance, is anybody have worked on this before and maybe share some advice or suggestion?

The first code shown below is a fragment of my simple procedural-style program version, which was executed successfully on PHP server by inserting a new data into MySQL database…

    require '../vendor/autoload.php';
    $servername = "xxxxxx";
    $username = "xxxx";
    $password = "xxxx";
    $dbname = "xxxxxx";

    try {
            $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
            $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            $sql = "INSERT INTO customers (first_name, last_name, phone, email, address, city, state) VALUES
            ('Aaron', 'Mayer','666-124-8888','','1457 North Ave','Los Cabos','WA')";
            $last_id = $conn->lastInsertId();
            echo "New record created successfully. Last inserted ID is: " . $last_id;
    catch(PDOException $e)
            echo $sql . "<br>" . $e->getMessage();

    $conn = null;

However, any attempt to insert a new customer data following the object-oriented style fails. And I’m not sure why. Here is the code that supposed to work, but it doesn’t.

This is jason-content data entry I was trying to insert…

“address”:“4523 Evergreen Ave”,

I’d prefer this code to work for my app because it allows me build a RESTfull API/mySQL with multiple routes using function handlers like get, post, delete, ect. The db variable in this code is an instance or object of the class db, which contains all properties and attributes in the separate code pasted below.

$app->post('/api/newcustomer/add', function (Request $request, Response $response) {
    $first_name = $request->getParams('first_name');
    $last_name = $request->getParams('last_name');
    $phone = $request->getParams('phone');
    $email = $request->getParams('email');
    $address = $request->getParams('address');
    $city = $request->getParams('city');
    $state = $request->getParams('state');
    $sql = "INSERT INTO customers (first_name, last_name, phone, email, address, city, state) VALUES
    (:first_name, :last_name, :phone, :email, :address, :city, :state)";
    try {
        $db = new db();
        $db = $db->connect();

        $stmt = $db->prepare($sql);
        $stmt->bindParam(':last_name', $last_name);
        $stmt->bindParam(':phone',     $phone);
        $stmt->bindParam(':email',     $email);
        $stmt->bindParam(':address',   $address);
        $stmt->bindParam(':city',      $city);
        $stmt->bindParam(':state',     $state);

        echo '{"NOTICE!":{"text": "Customer Added"}}';

    catch(PDOException $e)
        echo "Connection failed: " . $e->getMessage();

//This code saved in the separate php file...

    class db{
    //Connect to database
    public function connect(){
        $conn = new PDO("mysql:host=xxxxx;dbname=xxxxx", "xxxx", "xxxxx");
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        return $conn;

I’m using Restlet Client Chrome Extension to verify this code with POST handler and it gives me the 500 Internal Server Error… The connection with the database has been established successfully through other routes with an instantiation of the same class constructor db{}