php - 无法对数据库执行查询

标签 php mysql azure

我正在获取

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/

相关文章:

php - 我希望在用户选择选项后在我的 <select> 表单中选择最后选择的选项

mysql - 如何将文件从 mysql 托管站点同步到谷歌驱动器

mysql - 如何从 Ajax 调用或更改 SQL 查询中获取 "normalize"json 对象

php - 从数据库中获取值并将结果显示在数组中

azure - Terraform - 如何映射不同的 azure 环境变量文件?

Azure Databricks 和表单识别器 - 图像无效或受密码保护

c# - CloudBlockBlob 上传的非异步版本是否已弃用

phpmyadmin 页面显示源代码,Ubuntu 16.04 安装了 Vagrant 和 Puphpet

php - 循环遍历 MySQL 行并拆分。最初是 Excel 电子表格

php - 有限空间内的文本无限制,无换行