How PHP accesses databases through ODBC

Source: Internet
Author: User
Tags odbc odbc connection php script return zend
Environment used by ODBC
Create a test data library first.
Then create an ODBC connection
and build a test PHP Script.
Let's try it.
Author
Thank
Use of the environment

This document is mainly stated in the WIN32 environment, what you need is a computer that runs Windows 9x/nt/2000 and is equipped with any Web server and PHP3 or PHP4, and you can perform PHP Script correctly. And there is more than one SQL library software, for example: Access ...

This document is presented in ms-access databases, and other types of data repositories can be used for ODBC connectivity.

Create a test data library first.

Go into ms-access and build a Odbctest.mdb file.
Start building a data table.
This data table we give it two places: ID and name.


Name this data table Class.


Then we input some data. For example:
Then create an ODBC connection

Open the "ODBC data source" in the console.

Select the "System Data Source Name" page.
Click the "New ..." option. Push button.
Select the ODBC driver you want to use. Select Microsoft Access Driver, and if you are using a different kind of data library, select the ODBC Driver for that library.




Click the "Finish" button.


Then the window will appear to make a further setting.


Input the source name is called "WebDB". The description of the part can be input, you know it.
Press "SELECT ..." button, enter the location of the data library you want to connect to. For example: Here's the C:\odbctest.mdb
Then click on the "Advanced ..." option. button. This screen will appear:
The part you need to enter is: Login name and password, we will first separate the ' WebUser ' and ' WebPassword '.




OK, okay. The setup part of ODBC has been completed.




and build a test PHP Script.

Here is a test of the PHP script content, please save it, for example, to the file root of your Web server record.


?

//____________________________________________________________
//
Single-bit php/zend RC (http://mark.ee.nctu.edu.tw:8888)
File name odbctest.php
Use a simple PHP program to test through how ODBC accesses the library
Author Ernest (Chiang Deng-wei) to May 14, 2000
Mailbox ernestch@ms2.url.com.tw
//____________________________________________________________


function Error_Handler ($msg, $CNX)
{
echo "$msg \ n";
To avoid the use of connections, it is important to close the program before it is finished.
Odbc_close ($CNX);
Exit ();
}

Create an ODBC link and return to the $CNX
$CNX = Odbc_connect (' webdb ', ' webuser ', ' WebPassword ');

If you have a problem with your test, maybe you can use Superadmin to access:
$CNX = Odbc_connect (' webdb ', [sa login], [sa password]);

if (! $cnx) {
Error_Handler ("In Odbc_connect, there are mistakes", $CNX);
}

Send out a simple ODBC query. The return of an ODBC indicator
$cur = Odbc_exec ($CNX, "Select Id,name from Class");
if (! $cur) {
Error_Handler ("There is a mistake in odbc_exec (no marker)", $CNX);
}


echo "<table border=1><tr><th> seat </th><th> name </th></tr>\n";
$num _row=0;

Take out the data that's been successfully uploaded
while (Odbc_fetch_row ($cur))
{
$num _row++;
Crawl "id" in the position of the data
$id = Odbc_result ($cur, 1);
Crawl the "name" in the position of the data
$name = Odbc_result ($cur, 2);
echo "<tr><td> $id </td><td> $name </td></tr>\n";
}

echo "<tr><td colspan=2> altogether $num _row person </td></tr></table>";

Odbc_close ($CNX);

?>



Let's try it.

From your Web browser, start browsing through this test PHP Script.

If all is right, you should be able to see the following information:

Name of the seat
1 Ernest
2 Norman
3 Php/zend RC
4 Odbccooler
5, I'm five.
66 is me.
Total 6 people

Author

The author of this paper is Ernest (Chiang Deng-wei), my email is ernestch@ms2.url.com.tw, if you have any use questions or suggestions on this document, welcome you to the Php/zend RC PHP version to participate in the discussion, We welcome you here first. It is my sincere hope that this paper can help Phper.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.