Php 使用Azure Web App连接到Azure数据库
我正在尝试使用以下代码连接到Azure sql数据库:Php 使用Azure Web App连接到Azure数据库,php,azure,azure-devops,azure-sql-database,Php,Azure,Azure Devops,Azure Sql Database,我正在尝试使用以下代码连接到Azure sql数据库: <?php //Constants to connect with the database define('DB_USERNAME', 'username'); define('DB_PASSWORD', 'pass'); define('DB_HOST', 'xyz-server.database.windows.net'); define('DB_NAME', 'xyz_db'); 我只是从Azure开始。也许我错过了什么。如
<?php
//Constants to connect with the database
define('DB_USERNAME', 'username');
define('DB_PASSWORD', 'pass');
define('DB_HOST', 'xyz-server.database.windows.net');
define('DB_NAME', 'xyz_db');
我只是从Azure开始。也许我错过了什么。如果您能简单地指出如何连接我的数据库和web应用程序(我正在使用php文件连接到数据库),那就太好了。在我看来,您的代码试图连接到MySQL而不是MSSQL 要连接到MSSQL,请使用以下命令:
<?php
$serverName = "your_server.database.windows.net"; // update me
$connectionOptions = array(
"Database" => "your_database", // update me
"Uid" => "your_username", // update me
"PWD" => "your_password" // update me
);
//Establishes the connection
$conn = sqlsrv_connect($serverName, $connectionOptions);
$tsql= "SELECT TOP 20 pc.Name as CategoryName, p.name as ProductName
FROM [SalesLT].[ProductCategory] pc
JOIN [SalesLT].[Product] p
ON pc.productcategoryid = p.productcategoryid";
$getResults= sqlsrv_query($conn, $tsql);
echo ("Reading data from table" . PHP_EOL);
if ($getResults == FALSE)
echo (sqlsrv_errors());
while ($row = sqlsrv_fetch_array($getResults, SQLSRV_FETCH_ASSOC)) {
echo ($row['CategoryName'] . " " . $row['ProductName'] . PHP_EOL);
}
sqlsrv_free_stmt($getResults);
?>
来源:在我看来,您的代码试图连接到MySQL而不是MSSQL 要连接到MSSQL,请使用以下命令:
<?php
$serverName = "your_server.database.windows.net"; // update me
$connectionOptions = array(
"Database" => "your_database", // update me
"Uid" => "your_username", // update me
"PWD" => "your_password" // update me
);
//Establishes the connection
$conn = sqlsrv_connect($serverName, $connectionOptions);
$tsql= "SELECT TOP 20 pc.Name as CategoryName, p.name as ProductName
FROM [SalesLT].[ProductCategory] pc
JOIN [SalesLT].[Product] p
ON pc.productcategoryid = p.productcategoryid";
$getResults= sqlsrv_query($conn, $tsql);
echo ("Reading data from table" . PHP_EOL);
if ($getResults == FALSE)
echo (sqlsrv_errors());
while ($row = sqlsrv_fetch_array($getResults, SQLSRV_FETCH_ASSOC)) {
echo ($row['CategoryName'] . " " . $row['ProductName'] . PHP_EOL);
}
sqlsrv_free_stmt($getResults);
?>
来源:由于您正在尝试连接到Azure SQL,如果您安装了此扩展,则可以使用驱动程序。请参见
php-m
$config = [
'dsn' => 'dblib:host=xyz-server.database.windows.net;dbname=xyz_db',
'user' => 'username',
'pass' => 'password',
];
$connection = new PDO($config['dsn'], $config['user'], $config['pass']);
$sth = $connection->prepare('Your query comes here;');
$sth->execute();
$rows = $sth->fetchAll(PDO::FETCH_CLASS);
foreach ($rows as $row) {
// Do the processing here
}
由于您正试图连接到Azure SQL,如果您安装了此扩展,则可以使用驱动程序。请参见
php-m
$config = [
'dsn' => 'dblib:host=xyz-server.database.windows.net;dbname=xyz_db',
'user' => 'username',
'pass' => 'password',
];
$connection = new PDO($config['dsn'], $config['user'], $config['pass']);
$sth = $connection->prepare('Your query comes here;');
$sth->execute();
$rows = $sth->fetchAll(PDO::FETCH_CLASS);
foreach ($rows as $row) {
// Do the processing here
}
是否相应地打开了SQL数据库上的防火墙?此错误似乎不是防火墙problem@silent防火墙是on@BanujanBalendrakumar我尝试了,现在我得到了以下错误:Message:mysqli::uu construct():(HY000/9002):找不到您尝试的服务器名称。请使用正确的名称,然后重试。请检查您的服务器名称xyz server。上的
不是问题,您应该允许客户端的IP。您是否相应地打开了SQL数据库上的防火墙?此错误似乎不是防火墙problem@silent防火墙是on@BanujanBalendrakumar我试过了,现在我得到了这个错误:Message:mysqli::uu construct():(HY000/9002):找不到您尝试的服务器名称。请使用正确的名称,然后重试。请检查您的服务器名xyz server。
上的不是问题,您应该允许客户端的IP