odbc_ok.php
<?php
$servername = "192.168.0.28"; // MSSQL 服务器名称或 IP 地址
$database = "test"; // 要连接的数据库名称
$username = "myusername"; // 数据库用户名
$password = "password7fgg"; // 数据库密码
// 创建 ODBC 连接
$conn = odbc_connect("Driver={SQL Server};Server=$servername;Database=$database", $username, $password);
if ($conn) {
echo "连接成功";
// 执行查询或其他数据库操作
$query = "SELECT * FROM [test].[dbo].[SUUsers]";
$result = odbc_exec($conn, $query);
while ($row = odbc_fetch_array($result)) {
echo $row['Password']."<br/>";
}
// ...
// 关闭连接
odbc_close($conn);
} else {
echo "连接失败";
}
?>
odbc_pdo_odbc_ok.php
<?php
/**
* Created by PhpStorm.
* User: 16666739
* Date: 2023/10/5
* Time: 13:11
*/
//$dsn = 'odbc:Driver={SQL Server Native Client 11.0};SERVER=192.168.0.28;DATABASE=test';
$dsn = 'odbc:Driver={ftp4};SERVER=192.168.0.28;DATABASE=test';
$username = 'myusername';
$password = 'password7fgg';
$dsn = "odbc:Driver={SQL Server};Server=192.168.0.28;Database=test;UID=myusername;PWD=password7fgg;";
try {
$conn = new PDO($dsn);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "SELECT * FROM [test].[dbo].[SUUsers]";
$stmt = $conn->prepare($sql);
$stmt->execute();
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach($results as $key=>$value){
var_dump($value);
}
echo "Your table has " . count($results) . " rows.\n";
echo 'Connected to the database successfully!';
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
mssql_sqlsrv_ok.php
<?php
$serverName = "192.168.0.28"; //数据库服务器地址
$uid = "myusername"; //数据库用户名
$pwd = "password7fgg"; //数据库密码
$connectionInfo = array("UID"=>$uid, "PWD"=>$pwd, "Database"=>"test");
$conn = sqlsrv_connect($serverName, $connectionInfo);
if( $conn == false)
{
echo "连接失败!";
var_dump(sqlsrv_errors());
exit;
}else{
$query = "SELECT * FROM [test].[dbo].[SUUsers]";
$result = sqlsrv_query($conn, $query);
var_dump($result);
while ($row = sqlsrv_fetch_array($result)) {
echo $row['Password']."<br/>";
}
echo "链接成功";
}
mssql_pdo_sqlsrv_ok.php
<?php
/**
* Created by PhpStorm.
* User: 16666739
* Date: 2023/10/5
* Time: 13:25
*/
$servername = "192.168.0.28";
$username = "myusername";
$password = "password7fgg";
$dbname = "test";
$conn = new PDO("sqlsrv:Server=$servername;Database=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "SELECT * FROM [test].[dbo].[SUUsers]";
$stmt = $conn->prepare($sql);
$stmt->execute();
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach($results as $key=>$value){
var_dump($value);
}
echo "Your table has " . count($results) . " rows.\n";
if ($result->rowCount() > 0) {
foreach ($result as $row) {
echo "loginID: " . $row["loginID"]. " - Password: " . $row["Password"]. "<br>";
}
} else {
echo "0 results";
}
$conn = null;