简体   繁体   English

PHP的MSSQL数据库查询不工作

[英]php mssql database query not working

I need some help. 我需要协助。 I am writing a small program to retrieve data from mssql database using php. 我正在编写一个小程序,使用php从mssql数据库检索数据。 When I write the query on sql management studio I get a result, but when I run the same query within php I dont get any results. 当我在sql management studio上写查询时,得到了结果,但是当我在php中运行相同的查询时,却没有得到任何结果。

the following is on init.php. 以下是init.php。 This is file containing the database connection strings 这是包含数据库连接字符串的文件

<?php

error_reporting(E_ALL);
$serverName = "SUPERMAN";

$connectionInfo = array('Database'=>'Eque', "UID"=>"develop", "PWD"=>"develop");
$conn = sqlsrv_connect($serverName, $connectionInfo);


if($conn) {
    "Connection established.<br />";
}else {
    "Connection could not be established.<br />";
    die(print_r(sqlsrv_errors(), true));
}

?>

the following is test page where the data is supposed to be displayed: 以下是应该显示数据的测试页:

<?php
error_reporting(E_ALL);
require_once 'scripts/init.php';

$query = "SELECT a.ID AS ApnID, b.Surname, b.Initials, b.Cell, b.Email, 
                            a.Deadline, a.NotifyEmail, a.NotifySMS, c.Name, a.RecvDate,a.Barcode
                        FROM dbo.Apn a
                        INNER JOIN dbo.Apt b ON a.AptID = b.ID
                        INNER JOIN dbo.Status c ON a.StatusID = c.ID";

//$params = array($id, $name);      
$stmt = sqlsrv_query( $conn, $query);

$record = sqlsrv_fetch_array($stmt);
                {
                    echo $record['Surname'];
                    echo '<br />';
                    echo $record['Initials']; 
                    echo '<br />';
                }



?>

I connects properly to the database, therefore I think the issue lies on the test page. 我已正确连接到数据库,因此我认为问题出在测试页面上。 Please Assist! 请协助!

Here 这里

if($conn) {
    "Connection established.<br />";
}else {
    "Connection could not be established.<br />";
    die(print_r(sqlsrv_errors(), true));
}

Try this 尝试这个

if($conn) {
    echo "Connection established.<br />";
}else {
    echo "Connection could not be established.<br />";
    die(print_r(sqlsrv_errors(), true));
}

and this 和这个

<?php

error_reporting(E_ALL);

As

    <?php  
    error_reporting(E_ALL);
   ini_set('display_errors', 1);

And this 和这个

die(print_r(sqlsrv_errors(), true));

as

die('Error: '.sqlsrv_errors());

This is no good too 这也不好

$record = sqlsrv_fetch_array($stmt);
                {
                    echo $record['Surname'];
                    echo '<br />';
                    echo $record['Initials']; 
                    echo '<br />';
                }

So 所以

while(false !== ($record = sqlsrv_fetch_array($stmt)))
                {
                    echo $record['Surname'];
                    echo '<br />';
                    echo $record['Initials']; 
                    echo '<br />';
                }

Or 要么

$record = sqlsrv_fetch_array($stmt);

echo $record['Surname'];
echo '<br />';
echo $record['Initials']; 
echo '<br />';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM