PDO MySQL Get Data

Main Screen

MySQL Get PDO connection:

        
            // Place at the top of the PHP File.
            require "./MySQL_Config.php";
            try {
            $pdo = new PDO($params["dsn"], $params["username"],
            $params["password"], $params["options"]);
            } catch (\PDOException$e) {
            throw new \PDOException($e->getMessage(), (int) $e->getCode());
            }
        

    

Simple Select No-Parameters:

        
            $stmt = $pdo->query('SELECT distinct lastName from users;');
            foreach ($stmt as $row) {
            echo $row['lastName'] . "\n";
            }
            $pdo = null;
        
    

Simple Select - Into a PHP Class:

        
            class User
            {
                public $id;
                public $lastName;
                public $firstName;
                public $gender;
                public $city;
                public $state;
                public $lastModified;
            }
            $sql = "Select * from users limit 5;";
            $users = $pdo->query($sql)->fetchAll(PDO::FETCH_CLASS, 'User');
            //echo $users[0]->lastModified;
            $data = json_encode($users);
            echo $data;
        
    

Simple Select using LIKE Keyword:

        
            $init = $argv[2];
            //$init = 'B';
            $search = "$init%";
            $stmt = $pdo->prepare("SELECT * FROM users WHERE lastName LIKE ? order by lastName;");
            $stmt->execute([$search]);
        //$data = $stmt->fetchAll();
            foreach ($stmt as $row) {
                echo $row["id"]. " " .  $row['lastName'] . ", " . $row['firstName'] . "\n";
            }
        
    

Simple Select using IN Keyword:

        
            $arr = [75,23,456];
            $in = str_repeat('?,', count($arr) - 1) . '?';
            $sql = "SELECT * FROM users WHERE id IN ($in)";
            $stm = $pdo->prepare($sql);
            $stm->execute($arr);
            $data = $stm->fetchAll();
            var_export($data);