我有两张桌子:
主机
+----+--------+
| 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查询)运行查询,但使用PHP
在sqlsrv
中运行,则会正确返回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'