PHP
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

インストール> <PHP を手に入れるには
Last updated: Fri, 05 Sep 2008

view this page in

データベースに関する問題

このセクションでは PHP とデータベースとの関係に関する一般的な質問を 扱います。なんと! PHP は事実上あらゆるデータベースにアクセスすることが できます。

  1. PHP は Microsoft SQL Server にアクセスできると聞きました。どうすれば よいのでしょうか?
  2. Microsoft Access データベースにアクセスできますか?
  3. PHP 5 では MySQL クライアントライブラリがバンドルされません。これは 私にとってどのような意味がありますか? PHP を MySQL とともに使用する ことはできるのですか? MySQL を使用しようとすると "function undefined" エラーが出るのですが、どうしたらいいのですか?
  4. 共有 MySQL サポートをインストールしたら、libphp4 がロードされると 同時に Apache がコアダンプします。直りますか?
  5. "Warning: 0 is not a MySQL result index in <file> on line <x>" もしくは "Warning: Supplied argument is not a valid MySQL result resource in <file> on line <x>" のようなエラーが出るのはなぜでしょうか?

PHP は Microsoft SQL Server にアクセスできると聞きました。どうすれば よいのでしょうか?

Windows マシン上では、ODBC サポートと適切な ODBC ドライバを 使用すればよいだけです。

Unix マシン上では、Sybase-CT ドライバを使って Microsoft SQL Server にアクセスすることができます。なぜなら(ほとんど完全に)プロトコル 互換だからです。Sybase は » 必要な Linux 用ライブラリのフリーな実装 を作成しました。他の Unix システムでは適切なライブラリを手に入れるために Sybase と連絡を取る必要があります。 次の質問に対する回答も参照してください。

Microsoft Access データベースにアクセスできますか?

はい。もし全て (PHP と Microsoft Access) を Windows9x/Me/NT/2000 上で動作させるのであり、ODBC と Microsoft が提供する Microsoft Access 用 ODBC ドライバ が使用できる状態ならば、 すべての必要なツールは揃っています。

Unix で PHP を動作させて Windows マシンで動作する MS Access に接続したい場合には Unix ODBC ドライバが必要です。» OpenLink Software が Unix ベースの ODBC ドライバを提供しています。

他には、Microsoft SQL Server に ODBC ドライバを使用してデータを 保存するという手段もあります。これによって、Microsoft Access (ODBC を使用します) と PHP (組み込まれているドライバを使用します) でデータに アクセスすることができます。また、Microsoft Access と PHP 両者共に 解釈可能な中間ファイル(例えば単層からなるファイルや、dBase データベース等)を使用する手もあります。これに関しては OpenLink Software の Tim Hayes が以下のように述べています。

PHP からじかに ODBC ドライバが使用できる場合(つまり OpenLink の ドライバを使用している場合)には、他のデータベースを中継手段として 用いることはよいアイデアとは言えない。どうしても中間ファイルが 必要な場合のために、OpenLink は NT、Linux、そして他の Unix 用の Virtuoso(仮想データベースエンジン)をリリースした。私たちの » ウェブサイトを 訪れてもらえれば、無料でダウンロードできる。

うまくいくことが分かってるもう一つの方法は、MySQL と Windows 用の MyODBC ドライバを利用してデータベースを同期する方法です。Steve Lawrence が言うには、

  • MySQL を説明に従って任意のプラットフォームにインストールします。 最新バージョンは » http://www.mysql.com/ にあります。データベースを作成するとき、そしてユーザアカウントを を設定するときに、ホストフィールドに % か MySQL を使って アクセスする Windows マシンのホスト名を入力する、ということ以外には 特別な設定は必要ありません。 使用するサーバ名、ユーザ名、パスワードを書きとめておいてください。
  • MySQL サイトから Windows 用 MyODBC ドライバをダウンロードします。 それを Windows マシンにインストールします。 アーカイブに含まれるユーティリティプログラムで、 テストを行うことができます。
  • コントロールパネルの ODBC administrator を使用して、ユーザ もしくはシステム DSN を作成します。DSN 名を決定して、Step1 で MySQL に設定したホスト名、ユーザ名、パスワード、ポート等を入力します。
  • Access をフルインストールでインストールします。これは適切な アドインをインストールするためです。少なくとも ODBC サポートと リンクテーブルマネージャが必要です。
  • ここからがお楽しみです。新規データベースを作成しましょう。 テーブルウィンドウで右クリックしてリンクテーブルを選択します。 もしくはファイルメニューから外部データの取り込みを選び リンクテーブルを選択します。ファイルブラウザが表示されたら ファイルタイプから ODBC を選択します。次にシステム DSN で 選択肢 STEP3 で作成した DSN 名を選択します。リンクするテーブルを 選んで OK ボタンを押しましょう。MySQL サーバでテーブルを オープンできるようになっていて、データの追加/削除/編集ができるように なっています。さらに、クエリの構築、テーブルのインポート/エクスポート、 フォームやレポートの構築等が可能です。

Tips and Tricks(役に立つヒント):

  • Access でテーブルを作成してそれを MySQL へエクスポートします。 それを再度アクセスからリンクすれば素早くテーブルを作成すること ができます。
  • Access でテーブルを作成するときは、そのテーブルに書き込み権限を もたせるためにプライマリキーを設定しておく必要があります。 Access とテーブルをリンクする前に MySQL にプライマリキーを設定 しておくことも忘れないでください。
  • MySQL 側でテーブル(の構成)を変更した場合、再度 Access とリンクする 必要があります。ツール>アドイン>リンクテーブルマネージャから 適切な ODBC DSN を選んで再リンクが必要なテーブルを選択します。 また、ここで OK ボタンを押す前に「リンク先を更新するための プロンプトを毎回表示する」をチェックしておけば DSN ソースを 移動させることができます。

PHP 5 では MySQL クライアントライブラリがバンドルされません。これは 私にとってどのような意味がありますか? PHP を MySQL とともに使用する ことはできるのですか? MySQL を使用しようとすると "function undefined" エラーが出るのですが、どうしたらいいのですか?

はい。どのような意味においても PHP は常に MySQL をサポートしています。 PHP 5 で変わったことといえば、クライアントライブラリをバンドルしなく なったということだけです。以下に、順不同で理由を挙げます。

  • 最近のシステムにはすでにクライアントライブラリがインストールされています。

  • 上と関連して、複数バージョンのライブラリを共存させると環境がおかしく なりがちです。たとえば、mod_auth_mysql と PHP をそれぞれ別のバージョンの ライブラリとリンクさせ、Apache でそれら両方を有効にすると、いとも簡単に クラッシュすることでしょう。また、バンドルされているライブラリが インストールされているサーバとうまく動作するとは限りません。もっとも ありがちな症状は、Unix ドメインソケットのファイル mysql.socket を探す場所の不一致です。

  • 保守がとどこおりがちで、現在リリースされているバージョンに どんどん遅れをとってしまいます。

  • 将来のバージョンのライブラリは GPL の元で配布されます。そのため、 私たちはアップグレードの手段を提供することができません。なぜなら 私たちのような BSD/Apache スタイルのライセンスを採用している プロジェクトは、GPL のライブラリをバンドルできないからです。 PHP 5 でバンドルをやめたことは最善の方法だと考えます。

このことがそんなに多くの人々に影響することはないでしょう。 Unix ユーザ、少なくとも自分が何をしているかを把握している人たちは、 PHP をビルドする際には常に --with-mysql=/usr してシステムの libmyqlclient ライブラリを使用するようにしているでしょう。 Windows ユーザは、php.iniphp_mysql.dll を有効にしていることでしょう。 インストール手順の詳細は、MySQL リファレンス を参照ください。また、libmysql.dll が システムの PATH が通った場所にあることを確認してください。その方法に ついては、FAQ の Windows のシステム PATH を設定する を参照ください。 libmysql.dll(やその他多くの PHP 関連ファイル)が PHP フォルダにあることから、このフォルダをシステムの PATH に追加 したくなるかもしれません。

共有 MySQL サポートをインストールしたら、libphp4 がロードされると 同時に Apache がコアダンプします。直りますか?

もしあなたのシステムの MySQL ライブラリが pthreads とリンクされて いる場合にはこの現象が発生します。ldd コマンドを使用してチェック してください。もし pthreads がリンクされている場合は、MySQL の tarball を展開してソースからコンパイルしなおしてください。もしくは SRPM の SPEC ファイルのスレッドクライアントコードの箇所を削除してコンパイル しなおしてください。いずれかの方法で問題を解決できます。その後、 PHP を新しい MySQL ライブラリでコンパイルしなおしてください。

"Warning: 0 is not a MySQL result index in <file> on line <x>" もしくは "Warning: Supplied argument is not a valid MySQL result resource in <file> on line <x>" のようなエラーが出るのはなぜでしょうか?

あなたは値が 0 である結果(result)ID を指定しようとしています。0 は あなたのクエリが何らかの理由で失敗したことを示しています。結果 (result)ID を使用する前に、クエリを送信したあとのエラーをチェック する必要があります。以下のようなコードが正しい方法です。

<?php

$result 
mysql_query("SELECT * FROM tables_priv");
if (!
$result) {
    echo 
mysql_error();
    exit;
}
?>
もしくは
<?php

$result 
mysql_query("SELECT * FROM tables_priv")
    or die(
"Bad query: " mysql_error());
?>



インストール> <PHP を手に入れるには
Last updated: Fri, 05 Sep 2008
 
add a note add a note User Contributed Notes
データベースに関する問題
kalle dot kipina at gmail dot com
09-Aug-2007 10:06
As reply to the entry from 20-Mar-2005 01:19:

The trick seems to be to insert the php directory into your path before any of the other libmysql.dll locations.
cantelow at csd dot net
31-Aug-2006 03:48
On Tru64 5.1A, we had what looked like the mysql pthread problem, with php 5 shared lib compiling fine but crashing upon firing up apache.  But, our problem turned out to be this: we were running 2 versions of mysql and attempting to use --with-mysql=/usr/local/newmysql5 to link to our running test mysql5 distribution.  php was still linking against an old /usr/local/lib/mysql mysqlclient library, however (or setting up to use old /usr/local/lib/mysql shared mysqlclient.so, not sure which).

When we made sure that only /usr/local/newmysql5/lib/mysql/ mysqlclient library (which was a static one in our case) was available to our compiling user, our problem with php crashing on apache start went away. (We used tru64's handy setacl command to accomplish this.)  We still have the mysql5 pthread-included compile.
Kaleabtesfaye at hotmail dot com
14-Apr-2006 05:56
If Some1 is intersted on PHP with MS Access
here is some code
<?php

$db
= 'C:\\Program Files\\Microsoft Office\\Office\\Samples\\Northwind.mdb';

$conn = new COM('ADODB.Connection') //or exit('Cannot start ADO.');

// Two ways to connect. Choose one.
$conn->Open("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=$db") or exit('Cannot open with Jet.');
//$conn->Open("DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=$db") //or exit('Cannot open with driver.');

$sql = 'SELECT   Table Field1,feild2...
    FROM     db
    ORDER BY Field'
;
$rs = $conn->Execute($sql);

?>

Enjoy
Addis Ababa,
Ethiopia
jstoner at NOSPAM dot opsource dot net
02-Mar-2006 08:56
Building in a Redhat 4 64-bit environment:

This assumes you're using Redhat's RPMs (mysql, mysql-devel).

MySQL libraries get installed in /usr/lib64 but PHP only searches /usr/lib. This means your config will fail. You need to create a fake install of MySQL in another directory to get PHP to build correctly.

Create another directory, say /usr/local/mysql. Then cd into that directory and create 2 symbolic links pointing to the appropriate include directory and lib directory:

# mkdir /usr/local/mysql
# cd /usr/local/mysql
# ln -s /usr/include/mysql include
# ln -s /usr/lib64/mysql lib

Now, you should be able to configure PHP with "--with-mysql=/usr/local/mysql" and build properly.
James Coburn
02-Feb-2006 12:31
An easy way to compile PHP5 when you have installed mysql, but
- you can't find the "mysql header files" or
- get this error: Cannot find MySQL header files under /usr/

: simply install mysql-devel from mysql site.
plandis98 at yahoo dot com
19-Dec-2005 05:46
If one is using PHP on Windows to connection to MS SQL Server and does not want to pass credentials via the mssql_connect but instead use MS Connectionless NT Authentication, what is the proper syntax/usage? 

I believe you want to turn on secure_connection in php.ini file:
; Use NT authentication when connecting to the server
mssql.secure_connection = On
djlopez at gmx dot de
08-Sep-2005 10:51
How to compile PHP5 when you have installed mysql, but
- you can't find the "mysql header files" or
- get this error: Cannot find MySQL header files under /usr/

If you want to use mysql 4.0 (*not* 4.1 or even higher!) just download & extract the latest mysql4.0 binary distribution to e.g.
/root/mysql40/

but don't start it (it even should not work, if you have a mysql server already running)

Then use --with-mysql=/root/mysql4 within your php5 configuration.

After compiling/installing (make, make install), you may delete the /root/mysql40/ directory, or keep it for later versions of php5.
21-Mar-2005 08:19
Just a quick note which may help Windows users setting up PHP/Apache/MySQL.  I was receiving these two error messages on Apache startup: 1) "The procedure entry point mysql_thread_end could not be located in the dynamic link library LIBMYSQL.dll" and 2) "PHP Startup: Unable to load dynamic library '<my php installation dir>/ext/php_mysql.dll' - The specified procedure could not be found".  After checking that libmysql.dll did exist in my root PHP install dir, and that this dir was in my path, I determined that the problem was an older version of libmysql.dll.  My solution was to search all harddrives for this file (I found 6 copies of 5 different versions), and rename all of them except my PHP install dir copy.  PHP, Apache and MySQL all function as expected after making this change.
knb at gfz-potsdam dot de
11-Oct-2004 07:53
This is a crucial piece of information for SYBASE users:

If you are using the free, but old, 11.x client libs from sybase,
 then compile with option  "--with-sybase-ct=$SYBASE"
substitute $SYBASE with the appropriate directory name.

option --with-sybase (without ct) can somehow be used to talk to old MS-SQL servers, but only with the 11.x client libs.

If you are using free, or have legally obtained, 12.x client libs from sybase, then compile with option  "--with-sybase-ct=$SYBASE/$SYBASE_OCS"
substitute $SYBASE/$SYBASE_OCS with the appropriate directory name.
Simon
05-Nov-2003 11:38
You can connect to Microsoft SQL Server from PHP without using ODBC. It's even detailed in the PHP docs! Wow! You can find the details at http://www.php.net/manual/en/ref.mssql.php
jastern at uci dot edu
27-Sep-2002 07:24
i made some notes of trials/tribulations setting up PHP (on a linux redhat 7.3 box) to connect (via interbase/ODBC client libraries) to an interbase server on another machine:

http://www.faqts.com/knowledge_base/view.phtml/aid/18951/fid/679

hope it helps someone..
michal at tuxy dot org
27-Feb-2002 06:22
You can approch problem with mysql and pthread by recompiling/linking apache (1.3.2x) with pthread library instead of removing pthread from mysql. This solved my problem with apache dumping core with php (3.0.18 and php 4.1.1) and mysql.
You can use ldd to check if apache is compiled with pthreads.
Hope that helps.

インストール> <PHP を手に入れるには
Last updated: Fri, 05 Sep 2008
 
 
show source | credits | stats | sitemap | contact | advertising | mirror sites