How PHP accesses databases through ODBC _php basics

Source: Internet
Author: User
Tags odbc php script access database
Use of the environment
Build a test Database first
Then create an ODBC link
and build a test PHP Script.
Let's test it out.
Use of the environment

This document is mainly in the context of the WIN32 environment, you need to run Windows 9x/nt/2000 computer, and equipped with any kind of Web server and PHP3 or PHP4, and the correct execution of PHP Script. And there is more than one SQL database software, such as : Access ...

This document is illustrated with the Ms-access database, and other types of database terminals can be used as ODBC links.

Build a test Database first

Enter ms-access and build a Odbctest.mdb file.
Starts creating a datasheet (table).
This datasheet we give it two fields: ID and name.
Name this table Class.
Then we 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 "New ..." Button.
Select the ODBC driver you want to use. Select Microsoft Access Driver, of course, if you are using a different kind of database end, select the ODBC Driver for that database.
Click the Finish button.
This window will then appear for further setup.
Enter the data source name "WebDB". The description of the part can be arbitrarily entered, you know on it.
Press "SELECT ..." button, enter the location of the database file you want to link to. For example: the C:\odbctest.mdb here
Then click "Advanced ..." button. This screen will appear:
The part you need to enter is: Login name and password, we first set respectively ' WebUser ' and ' WebPassword '.
OK, okay. The ODBC Setup section is complete.
and build a test PHP Script.
Here is a test php script content, save it, for example, 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 the program before it is finished.
Odbc_close ($CNX);
Exit ();
}

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

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

if (! $cnx) {
Error_Handler ("There are errors in Odbc_connect", $CNX);
}

Send out a simple ODBC query. Returns an ODBC metric
$cur = Odbc_exec ($CNX, "Select Id,name from Class");
if (! $cur) {
Error_Handler ("There are errors occurring in odbc_exec (no indicators returned)", $CNX);
}


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

Remove the data that was successfully returned
while (Odbc_fetch_row ($cur))
{
$num _row++;
Crawl the data in the ID field
$id = Odbc_result ($cur, 1);
Fetching data from the ' Name ' field
$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 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 of seat
1 Ernest
2 Norman
3 Php/zend RC
4 Odbccooler
5, I'm number fifth.
Number 66th is me.
Total 6 people

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.