使用php保存sql结果到变量


Save sql result to variable using php

我有这样的查询:

$result = "SELECT MAX(N) as maxid FROM (
    SELECT SUBSTRING(id_f, 2,len(id_f) -1)  as N 
    From formas  WHERE id_f LIKE '%D%'
    ) t" ;  
$res = odbc_exec($connection, $result) or die(odbc_error());

现在,如果我把查询在SQL SERVER我得到正确的结果。我需要的是将maxid保存为变量。怎么做呢?

谢谢

您需要的是函数odbc_fetch_array。

$result = "SELECT MAX(N) as maxid FROM (
    SELECT SUBSTRING(id_f, 2,len(id_f) -1)  as N 
    From formas  WHERE id_f LIKE '%D%'
    ) t" ;  
$res = odbc_exec($connection, $result) or die(odbc_error());
$info = odbc_fetch_array($res);
$content[] = $info;
$maxN = $content[0]

对于多行查询,需要将函数封装在while循环中:

while($row = odbc_fetch_array($res)) 
{ 
    print_r($row); 
}