Help us understand the problem. What is going on with this article?

Cloud9でMySQLを使う

More than 3 years have passed since last update.

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;
    }

  }


}
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした