Skip to content

Instantly share code, notes, and snippets.

@yandod yandod/sample.php
Created Jan 24, 2012

Embed
What would you like to do?
PDOとmod_rewriteの動作状況確認スクリプト (CakePHP2にはPDOが必要です。mod_rewriteはあった方が良い。)
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<?php
//DBの接続情報
$user = 'root';
$pass = 'pass';
$name = 'yourdatabase';
/**
-- 下記のデータをmysqlの任意のデータベースに登録してください。
CREATE TABLE IF NOT EXISTS `FOO` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`number` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=3 ;
INSERT INTO `FOO` (`id`, `name`, `number`) VALUES
(1, '下北沢', 10),
(2, '渋谷', 20);
**/
$dbh = new PDO(
'mysql:host=localhost;dbname='.$name.';charset=utf8',
$user,
$pass
);
echo '<pre>';
foreach($dbh->query('SELECT * from FOO') as $row) {
print_r($row);
}
if (function_exists('apache_get_modules')) {
echo "apache modules\r\n";
print_r(apache_get_modules());
} else {
echo 'not apache';
}
?>
</body>
</html>
@yandod

This comment has been minimized.

Copy link
Owner Author

commented Jan 24, 2012

うまく動作すれば下記のようになります。
https://skitch.com/yando/g5mxp/127.0.0.1-8080-testdir-pdosample.php

@cognitom

This comment has been minimized.

Copy link

commented Jan 24, 2012

PHP5.3.6で動きました〜。準備おつかれさまです!
PHP5.2.xだと、charset指定が効かず文字化けしそう。
$dbh->query("SET NAMES utf8");
とかしとけばOKですか?

@yandod

This comment has been minimized.

Copy link
Owner Author

commented Jan 24, 2012

SET NAMES utf8 を足すのもアリですー。

@ShinichiNishikawa

This comment has been minimized.

Copy link

commented Jan 25, 2012

動きました!!

今日はよろしくお願いします!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.