PHP: Database issues - Manual
PHP  
downloads | documentation | faq | getting help | | php.net sites | links 
search for in the  
previousObtaining PHPInstallationnext
Last updated: Wed, 24 Jul 2002
view this page in Printer friendly version | English | Brazilian Portuguese | Czech | Dutch | Finnish | French | German | Hungarian | Italian | Japanese | Korean | Polish | Romanian | Russian | Spanish | Turkish

�� 48. Database issues

This section holds common questions about relation between PHP and databases. Yes, PHP can access virtually any database available today.

1. I heard it's possible to access Microsoft SQL Server from PHP. How?
2. Can I access Microsoft Access databases?
3. I upgraded to PHP 4, and now mysql keeps telling me "Warning: MySQL: Unable to save result set in ...". What's up?
4. After installing shared MySQL support, Apache dumps core as soon as libphp4.so is loaded. Can this be fixed?
5. Why do I get an error that looks something like this: "Warning: 0 is not a MySQL result index in <file> on line <x>" or "Warning: Supplied argument is not a valid MySQL result resource in <file> on line <x>?

1. I heard it's possible to access Microsoft SQL Server from PHP. How?

On Windows machines, you can simply use the included ODBC support and the correct ODBC driver.

On Unix machines, you can use the Sybase-CT driver to access Microsoft SQL Servers because they are (at least mostly) protocol-compatible. Sybase has made a . For other Unix operating systems, you need to contact Sybase for the correct libraries. Also see the answer to the next question.

2. Can I access Microsoft Access databases?

Yes. You already have all the tools you need if you are running entirely under Windows 9x/Me, or NT/2000, where you can use ODBC and Microsoft's ODBC drivers for Microsoft Access databases.

If you are running PHP on a Unix box and want to talk to MS Access on a Windows box you will need Unix ODBC drivers. has Unix-based ODBC drivers that can do this. There is a free pilot program where you can download an evaluation copy that doesn't expire and prices start at $675 for the commercial supported version.

Another alternative is to use an SQL server that has Windows ODBC drivers and use that to store the data, which you can then access from Microsoft Access (using ODBC) and PHP (using the built in drivers), or to use an intermediary file format that Access and PHP both understand, such as flat files or dBase databases. On this point Tim Hayes from OpenLink software writes:
Using another database as an intermediary is not a good idea, when you can
      use ODBC from PHP straight to your database - i.e. with OpenLink's drivers. If
      you do need to use an intermediary file format, OpenLink have now released
      Virtuoso (a virtual database engine) for NT, Linux and other unix platforms.
      Please visit our  for a free download.

One option that has proven successful is to use MySQL and its MyODBC drivers on Windows and synchronizing the databases. Steve Lawrence writes:

  • Install MySQL on your platform according to instructions with MySQL. Latest available from (get it from your mirror!). No special configuration required except when you set up a database, and configure the user account, you should put % in the host field, or the host name of the Windows computer you wish to access MySQL with. Make a note of your server name, username, and password.

  • Download the MyODBC for Windows driver from the MySQL site. Latest release is myodbc-2_50_19-win95.zip (NT available too, as well as source code). Install it on your Windows machine. You can test the operation with the utilities included with this program.

  • Create a user or system dsn in your ODBC administrator, located in the control panel. Make up a dsn name, enter your hostname, user name, password, port, etc for you MySQL database configured in step 1.

  • Install Access with a full install, this makes sure you get the proper add-ins.. at the least you will need ODBC support and the linked table manager.

  • Now the fun part! Create a new access database. In the table window right click and select Link Tables, or under the file menu option, select Get External Data and then Link Tables. When the file browser box comes up, select files of type: ODBC. Select System dsn and the name of your dsn created in step 3. Select the table to link, press OK, and presto! You can now open the table and add/delete/edit data on your MySQL server! You can also build queries, import/export tables to MySQL, build forms and reports, etc.

Tips and Tricks:

  • You can construct your tables in Access and export them to MySQL, then link them back in. That makes table creation quick.

  • When creating tables in Access, you must have a primary key defined in order to have write access to the table in access. Make sure you create a primary key in MySQL before linking in access

  • If you change a table in MySQL, you have to re-link it in Access. Go to tools>add-ins>linked table manager, cruise to your ODBC DSN, and select the table to re-link from there. you can also move your dsn source around there, just hit the always prompt for new location checkbox before pressing OK.

3. I upgraded to PHP 4, and now mysql keeps telling me "Warning: MySQL: Unable to save result set in ...". What's up?

Most likely what has happened is, PHP 4 was compiled with the '--with-mysql' option, without specifying the path to MySQL. This means PHP is using its built-in MySQL client library. If your system is running applications, such as PHP 3 as a concurrent Apache module, or auth-mysql, that use other versions of MySQL clients, then there is a conflict between the two differing versions of those clients.

Recompiling PHP 4, and adding the path to MySQL to the flag, '--with-mysql=/your/path/to/mysql' usually solves the problem.

4. After installing shared MySQL support, Apache dumps core as soon as libphp4.so is loaded. Can this be fixed?

If your MySQL libs are linked against pthreads this will happen. Check using ldd. If they are, grab the MySQL tarball and compile from source, or recompile from the source rpm and remove the switch in the spec file that turns on the threaded client code. Either of these suggestions will fix this. Then recompile PHP with the new MySQL libs.

5. Why do I get an error that looks something like this: "Warning: 0 is not a MySQL result index in <file> on line <x>" or "Warning: Supplied argument is not a valid MySQL result resource in <file> on line <x>?

You are trying to use a result identifier that is 0. The 0 indicates that your query failed for some reason. You need to check for errors after submitting a query and before you attempt to use the returned result identifier. The proper way to do this is with code similar to the following:
$result = mysql_query("SELECT * FROM tables_priv");
if (!$result) {
    echo mysql_error();
    exit;
}
or
$result = mysql_query("SELECT * FROM tables_priv")
    or die("Bad query: ".mysql_error());

User Contributed Notes
Database issues
add a note about notes

08-Aug-2001 11:50

Postgres and PHP.  If you receive an error of "undefined
function" when trying to connect to a Postgres database on a UNIX
box, you may have to do the following:

1. Locate the pgsql.so file on your server.
2. Create a symbolic link to that file in the directory that you are
working in.
3. Inlcude this line at the beginning of your PHP Code:
dl("pgsql.so");
Your Postgres should work correctly.


10-Jan-2002 02:19

The default install of PHP on RedHat 7.1 is NOT enabled for use with MySQL.
All over the place I was reading that I would have to compile my own
install of PHP, MySQL and Apache.

If you visit  and
search for mysql.so you should be able to find a Red Hat compiled RPM
which has the required modules.

After installing the RPM just init 1 and init 3 to restart the services
and it should be working.

Wanted to post this here to save others from a few days of frustration.

BTW PHP rocks!!!


25-Jan-2002 10:29

Onde eu acho os modulos para usar o interbase no php4??

mark


27-Feb-2002 05: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.


22-Apr-2002 05:16

Regarding that post for mysql.so and RPMFIND!!

If your system (RedHat) can use "up2date" - Just request the
package php-mysql - It's much easier you know ;)

22-May-2002 10:51
I want to use php to connect and access MS-SQL 2000 database.
but not use the odbc how may i to do this ?


27-May-2002 03:54

How to make a query in DB PostgreSQL within PHP


17-Jun-2002 07:48

Is it possible to use php to access and edit .dbf files??


09-Jul-2002 04:27

I have necessity to connect PHP 4 in PostgreSQL 7,2 twirling in the Apache
in environment Win-ME!


16-Jul-2002 09:38

I am planning to develope an online general knowledge database with
built-in search engine function. I wish to set up this system by using PHP
and MySQL.

What are the steps should I follow?


18-Jul-2002 07:32

How I create tables with PHP into MySQL DB ?


21-Jul-2002 09:17

Pulling my frickin' hair out trying to get PHP4 to talk to a PostgreSQL 7.2
database on Linux.  

PGSQL is up and running no problem.

In my php code I have tried:
dl("pgsql.so") even though there is no pgsql.so on the server so
I copy the /lib/libpgsql.2.2.so into place and rename.  PHP erros out that
pgsql.so is not a PHP library.

Tried include("DB/pgsql.php") and this gives endless errors
about class db_common can't inherit this that bla bla bla.

Isn't there any clear cut examples or step-by-step how tos?  

What am I missing here?

By the way, I have set the LD_LIBRARY_PATH and I have included pgsql/lib
path in /etc/ld.so.conf.

Compiled PHP4 as with:
./configure --enable-track-vars --with-gettext --enable-force-cgi-redirect
--with-apache=../apache_1.3.26 --without-mysql
--with-pgsql=/usr/local/pgql

PHP & Apache are working just none of my database code works.

Any ideas, suggestions are greatly appreciated.

add a note about notes
previousObtaining PHPInstallationnext
Last updated: Wed, 24 Jul 2002
show source | credits | stats | mirror sites:  
Copyright © 2001, 2002 The PHP Group
All rights reserved.
This mirror generously provided by:
Last updated: Sun Jul 28 16:09:02 2002 CEST