Build CRUD Application PHP & Mysql
- Config – connect with database (db.php)
- Teams (teams.php) – list of teams with the possibility of editing information about team
- Players (players.php) – list of players with the possibility of editing information about player
- Countries (countries.php) – list of teams by countries.
In my projects I use PDO for work with databases. There are another variants, for example, mysql and mysqli. I notice, that mysql extension is deprecated from php 5.5 and removed from php 7.
Why do I use PDO? PDO is universal decision for working with databases. It can work with different databases. And it is easy to change database in your application. Next details with PDO working we will consider in next lessons, while writing code of our app.
Also you will know about try.catch construction. When can you use it? There are arise different errors (logical and so on), but sometimes you can provide for and handle this errors. For example, you may understand, attempt with database connection can finish with error. This error you need to provide.
try...catch example
<?php
try {
$db = new PDO("mysql:host=localhost;dbname=sport", $user, $password);
} catch (Exception $e) {
echo $e->getMessage();
}
Code lesson (db.php)
<?php $user = "root"; $password = "29061990"; /*Почему данный код является избыточным? Оставляйте ответы в комментариях*/
try {
$db = new PDO("mysql:host=localhost;dbname=sport", $user, $password);
} catch (Exception $e) {
echo $e->getMessage();
}
Code lesson (index.php)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<header>
<nav class="navbar navbar-default" role="navigation">
<div class="container-fluid">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-ex1-collapse">
<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 navbar-ex1-collapse">
<ul class="nav navbar-nav">
<li><a href="index.php">Главная</a></li>
<li><a href="players.php">Игроки</a></li>
<li><a href="teams.php">Команды</a></li>
<li><a href="countries.php">Страны</a></li>
</ul>
</div>
</div>
</nav>
</header>
<div id="content">
</div>
<footer>
</footer>
</body>
</html>
0 Comments