Procedural vs. Object-Oriented Style Programming


#1

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','tyson@gmail.com','1457 North Ave','Los Cabos','WA')";
            
            $conn->exec($sql);
            $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…

{
“first_name”:“Aaron”,
“last_name”:“Tyson”,
“phone”:“010-123-4567”,
“email”:"tyson@example.com",
“address”:“4523 Evergreen Ave”,
“city”:“city”,
“state”:“state”
}

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(':first_name',$first_name);
        $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);

        $stmt->execute();
        echo '{"NOTICE!":{"text": "Customer Added"}}';

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

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

<?php
    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{}