Build CRUD Application PHP & Mysql. Intro to PDO

Home » Tutorials » PHP+MySQL » Build CRUD Application PHP & Mysql. Intro to PDO
Today we’ll continue to create our first web app, write function for getting data with php pdo and print this data.
First of all we’ll consider why code from last lesson is excess. Try…catch block will print error. But if you wont’ use try…catch in this situation you will see such error, which will tell you about wrong database connection configuration.
As far as the function getAllPlayers, which we will write, its logic is very simple: first of all you must pass $db argument, which we defined in db.php. This is necessary because this function (and others) will work with database. Next we’ll create string variable as sql query and prepare it.

Prepare queries useful because they initialized once, next only parameters are substituted. In fact it is templates for MySQL.

Next we execute our query with execute function. Then we begin to work with data. But this data we must fetch. For this php has got fetch, fetchAll and other functions. Fetch has got one important parameter – fetching style (array, objects and so on).
And now we are ready to print our data.

Code lesson (api.php)

<?php

function getAllPlayers($db) {

    $sql = "SELECT * FROM players";
    $result = array();

    $stmt = $db->prepare($sql);
    //print_r($stmt);
    $stmt->execute();
    //print_r($stmt);

    while($row = $stmt->fetch(PDO::FETCH_ASSOC)) { // рассмотреть fetchAll (режим FETCH_BOTH)
        $result[$row['player_id']] = $row;
        /*echo "<pre>";
        print_r($result[$row['player_id']]);
        echo "</pre>";*/
    }

    //$result = $stmt->fetchAll();

    /*echo "<pre>";
    print_r($result);
    echo "</pre>";*/

    return $result;
}

Code lesson (index.php)

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
        <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.css" rel="stylesheet" crossorigin="anonymous">
        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" crossorigin="anonymous"></script>
    </head>
    <body>
        <header>
            <nav class="navbar navbar-default">
              <div class="container-fluid">
                <div class="navbar-header">
                  <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
                    <span class="sr-only">Toggle navigation</span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                  </button>
                  <a class="navbar-brand" href="#">Sport CRM</a>
                </div>

                <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
                  <ul class="nav navbar-nav">
                    <li><a href="/">Главная</a></li>
                    <li><a href="players.php">Игроки</a></li>
                    <li><a href="teams.php">Команды</a></li>
                  </ul>
                </div>
              </div>
          </nav>
        </header>

        <div class="container-fluid">
            <?php include 'db.php'; ?>
            <?php include 'api.php'; ?>
            <?php
                    $players = getAllPlayers($db);
             ?>
             <table class="table table-bordered">
                 <tr>
                     <th>Игрок</th>
                     <th>Команда</th>
                     <th>Страна</th>
                 </tr>
                 <?php foreach ($players as $player) { ?>
                    <tr>
                        <td><?php echo $player['player_name']; ?></td>
                    </tr>
                <?php } ?>

             </table>
        </div>

        <footer>

        </footer>
    </body>
</html>

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *


The reCAPTCHA verification period has expired. Please reload the page.

Pin It on Pinterest

Share This