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);