PHP sqlsrv查询不返回该值

时间:2016-06-24 09:32:41

标签: php sql sql-server sqlsrv

我有两张桌子:

主机

+----+--------+
| ID | name   |
+----+--------+
| 1  | mycomp |
+----+--------+

所有者

+----+--------+------+
| ID | hostID | name |
+----+--------+------+
| 1  | 1      | John |
+----+--------+------+

我需要主持人。这应该是查询本身:

SELECT
  o.name
FROM
  owner o 
LEFT OUTER JOIN
  hosts h ON h.ID=o.hostID
WHERE
  h.name = 'mycomp'

如果我在任何地方(例如Flyspeed SQL查询)运行查询,但使用PHPsqlsrv中运行,则会正确返回owner。在PHP我没有回来。这是代码:

$connectionInfo = array("Database"=>$db, "UID"=>$usr, "PWD"=>$pwd);
$connection = sqlsrv_connect($server, $connectionInfo);
if(!$connection) {
  die('Unable to connect to the database.');
}
$host = "mycomp";
$owner = "";
$options =  array("Scrollable" => SQLSRV_CURSOR_KEYSET);
$result = sqlsrv_query(
  $connection, 
  'SELECT
    o.name
  FROM
    owner o 
  LEFT OUTER JOIN
    hosts h ON h.ID=o.hostID
  WHERE
    h.name = ?', 
  array($host), 
  $options
);

if( $result == false ) {
     die( print_r( sqlsrv_errors(), true));
}
$row_count = sqlsrv_num_rows($result);

if (sqlsrv_num_rows($result) < 1)
{
    $owner = "n/a";
}
else {
    $temp = sqlsrv_fetch_array($result);
    $owner = $temp[0];
    sqlsrv_close($connection);
    $result = "";
}
echo $owner; //prints 'n/a'

0 个答案:

没有答案