How PHP accesses the database through ODBC

Source: Internet
Author: User
Tags odbc connection
How PHP accesses the database through ODBC, and how PHP accesses the database through ODBC, in the environment used, create a database for testing, establish an ODBC Connection, and then create a PHPScript for testing. let's test the environment. This file is mainly described in the Win32 environment, you need a computer running Windows 9x/NT/2000 "> <LINKh environment
Create a test database first
Create an ODBC Connection
Create another PHP Script for testing
Let's test it.
Environment used

This document describes how to run Windows 9x/NT/2000 in a Win32 environment. you need a computer that runs Windows 9x/NT/and has any web server, PHP3, or PHP4, the PHP Script can be correctly executed. there is more than one type of SQL database software, such as Access...

This document describes how to use the MS-Access database. other types of databases can be used for ODBC connections in a similar way.

Create a test database first

Go to MS-Access and create an odbctest. mdb file.
Create a table ).
This data table has two fields: id and name.
Name the table Class.
Then we enter some data, for example:
Create an ODBC Connection
Enable "ODBC data source" in the console ".
Select the system data source name page.
Click "Add.
Select the ODBC Driver you want to use. select "Microsoft Access Driver" here. of course, if you want to use another database, select the ODBC Driver for the database.
Click "finish.
This window will appear for further settings.
Enter the data source name "WebDB". you can enter the description as needed.
Press the "Select..." button and enter the location of the database file you want to connect to. for example, c: \ odbctest. mdb
Next, click the "advanced..." button. this screen will appear:
You need to enter the logon name and password. Here we set them to 'webuser' and 'webpassword '.
OK. The ODBC settings are complete.
Create another PHP Script for testing
The following is a PHP Script for testing. save it, for example, to the root directory of your web server file.
Function Error_Handler ($ msg, $ cnx)
{
Echo "$ msg \ n ";
// To avoid link occupation, it is important to close the link before the end of the program.
Odbc_close ($ cnx );
Exit ();
}

// Create an ODBC Connection and return it to $ cnx
$ Cnx = odbc_connect ('webdb', 'webuser', 'webpassword ');

// If you have permission issues during the test, you may be able to use superadmin for access:
// $ Cnx = odbc_connect ('webdb', [sa login], [sa password]);

If (! $ Cnx ){
Error_handler ("An error occurred in odbc_connect", $ cnx );
}

// Send out a simple odbc query. return an odbc indicator
$ Cur = odbc_exec ($ cnx, "select id, name from Class ");
If (! $ Cur ){
Error_handler ("An error occurred in odbc_exec (no indicator is returned)", $ cnx );
}


Echo"













\ N ";$ Num_row = 0;// Retrieve the data successfully returnedWhile (odbc_fetch_row ($ cur )){$ Num_row ++;// Capture the data of the "id" field$ Id = odbc_result ($ cur, 1 );// Capture the data of the "name" field$ Name = odbc_result ($ cur, 2 );Echo" \ N ";}Echo"
Landline number Name
$ Id $ Name
Total $ num_row users
";

Odbc_close ($ cnx );

?>



Let's test it.

Open the PHP Script for this test from your web browser.

If everything is correct, you can see the following data:

Seat number name
1 Ernest
2 Norman
3 PHP/Zend RC
4 ODBCCooler
5. I am V5.
6, 6, and I
6 people in total

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.