Agenda

  • PHPプログラミング(練習問題)
  • SQLプログラミング
  • DBサーバ

config

188.166.226.88
codeU1219#a

環境構築

sudo apt update
sudo apt install apache2
sudo apt install php
sudo apt install mysql-server
sudo apt install php-mysql

MySQLの接続

mysql -uroot

DBの一覧表示

show databases;

DBの作成

create databse eldb;

DBの接続

use eldb;

サンプルデータ

drop table if exists categories;
create table categories(
  id integer primary key,
  title varchar(255)
);

insert into categories (id, title) values (1, 'Programming');
insert into categories (id, title) values (2, 'Design');
insert into categories (id, title) values (3, 'Marketing');

サンプルプログラム - hello.php

<?php
echo "Hello World!";

サンプルプログラム - db.php

<?php
$dsn = "mysql:host=localhost; dbname=eldb; charset=utf8mb4";
$username = "root";
$passwd = "";
$pdo = new PDO($dsn, $username, $passwd);

$sql = "select id, title from categories order by id";
$st = $pdo->query($sql);
$row = $st->fetch();
while ($row !== false) {
    echo $row["id"] . ":" . $row["title"] .  PHP_EOL;
    $row = $st->fetch();
}