0

我准备了对 MSSQL 数据库的查询。在 Manager 中执行时,它工作得非常好,但显然它不会向 PHP 脚本返回任何行。最有问题的部分可能是这个专栏:

Difference = t0.col1 - ISNULL((SELECT t1.col1 FROM table AS t1 WHERE t1.date =
(SELECT MAX(t2.date) FROM table AS t2 WHERE t2.date < t0.date)), 0)

或者它可能是这样的:

(SELECT NORMA FROM base2.dbo.ODB_S_PO WHERE <condition>)

将 PHP 与 MS SQL 一起使用时,我可以使用子查询吗?这些查询可以指向不同的数据库吗?我可以在一个 PHP 查询中排列多个语句,例如定义一个变量吗?

编辑:发布完整查询:

SELECT t0.TA_DATA,
col1+' '+col2,
t0.TA_STAN_LICZNIKA,
Difference = t0.TA_STAN_LICZNIKA - ISNULL((SELECT t1.TA_STAN_LICZNIKA FROM tab1 AS t1 WHERE t1.TA_DATA =
(SELECT MAX(t2.TA_DATA) FROM tab1 AS t2 WHERE t2.TA_DATA < t0.TA_DATA AND t2.TA_ID_PJ = '03102')), 0),
t0.TA_ILE,
(SELECT NORMA FROM db2.dbo.ODB_S_PO WHERE KOD = '03102')
FROM tab1 AS t0
INNER JOIN tab2 ON t0.TA_ID_KI = KI_ID
WHERE t0.TA_ID_PJ = '03102' ORDER BY t0.TA_DATA DESC

这是完整的查询。使用一个数据库我引用同一个表三次来计算差异并尝试访问另一个数据库以获取某个变量。从另一个数据库中省略选择并在单独的查询中获取变量并没有帮助。

EDIT2:我尝试使用 Management Studio 中使用的帐户,以及专用于 Web 界面的帐户。根据建议,这是(几乎)完整的 PHP 脚本:

$car = $_POST['id'];
include '../bd.php';
$nazwa_hosta = '';
$username = '';
$passwd = '';
$baza = 'gps';
//print_r($_POST);
$res_dbase = new BD($nazwa_hosta, $username, $passwd, $baza);
$res_dbase->bd_polacz();
$query = "SELECT MARKA, NRR,
 CONVERT(varchar, [DATE_TIME], 104) AS Dzien,
 DATE_TIME,
 CAST(DISTANCE AS FLOAT) / 1000 AS DIST,
 Diff = CAST(t0.DISTANCE - ISNULL((SELECT t1.DISTANCE FROM reports AS t1 WHERE t1.ID =
(SELECT MAX(t2.ID) FROM reports AS t2 WHERE t2.ID < t0.ID AND t2.DEVICE_ID = t0.DEVICE_ID)), 0) AS FLOAT) / 1000,
 IGNITION_SWITCH_STATE AS Stac,
 SPEED,
 KI_NAZWISKO+' '+KI_IMIE AS Kier
 FROM Mediator.dbo.ODB_S_PO AS pojlist
 INNER JOIN mikrostacje.dbo.MS_BLACK_BOX ON pojlist.KOD = '0'+CONVERT(VARCHAR, BB_ID_PJ)
 INNER JOIN gps.dbo.reports AS t0 ON DEVICE_ID = BB_ID
 INNER JOIN mikrostacje.dbo.MS_KIEROWCA ON KI_ID = (SELECT IDK_ID_KI FROM mikrostacje.dbo.MS_ID_KARTA_KIEROWCA WHERE IDK_RFID_NO = RFID)
 WHERE KOD = '$car'
 ORDER BY DATE_TIME DESC";
echo $query."<br />";
if(!($res_dbase->bd_zapytaj($query))) //the code fails here - otherwise I wouldn't get any message from the server. And the only message I get is that it changes the context to 'gps'
{
    echo mssql_get_last_message();
}
$marka = "";
$nrr = "";
$result = "";
while(false !== ($row = $res_dbase->bd_pobierzTabliceAsocjacyjna()))
{
    $marka = $row['MARKA'];
    $nrr = $row['NRR'];
    $result .= "<tr><td>".$row['Dzien']."</td><td>".$row['DATE_TIME']."</td><td>".$row['Dist']."</td><td>".$row['Diff']."</td><td>".$row['Stac']."</td><td>".$row['Kier']."</td></tr>".PHP_EOL;
}
$head = "<div style='text-align: center; margin: auto'><b>$nrr $marka</b><table>";
$result .= "</table></div>";
$echo = remove_pl($head.$result, "latin2"); // Removing Polish characters, since the target is encoded in Windows-1250
echo $echo;
4

0 回答 0