Skip to content

Instantly share code, notes, and snippets.

@ryuichimatsumoto-single
Created March 28, 2012 14:45
Show Gist options
  • Save ryuichimatsumoto-single/2226769 to your computer and use it in GitHub Desktop.
Save ryuichimatsumoto-single/2226769 to your computer and use it in GitHub Desktop.
Class for MySQL C0nnect
<?php
/**
*MySQLの接続の為のクラス
*Class for connecting MySQL
*/
class MySQL_Connect
{
/*
*コンストラクタ
*@var url:パス
*@var user:ユーザー
*@var pass:パス
*@var db:データーベース名
*/
function __construct($url,$user,$pass,$db)
{
$this->url = $url;
$this->user = $user;
$this->pass = $pass;
$this->db = $db;
}
/*
*DBの結果を配列で出力
*@引数 tblname:テーブル名
*/
function MySQL_fetch_all($tblname)
{
$link = mysql_connect($this->url,$this->user,$this->pass) or die("MySQLへの接続に失敗しました。");
$sdb = mysql_select_db($this->db,$link) or die("データベースの選択に失敗しました。");
//sql文
$sql = "SELECT * FROM ".$tblname;
$result = mysql_query($sql, $link) or die("クエリの送信に失敗しました。<br />SQL:".$sql);
$data = array();
$i=0;
while($rows = mysql_fetch_array($result))
{
$data[$i] = $rows;
$i++;
}
return $data;
}
}
?>
<?php
/*
*MySQL呼び出し
*/
include_once "MySQL_Connect.php";
$url = "localhost";
$user = "root";
$pass ="";
$db = "******";//any words
$tblname ="***";//any words
$Connect = new MySQL_Connect($url,$user,$pass,$db);
print_r($Connect->MySQL_fetch_all($tblname));
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment