Home  >  Article  >  Backend Development  >  (Advanced) Method 2 of accessing MySQL database with PHP

(Advanced) Method 2 of accessing MySQL database with PHP

黄舟
黄舟Original
2017-02-06 10:09:461010browse

Use Adodb.Connection to access MySQL database

try{   
    $com = new COM("Adodb.Connection");
    $com->open("Driver={SQL Server};server=(local);uid=sa;pwd=*****;database=*****;");
    }catch(com_exception $e){
        die($e->getMessage());
    }
try{
    $rs = new COM("Adodb.RecordSet");
    $rs->open("select * from user_list",$com,1,1);
    }catch(com_exception $e){ 
        die($e->getMessage());
    }
$AllCount = $rs->RecordCount;
for($i=1;$i<$AllCount;$i++){
    echo($rs["u_id"]->value." ");
    $rs->MoveNext(); 
}
$rs->Close();
$rs = null; 
$com->Close(); 
$com = null;
?>

Use ODBC to connect to mysql

With an ODBC connection, you can connect to any database on any computer in your network, as long as ODBC Connection is available.

1. Open the ODBC Data Source Manager in the control panel and confirm that the mysql odbc driver already exists in the driver

2. Add a data source to the system DSN and enter

 data source name :mysqlodbc,
 user:root,
 password:root,

Select database and confirm

3. Enter the code

<?php
    $conn=odbc_connect(&#39;mysqlodbc&#39;,&#39;root&#39;,&#39;root&#39;);
    if (!$conn){exit("Connection Failed: " . $conn);}
    $sql="SELECT * FROM class";
    $rs=odbc_exec($conn,$sql);
    if (!$rs) {exit("Error in SQL");} 
    echo "<table><tr>";
    echo "<th>id</th>";
    echo "<th>name</th></tr>";  
    while (odbc_fetch_row($rs)){
        $id=odbc_result($rs,"id");
        $name=odbc_result($rs,"name");  
        echo "<tr><td>$id</td>"; 
        echo "<td>$name</td></tr>"; 
    }
odbc_close($conn);
echo "</table>";
?>

The above is the (advanced) method 2 of PHP accessing MySQL database. For more related content, please pay attention to PHP Chinese Net (www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn