php odbc_connect () function description and example code

Source: Internet
Author: User
Keywords Network programming PHP tutorial
Tags close closed code connect data data sources echo example

Connect to odbc
The odbc_connect () function is used to connect to odbc data sources. The function has four parameters: data source name, user name, password, and optional pointer type parameters.

The odbc_exec () function is used to execute sql statements

$ db_user = "dbuser"; / / define the connection user name
$ db_pass = "dbpass"; / / connect to the user's corresponding password
$ dsn = "dsn"; / / define dsn data source
if (odbc_connect ($ dsn, $ db_user, $ db_pass)) // connect odbc data source
{
echo "successfully connected to odbc data source"; / / If successful output content
}
else
{
echo "problem connecting to odbc!"; // if the output failed
}

odbc_close_all (); // Close all open connections
echo "all open odbc connections have been closed!"; / / output function after the implementation of the function

Look at a more advanced host connection

$ db_host = "server.mynetwork"; / / define the host name
$ db_user = "dbuser"; / / define the connection user name
$ db_pass = "dbpass"; / / define the user's password
$ dsn = "dsn"; // Define dsn
$ result = odbc_pconnect ($ dsn, $ db_user, $ db_pass); // Open persistent odbc connection
if ($ result) / / to determine the results
{
echo "Open a persistent connection"; / / Successful implementation of the output
}
echo "<br>";
if (odbc_close ($ result)) // try to close the connection
{
echo "closed?"; // if the output is successfully closed
}
else
{
Echo "can not be closed!"; / / If the output fails to close the output
}
Query database tutorial save

Data source to connect

$ my_sql = "select * from usertable"; / / Define the sql statement
$ result = odbc_do ($ myconn, $ my_sql); // Execute the sql statement
echo odbc_num_rows ($ result);
echo "<table border =" 1 "> n";
echo "<tr> n";
echo "<td> id </ td> n";
echo "<td> name </ td> n";
echo "<td> address </ td> n";
echo "</ tr> n";
while (odbc_fetch_row ($ result))
{
echo "<tr> n";
echo "<td>". odbc_result ($ result, 1). "</ td> n";
echo "<td>". odbc_result ($ result, 2). "</ td> n";
echo "<td>". odbc_result ($ result, 3). "</ td> n";
echo "</ tr> n";
}
echo "</ table>";
Some related

odbc_free_result ($ result); / / Release the memory used to execute sql statement
echo "has successfully released the result set occupied memory!";
odbc_rollback ($ myconn) Cancel all uncommitted operations
odbc_commit ($ myconn) // Submit all uncommitted operations
odbc_autocommit ($ myconn, false); // Disable automatic commit
odbc_columnprivileges ($ myconn, "dbuser", "admin", "usertable", "name"); // List the columns and permissions for the given table
$ result = odbc_columns ($ myconn); // Lists the names of the columns of the specified table
echo odbc_result_all ($ result);

Related Article

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.