MSSql仅返回echo sqlsrv_query()上的资源ID#3响应

时间:2017-11-30 07:03:58

标签: php sql-server

在我的所有api中遇到同样的问题.. 在echo sqlsrv_query();给出响应资源ID#3。 也无法找到任何方式来查看返回的数据是否为空。 可能是因为资源ID#3响应我的查询数据受到阻碍。 请帮忙。

以下是我的代码。

    apply plugin: 'com.android.application'

    dependencies {
    compile project(':library')
    compile project(':camerafragment')
    compile 'com.google.android.gms:play-services:11.0.0'
    compile 'com.squareup.picasso:picasso:2.5.2'
    compile 'com.mcxiaoke.volley:library:1.0.17'
    compile 'com.android.support:appcompat-v7:26.0.0-alpha1'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    compile 'com.android.support:recyclerview-v7:26.0.0-alpha1'
    compile 'com.android.support:cardview-v7:26.0.0-alpha1'
    compile 'com.google.firebase:firebase-messaging:11.0.0'
    compile 'com.google.android.gms:play-services-maps:11.0.0'
    compile 'com.facebook.android:facebook-android-sdk:[4,5)'
    compile 'com.android.support:design:26.0.0-alpha1'
    compile 'com.amulyakhare:com.amulyakhare.textdrawable:1.0.1'
    compile 'com.jakewharton:butterknife:7.0.1'
    compile 'com.google.android.gms:play-services-auth:11.0.0'
    compile 'net.gotev:uploadservice:2.1'
    compile 'com.google.firebase:firebase-auth:11.0.0'
    compile 'com.google.code.gson:gson:2.8.0'
    compile 'com.android.support:support-v4:26.0.0-alpha1'
    }

    android {

    compileSdkVersion 27
    buildToolsVersion "27.0.0"
    dexOptions {
        javaMaxHeapSize "4g"
    }
    defaultConfig {
        applicationId "com.trashmap"
        minSdkVersion 17
        targetSdkVersion 27

        // Enabling multidex support.
        multiDexEnabled true
        versionCode 17
        versionName "1.16"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
           // shrinkResources true//new add to reduce size
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }

    }



      sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
            res.srcDirs = ['res']
        }
    }
}

apply plugin: 'com.google.gms.google-services'

1 个答案:

答案 0 :(得分:1)

如果您read the documentation,您会发现sqlsrv_query返回“成功的语句资源,FALSE如果发生错误。”您无法打印资源,因此需要此行为。

接下来,您实际上并未输出任何JSON,这可能解释了您遇到API问题的原因。 die立即停止程序执行。

如果您正在输出JSON,那么您将使用wrong content type发送它,并且您将以不一致的格式发送它。错误条件在status元素中包含status_codedata,而成功则没有。

你是fetching your database query作为关联数组,但后来尝试使用数字索引来获取列值。同样,如果您只获得一行,则无需循环结果。

最后,通过盲目地将用户提供的数据插入到查询中,您可以自己打开SQL injection。这是一个非常糟糕的主意,并且允许任何人在没有身份验证的情况下登录。

尝试这样的事情:

<?php
$connectionInfo = [ 
    "UID"          => $uid,
    "PWD"          => $pwd,
    "Database"     => $databaseName,
    "CharacterSet" => "UTF-8",
];

$conn = sqlsrv_connect($serverName, $connectionInfo); 
if ($conn === false) {
    $data = [
        "status_code" => "203",
        "status"      => "Database error",
    ];
    header("Content-Type: application/json");
    echo json_encode($data);
    exit;
}

$username= $_REQUEST['username'];
$password= $_REQUEST['password'];

if(empty($username) || empty($password)) {
    $data = [
        "status_code" => "203",
        "status"      => "Parameter Missing !!",
    ];
    header("Content-Type: application/json");
    echo json_encode($data);
    exit;
}

$query = "SELECT NAME FROM SMS_ACCESS WHERE LOGINID = ? AND PASSWORD = ?";
$parameters = [$username, $password];
$stmt = sqlsrv_query($conn, $query, $parameters);
if ($stmt === false) {       
    $data = [
        "status_code" => "203",
        "status"      => "Error in execution",
    ];
    header("Content-Type: application/json");
    echo json_encode($data);
    exit;
}
$row = sqlsrv_fetch_array($stmt);
$data = [
    "status_code" => "200",
    "status"      => "success",
    "data"        => [
        "name"    => $row[0]
    ],
];
header("Content-Type: application/json");
echo json_encode($data);

sqlsrv_free_stmt($stmt);    
sqlsrv_close($conn);