本文主要给大家介绍如何使用PHP 通过 ODBC 连接 Mysql 数据库,关于其中的原理这里就不再多介绍了,请大家直接测试下边的代码:
Copy to Clipboard引用的内容:
[www.veryhuo.com] <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>PHP and ODBC: XHTML Example 1</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<?php
$conn = odbc_connect(
"DRIVER={MySQL ODBC 3.51 Driver};Server=localhost;Database=phpodbcdb",
"username", "password");
if (!($conn)) {
echo "<p>Connection to DB via ODBC failed: ";
echo odbc_errormsg ($conn );
echo "</p>";
}
$sql = "SELECT 1 as test";
$rs = odbc_exec($conn,$sql);
echo "<table><tr>";
echo "<th>Test</th></tr>";
while (odbc_fetch_row($rs))
{
$result = odbc_result($rs,"test");
echo "<tr><td>$result</td></tr>";
}
// 辛苦整理教程,勿恶意采集 VeryHuo_COM
odbc_close($conn);
echo "</table>";
?>
</body>
</html>