PDO PHP选择不适用于mssql的不同查询

时间:2016-07-21 15:57:08

标签: php sql-server pdo

我有一个从我的MSSQL Server中提取数据的网站。我正在使用函数来构建报表。这就是我所拥有的:

function BeginTable($rowCount,$headings,$searchValue,$ReportName,$OneButton,$NewSearch)
{
    try{
    $StateSelectSQL = "select distinct State from pmdb.MaterialTracking where State is not null";
    var_dump($StateSelectSQL);echo " What!<br>";

    $getSelect = $conn->query($StateSelectSQL);
    var_dump($getSelect);echo " When!<br>";

    $StateSelectNames = $getSelect->fetchALL(PDO::FETCH_ASSOC);
    var_dump($StateSelectNames);echo " Where!<br>";
    }
    catch(Exception $e)
    {
        echo "Something went wrong";
        die(print_r($e->getMessage()));
    }

我也试过了:

    try{
        $StateSelectSQL = "select distinct State from pmdb.MaterialTracking where State is not null";
        var_dump($StateSelectSQL);echo " What!<br>";

         $getSelect = $conn->prepare($StateSelectSQL);
         $getSelect->execute();

        //$getSelect = $conn->query($StateSelectSQL);
        //var_dump($getSelect);echo " When!<br>";

        $StateSelectNames = $getSelect->fetchALL(PDO::FETCH_ASSOC);
        var_dump($StateSelectNames);echo " Where!<br>";
    }
    catch(Exception $e)
    {
        echo "Something went wrong<br>";
        die( print_r( $e->getMessage()));
    }

第二个和第三个var_dump从未显示任何内容,其余代码(此处未显示)也无法运行。如果我注释掉$getSelect$StateSelectNames行(其中包含var_dump),那么其他所有内容都会有效。

这是我的included上面Function的DBConn.php文件:

    $conn = new PDO("sqlsrv:server=$servername;database=$dbname", $username,$password);
    //set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $conn->setAttribute(PDO::SQLSRV_ATTR_QUERY_TIMEOUT, 10);

线$getSelect = $conn->query($StateSelectSQL);有什么问题我无法弄明白。我稍后尝试在我的foreach中使用它:

foreach($conn->query($StateSelectSQL) as $StateName)

但这也不起作用。它再次停在这条线上并且不再继续。我能想到的唯一一件事就是我的SQL搞砸了,但是当我在SSMS中运行它时它运行正常!

发生了什么?

2 个答案:

答案 0 :(得分:1)

在使用fetchAll之前,请尝试准备并执行SQL。如果您还没有将语句包装在try catch中,也可以考虑启用异常模式 - 这应该标记任何问题(例如,您的应用程序数据库用户没有访问模式的权限,或语法错误等)

例外:

See this stack overflow post for info about how to enable

代码:

try { 
    $sql = "
    SELECT DISTINCT State
      FROM pmdb.MaterialTracking
     WHERE State IS NOT NULL
    ";

    $sth = $conn->prepare($sql);
    $sth->execute();

    $rowset = $sth->fetchAll(PDO::FETCH_ASSOC);
    print_r($rowset);

} catch PDOException($err) {
    echo "Something went wrong".
    echo $err;
}

答案 1 :(得分:0)

我整天拔头发后想出来了!我必须在includefunction我的DBConn.php。在此之后它起作用了。我不知道为什么这很重要,因为它在文件的开头是included。如果有人能够解释为什么我会感激不尽!

现在看起来像这样:

function BeginTable($rowCount,$headings,$searchValue,$ReportName,$OneButton,$NewSearch)
{
    try{
        include("DBConn.php");
        $SelectSQL = "select distinct State from pmdb.MaterialTracking where State is not null order by State";            

        $getSelect = $conn->prepare($SelectSQL);

        $getSelect->execute();

        $StateSelectNames = $getSelect->fetchALL(PDO::FETCH_ASSOC);

        }
        catch(Exception $e)
        {
            echo "Something went wrong<br>";
            die( print_r( $e->getMessage()));
        }