『PEARによるODBC』『DBC関数』
『Microsoft SQL Server関数』『MySQL関数』。
他は順次追加。
PEARによるODBC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | require_once "DB.php" ; require_once "DB/odbc.php" ; // DSNを設定(使用DB://USER:PASS@HOST/DATABASE-NAME) $dsn = "mysql://admin:passwd@localhost/AAA" ; // DBへ接続 $db = DB::connect( $dsn ); // エラーチェック if (DB::isError( $db )){ echo "DB接続エラー:" .DB::errorMessage( $db ). "<br/>" ; } // テーブルのフィールド名が直接添字として使用可能になる $db ->setFetchMode(DB_FETCHMODE_ASSOC); // SQLを実行 $sql = "SELECT * FROM BBB" ; $result = $db ->query( $sql ); // テーブルの中身を表示 print ( "<table>" ); while ( $row = $result ->fetchRow()){ print ( "<tr>" ); print ( "<td>" . $row [ "aaa" ]. "</td>" ); print ( "<td>" . $row [ "bbb" ]. "</td>" ); print ( "</tr>" ); } print ( "</table>" ); // 件数 print ( $result ->numRow(). "件" ); // DB接続切断 $db ->disconnect(); |
PEARによるODBC補足
『DB::isError($db)』は『PEAR::isError($db)』、
『DB::errorMessage($db)』は『$db->getMessage()』、
でも良い様子。
1 2 3 | if (PEAR::isError( $db )) { die ( $db ->getMessage()); } |
1 | $db ->setFetchMode(DB_FETCHMODE_ASSOC); |
をしない場合、$row[0]、$row[1]…となる。
ODBC関数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | // DBへ接続(HOST,USER:PASS) $db = odbc_connect( "localhost" , "admin" , "passwd" ); // 使用データベース指定 odbc_exec( $db , "use AAA" ); // SQL実行 $result = odbc_exec( $db , "SELECT * FROM BBB" ); // テーブルの中身を表示 print ( "<table>" ); while ( $row = odbc_fetch_array( $result )){ print ( "<tr>" ); print ( "<td>" . $row [ "aaa" ]. "</td>" ); print ( "<td>" . $row [ "bbb" ]. "</td>" ); print ( "</tr>" ); } print ( "</table>" ); // 結果を保持するリソースを開放 odbc_free_result( $result ); // DB接続切断 odbc_close( $db ); |
ODBC関数補足
odbc_connectの他に『odbc_pconnect』もある
・接続を継続して使いまわせる
・odbc_closeが要らない(止まらない)
・サーバにより接続可能数に制限有り
Microsoft SQL Server関数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | // DBへ接続(HOST,USER:PASS) $db = mssql_connect( "localhost" , "admin" , "passwd" ); // 使用データベース指定 mssql_select_db( "AAA" , $db ); // SQL実行 $result = mssql_query( "SELECT * FROM BBB" , $db ); // テーブルの中身を表示 print ( "<table>" ); while ( $row = mssql_fetch_array( $result )){ print ( "<tr>" ); print ( "<td>" . $row [ "aaa" ]. "</td>" ); print ( "<td>" . $row [ "bbb" ]. "</td>" ); print ( "</tr>" ); } print ( "</table>" ); // 件数 print (mssql_num_rows( $result ). "件" ); // 結果を保持するリソースを開放 mssql_free_result( $result ); // DB接続切断 mssql_close( $db ); |
MSSQL関数補足
mssql_connectの他に『mssql_pconnect』もある
・接続を継続して使いまわせる
・mssql_closeが要らない(止まらない)
・サーバにより接続可能数に制限有り
MySQL関数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | // DBへ接続(HOST,USER:PASS) $db = mysql_connect( "localhost" , "admin" , "passwd" ); // 使用データベース指定 mysql_select_db( "AAA" , $db ); // SQL実行 $result = mysql_query( "SELECT * FROM BBB" , $db ); // テーブルの中身を表示 print ( "<table>" ); while ( $row = mysql_fetch_array( $result )){ print ( "<tr>" ); print ( "<td>" . $row [ "aaa" ]. "</td>" ); print ( "<td>" . $row [ "bbb" ]. "</td>" ); print ( "</tr>" ); } print ( "</table>" ); // 件数 print (mysql_num_rows( $result ). "件" ); // 結果を保持するリソースを開放 mysql_free_result( $result ); // DB接続切断 mssql_close( $db ); |
MySQL関数補足
mysql_connectの他に『mysql_pconnect』もある
・接続を継続して使いまわせる
・mysql_closeが要らない(止まらない)
・サーバにより接続可能数に制限有り
PHP7では『mysql_』使えないので『mysqli_』
1 2 3 4 | // DBへ接続(HOST,USER:PASS) $db = mysqli_connect( "localhost" , "admin" , "passwd" ); // 使用データベース指定 mysqli_select_db( $db , "AAA" ); |
コネクトリンクIDの位置が変わっているので注意。
コメント