sql server - PDO PHP select distinct query not working for mssql -


i've got website pulling data mssql server. using functions build tables reports. here's i've got:

function begintable($rowcount,$headings,$searchvalue,$reportname,$onebutton,$newsearch) {     try{     $stateselectsql = "select distinct state pmdb.materialtracking state 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()));     } 

i tried too:

    try{         $stateselectsql = "select distinct state pmdb.materialtracking state 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()));     } 

the second , third var_dump's never show , rest of code (not shown here) doesn't run. if comment out $getselect , $stateselectnames lines (with var_dump's under them) else works.

here dbconn.php file included above function:

    $conn = new pdo("sqlsrv:server=$servername;database=$dbname", $username,$password);     //set pdo error mode exception     $conn->setattribute(pdo::attr_errmode, pdo::errmode_exception);     $conn->setattribute(pdo::sqlsrv_attr_query_timeout, 10); 

what wrong line $getselect = $conn->query($stateselectsql); can't figure out. tried using later in foreach this:

foreach($conn->query($stateselectsql) $statename) 

but doesn't work either. again stops @ line , doesn't go further. thing can think of sql messed up, when run in ssms works fine!

what's going on?

try preparing , executing sql before using fetchall. consider enabling exception mode if haven't , wrapping statement in try catch - should flag issues (e.g. applications database user not having permission access schema, or syntax error etc)

exceptions:

see stack overflow post info how enable

and code:

try {      $sql = "     select distinct state       pmdb.materialtracking      state 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; } 

Comments