Brutally Roll Your Own Backend – Part 4

lamp-stack (1)

Brutally Roll Your Own is a video series on building a PUSH enabled mobile application from scratch, using a Linux, Apache, MySQL, PHP (LAMP) server for the back end, and Delphi for the mobile app. In Part-4 we begin building a reusable php endpoint script to publish our data.

Sign up with linode here


Sign up for Linode by clicking here!

If you select linode to host your back end, as I suggest in part 2 of this video series, please consider signing up through my referral link above. I host my servers (including this site) on linode, and this will help pay the bills! If you already have an account, you can enter this referral code: fb41cc68526abce4ffa14bfd797d5c4cc684f192  Your support is appreciated!

[Listing of all parts]

Print Friendly, PDF & Email
Facebooktwitterredditpinterestlinkedintumblrmail

7 thoughts on “Brutally Roll Your Own Backend – Part 4”

  1. Hello Craig,
    I figured it out , problem with Delphi 10.4 IDE Rest Debugger . I uses Postman and worked. I will move to the next Videos.

    Best regards,

    Mohamed

  2. ———————————–crud.php———————–
    ?php

    class CRUD {

    function doRead() {

    return “[GET] Not implemented.”;

    };

    function doCreate() {

    return “[POST] Not implemented.” ;

    };

    function doUpdate() {

    return “[PUT] Not implemented.” ;

    };

    function doDelete() {

    return “[DELETE] Not implemented.” ;

    };

    function getResponse() {

    $method = $_SERVER[‘REQUEST_METHOD’] ;

    if ( $method == ‘GET’) {

    return $this->doRead() ;
    };

    if ( $method == “POST”) {

    return $this->doCreate() ;
    } ;

    if ( $method == ‘PUT’) {

    return $this->doUpdate() ;
    };

    if ( $method == ‘DELETE’) {

    return $this->doDelete() ;

    } ;

    };

    }
    ?>

  3. Hi Craig,
    Thanks for the series , Following is the script , I have proplem with post . it is not inserting into regions :

    table = $table ;
    $this->keyField = $keyField ;

    }

    function doRead() {
    global $conn;
    $sql = “select * from ” . $this->table . “;” ;
    $stmt = $conn->prepare($sql);
    $stmt->execute();
    $results = $stmt->fetchAll(PDO::FETCH_ASSOC) ;
    return json_encode($results) ;
    }

    function doCreate() {
    global $conn;

    $inserted = array() ;

    $data = json_decode(file_get_contents(“php://input”)) ;

    foreach($data as $entry) {

    $paramstr = “” ;
    $fieldstr = “” ;
    $first = true ;
    foreach( $entry as $key => $value ) {

    if ($first) {

    $fieldstr = $fieldstr . $key ;
    $paramstr = $paramstr . “:$key” ;
    $first = false ;

    } else {

    $fieldstr = $fieldstr . “,$key” ;
    $paramstr = $paramstr . “,:$key” ;

    }

    }
    $sql = “insert into ” . $this->table . “( $fieldstr ) VALUES ( $paramstr ) ;”;

    $stmt = $conn->prepare($sql);

    foreach($entry as $key => $value ) {
    $stmt->bindValue(“:$key”, $value ) ;

    }

    $stmt->execute();

    $inserted[] = $conn->lastInsertId();

    }

    $sql = “insert into ” . $this->table .
    “WHERE” . $this->keyField .
    ” in ( ” . implode( “,” , $inserted ) . “) ;” ;
    $stmt = $conn->prepare($sql);
    $stmt->execute();
    $results = $stmt->fetchAll(PDO::FETCH_ASSOC);
    return json_encode($results);
    }

    }
    ?>

    —————————————–db.php—————-
    setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    } catch (PDOException $e) {
    echo “Connection failed : ” . $e->getMessage();
    die();
    }
    ?>
    ——————————-crud.php————————
    doRead() ;
    };

    if ( $method == “POST”) {

    return $this->doCreate() ;
    } ;

    if ( $method == ‘PUT’) {

    return $this->doUpdate() ;
    };

    if ( $method == ‘DELETE’) {

    return $this->doDelete() ;

    } ;

    };

    }
    ?>
    ———————————–index.php—————-
    getResponse();

    ?>

    ————————————

    Best regards,

    Mohamed

    Best regards,

    Mohamed

  4. Dear Graig,

    I have registered with Linode . I am following along , but got stuck with crudtable script . Can you share the correct scripts.

    Thanks and best regrads,

    Mohamed Aradi

    • This series is several years old, and some things have changed with the google push services in the meanwhile.
      You may not be able to fully follow along at this point.
      I can certainly try to help with mysql/php scripts though, can you give more information about the problem you’re having?

Leave a Comment