0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

webarenaでubuntu その53

Last updated at Posted at 2019-10-03

概要

webarenaでubuntu18.04やってみた。
練習問題やってみた。

練習問題

jsonで、アクセスカウンターを表示、せよ。

mysqlでテーブル作る。

create table counter(id varchar(20) NOT NULL PRIMARY KEY, atime timestamp NOT NULL, cnt int(10));

insetする。

insert into counter values('test', '2019/10/03 12:00:00', 0);

phpで、アクセスカウンターをjsonで返す。

<?php
	header("Access-Control-Allow-Origin: *");
	header("Content-Type:  application/json");
	$id = "test";
	$db = new PDO("mysql:host=localhost;dbname=mydb;", 'user', 'pass');
	if (!$db)
	{
		print "connect error!! <br>";
	}
	else
	{
		$sql = "UPDATE counter SET cnt = cnt + 1 WHERE id='$id'";
		$db->query($sql);
		$sql = "SELECT * FROM counter WHERE id='$id'";
		$rs = $db->query($sql);
		if (!$rs)
		{
			print "Error in database!! 0 <br>";
			print $sql;
		}
		else
		{
			$userData = array();
			while ($row = $rs->fetch())
			{
    			   $userData[] = array('count' => $row['cnt']);
			}
			echo json_encode($userData);
		}
	}
?>

成果物

以上。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?