How PHP accesses databases through ODBC _php tutorial

Source: Internet
Author: User
The environment used
Set up a test database first
Then create an ODBC link
and build a test PHP Script.
Let's test it out.
The environment used

This document is mainly described in WIN32 environment, you need to run Windows 9x/nt/2000 Computer, and installed any kind of Web server and PHP3 or PHP4, and correctly execute PHP Script. And there is more than one SQL database software, such as : Access ...

This document is described in the Ms-access database, and other types of databases can be used in a similar way to make ODBC connections.

Set up a test database first

Enter the ms-access and create a Odbctest.mdb file.
Start building a data table (table).
In this data table we give it two fields: ID and name.
Name this table Class.
We then enter some data. For example:
Then create an ODBC link
Open the ODBC data source in the console.
Select the system Data source Name page.
Click "Add ..." Button.
Select the ODBC driver you want to use. Select Microsoft Access Driver here, and if you use a different kind of database side, select the ODBC Driver for that database.
Click the "Finish" button.
This window will then appear for further setup.
The input data source name is "WebDB". The part of the description can be entered casually, you can understand it.
Press "SELECT ..." button to enter the location of the database file you wish to link to. For example: Here's C:\odbctest.mdb
Then click "Advanced ..." button. This screen will appear:
The part you need to enter is the login name and password, which we will first set to ' WebUser ' and ' WebPassword ' respectively.
It's OK. The ODBC Setup section has been completed.
and build a test PHP Script.
The following is a test php script content, please save it, such as to your Web server's file root directory.
function Error_Handler ($msg, $CNX)
{
echo "$msg \ n";
To avoid the use of links, it is important to close off before the program ends.
Odbc_close ($CNX);
Exit ();
}

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

If you have permission problems during testing, you may be able to use Superadmin to access:
$CNX = Odbc_connect (' webdb ', [sa login], [sa password]);

if (! $cnx) {
Error_Handler ("Error occurred in Odbc_connect", $CNX);
}

Send out a simple ODBC query. Returns an ODBC indicator
$cur = Odbc_exec ($CNX, "Select Id,name from Class");
if (! $cur) {
Error_Handler ("Error occurred in odbc_exec (no indicator passed back)", $CNX);
}


echo "













seat \ n "; $num _row=0; Retrieve the data that was successfully returned while (Odbc_fetch_row ($cur)) {$num _row++;//Fetch data $id = Odbc_result ($cur, 1) of the "id" field;//Fetch data from "Name" field $name = Odbc_result ($cur, 2); echo " \ n "; } echo "
name
$id $name
Total $num _row People
";

Odbc_close ($CNX);

?>



Let's test it out.

From your Web browser, open the PHP Script for this test.

If everything is correct, you should be able to see the following data:

Name
1 Ernest
2 Norman
3 Php/zend RC
4 Odbccooler
5, I'm number fifth.
Number 66th is me.
Total 6 people

http://www.bkjia.com/PHPjc/315162.html www.bkjia.com true http://www.bkjia.com/PHPjc/315162.html techarticle the environment used to build a test database and then build an ODBC link to build a test PHP Script let's test it. The environment used in this document is mainly in the WIN32 environment ...

  • 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.