75
69

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.

Cloud9でMySQLを使う

Last updated at Posted at 2015-04-28

Cloud9でDB接続するやり方を毎回ggってるのでメモ。

Terminal側での処理

まずは起動
$ mysql-ctl start

mysql> の入力画面にする
$ mysql-ctl cli

新規DBや新規テーブルの作成はTerminalから直接SQL叩けばOK
※phpMyadminもインストールできる
$ phpmyadmin-ctl install
Setting Up PHPMyAdmin

ちなみにphpMyadminのURIは
https://[workspace名]-[UserName].c9users.io/phpmyadmin/index.php

PHP側からDBを使う

    $servername = getenv('IP');
    $username = getenv('C9_USER');
    $password = "";
    $database = "使用するDB名";
    $dbport = 3306;

    // Create connection
    $db = new mysqli($servername, $username, $password, $database, $dbport);
    $db->set_charset('utf8');

    // Check connection
    if ($db->connect_error) {
        die("Connection failed: " . $db->connect_error);
    } 
  

Setting Up MySQL

ここまでを自分の環境に合わせれば問題なく利用できます。

以下は自分の覚書的な感じです。

PDOで接続

普段使ってるpdoのクラスを使いまわしたいので私は大体こうです。

define.php
<?php
include_once 'pdoclass.php';

$servername = getenv('IP');
$username = getenv('C9_USER');
$password = "";
$database = "DB名";
$dbport = 3306;

// DB接続
$conDB  = new DB($servername, $dbport, $database, $username, $password);

// 時刻設定
// どうやってもcloud9上で時刻設定ができなかったので全ファイルでincludeするここに書いた
date_default_timezone_set('Asia/Tokyo');

オプションの設定がアレなのは実環境がPHP5.2.4なせいですorz

pdoclass.php
<?php
/**
 * Class DB
 */
class DB {

  protected $pdo;
  protected $stmt;

  /**
   * @param $host
   * @param $dbname
   * @param $user
   * @param $password
   */
  public function __construct($host, $port, $dbname, $user, $password ) {
    try {
      $this->pdo = new PDO("mysql:host=".$host.";port=".$port.";dbname=".$dbname, $user, $password ,array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'));
      $this->pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE,PDO::FETCH_ASSOC);
      $this->pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
      $this->pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
      $this->pdo->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY,true);
    } catch (Exception $e) {
      print "エラー!!:". $e->getMessage() . "<br/>";
    }

    return $pdo;
  }

  /**
   * @param $sql
   * @return array $stmt
   */
  function pdoQuery($sql){
    $stmt = $this->pdo->query($sql);
    foreach ($stmt as $row) {
      $res[] = $row;
    }

    return $res;
  }

  /**
   * @param string $sql
   * @param array  $arr
   * @param int    $mode SELECTの時は1,それ以外の時は2
   *
   * @return array $stmt
   */
  function pdoPrepare($sql,$arr,$mode){
    $stmt = $this->pdo->prepare($sql);
    foreach ($arr as $key => $val) {
      $k = $key+1;
      if(is_int($val)){
          $val = (int)$val;
          $stmt->bindValue($k,$val,PDO::PARAM_INT);
      }else{
          $val = (string)$val;
          $stmt->bindValue($k,$val,PDO::PARAM_STR);
      }
    }

    $stmt->execute();

    if($mode == 1){

      foreach ($stmt as $row) {
        $res[] = $row;
      }

      return $res;

    }else{

      return true;
    }

  }


}
75
69
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
75
69

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?