我正在获取
Invalid query An attempt was made to access a socket in a way forbidden by its access permissions error
我正在尝试使用 php 连接到我的 azure sql 服务器。 我有以下代码:
$serverName = "cosy.database.windows.net, 1433"; //serverName\instanceName
$connectionInfo = array( "Database"=>"cosy", "UID"=>"eocribin", "PWD"=>"password");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn ) {
echo "Connection established.<br />";
$query = sprintf("SELECT username FROM Users
WHERE username=eoin");
$result = mysql_query($query);
echo $result;
// Check result
// This shows the actual query sent to MySQL, and the error. Useful for debugging.
if (!$result) {
$message = 'Invalid query: ' . mysql_error() . "\n";
$message .= 'Whole query: ' . $query;
die($message);
}
不确定是否是查询问题或数据库的实际权限问题,但我确实连接成功,然后出现错误,所以我知道这是连接。
最佳答案
您似乎尝试连接到 Azure SQL Server,但您在代码中使用了 mysql_query($query)
。
在SQLServer中查询,可以引用http://php.net/manual/zh/function.sqlsrv-fetch-array.php .
这是代码片段:
if ($conn) {
echo "Connection established.<br />";
$query = sprintf("SELECT 1 as test");
$stmt = sqlsrv_query($conn, $query);
if ($stmt === false) {
die(print_r(sqlsrv_errors(), true));
}
while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
print_r($row);
}
sqlsrv_free_stmt($stmt);
}
关于php - 无法对数据库执行查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34513303/