Logo  

CS479/579 - Web Programming II

Displaying exercises/e7/solution/mkdb.php

#!/usr/bin/php
<?php
include "config.php";

$myconn->query("DROP TABLE IF EXISTS pkgs;")
  or die("create table" . $myconn->error);
$myconn->query(<<<SQL
CREATE TABLE pkgs (
  `id`		INT(10)		NOT NULL AUTO_INCREMENT,
  `name`	VARCHAR(1024)	NOT NULL DEFAULT "",
  `desc`	TEXT		NOT NULL DEFAULT "",
  PRIMARY KEY (id),
  INDEX name (name)
)
SQL
) or die("create table" . $myconn->error);

$pkgs = json_decode(file_get_contents("pkgs.json"), true);

$stmt = $myconn->prepare("INSERT INTO pkgs (`name`, `desc`) VALUES (?, ?)");
if ($stmt == false) die("prepare: " . $myconn->error);

$stmt->bind_param("ss", $name, $desc);

foreach($pkgs as $pkg) {
  $name = $pkg['name'];
  $desc = $pkg['desc'];
  $stmt->execute();
}

$stmt->close();

?>