如何从SQL Server检索数据?

时间:2022-07-18 01:41:21

I was wondering why my query is returning null when I know there is data there.

当我知道那里有数据时,我想知道为什么我的查询返回null。

my query is as follows:

我的查询如下:

if (isset($_POST['noteid'])) 
    {
        $showNoteInfo = "SELECT Note FROM Notes WHERE NoteID = 2";
        $showNotes    = sqlsrv_query($conn, $showNoteInfo);

        var_dump($showNotes);
    }

I have tested $_POST['noteid'] and that displays an ID no problem, in theory this id will replace where I have the number 2 in my query.

我已经测试了$ _POST ['noteid']并且显示ID没问题,理论上这个id将替换我在查询中的数字2的位置。

However I know in my table in the Notes table where NoteID = 2 the text should be like this 如何从SQL Server检索数据?

但是我在Notes表中的表中知道NoteID = 2,文本应该是这样的

However var_dump displays "resource(7) of type (SQL Server Statement)"

但是var_dump显示“类型的资源(7)(SQL Server语句)”

And I have also tried a different method of displaying it and that returned as the query expected resource and was given NULL, so why is this query not getting any results?

我也尝试了一种不同的显示方法,并作为查询预期资源返回并被赋予NULL,那么为什么这个查询没有得到任何结果呢?

My connection details are in an include at the top of the page and are like this: http://pastebin.com/qz3tScdW

我的连接详细信息位于页面顶部的包含中,如下所示:http://pastebin.com/qz3tScdW

If you need anything else please ask.

如果您还有其他需求,请询问。

Underlying question, why is my Query returning NULL when I know theres data there?

基础问题,当我知道那里的数据时,为什么我的Query返回NULL?

1 个解决方案

#1


4  

You never actually try to retrieve your data. sqlsrv_query performs the database query, but it doesn't get the data. You need to use sqlsrv_fetch_array (or sqlsrv_fetch_object) for that:

您实际上从未尝试过检索数据。 sqlsrv_query执行数据库查询,但它不获取数据。您需要使用sqlsrv_fetch_array(或sqlsrv_fetch_object):

$stmt = sqlsrv_query($conn, $showNoteInfo);

if (sqlsrv_has_rows($stmt)) {
    $data = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC);
    var_dump($data['Note']);
} else {
    echo "No data found";
}

#1


4  

You never actually try to retrieve your data. sqlsrv_query performs the database query, but it doesn't get the data. You need to use sqlsrv_fetch_array (or sqlsrv_fetch_object) for that:

您实际上从未尝试过检索数据。 sqlsrv_query执行数据库查询,但它不获取数据。您需要使用sqlsrv_fetch_array(或sqlsrv_fetch_object):

$stmt = sqlsrv_query($conn, $showNoteInfo);

if (sqlsrv_has_rows($stmt)) {
    $data = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC);
    var_dump($data['Note']);
} else {
    echo "No data found";
}