1

When querying a SQL Server database from PHP with this statement:

SELECT [Value] FROM sys.extended_properties WHERE name='MS_Description'

I get this error message:

"Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[IMSSP]: Invalid type.' in C:\inetpub\wwwroot\index.php:104 Stack trace: #0 C:\inetpub\wwwroot\index.php(104): PDOStatement->fetch(2) #1 {main} thrown in C:\inetpub\wwwroot\index.php on line 104"

The php that I'm using is below, line 104 is the start of the While loop:

$sql = "SELECT [Value] FROM sys.extended_properties WHERE name='MS_Description'";
global $pdo;
$qry = $pdo->query($sql);
if (!$qry) {
    exit('<pre>' . print_r($qry->errorInfo()));
}
while($row = $qry->fetch(PDO::FETCH_ASSOC)) {
    foreach ($row as $key => $value) {
        echo '<p>Key: ' .$key. ', Value: ' .$value. '</p>';
    }
}

The code above works fine when selecting from any other table or view e.g:

SELECT * FROM sys.events

If I execute the statement via SSMS it works fine, any ideas why it doesn't work via PHP?

gbavba
  • 116
  • 2
  • 9

1 Answers1

1

Casting the columns being selected as varchar(255) solved the problem i.e.

$sql = "SELECT CAST([Value] AS VARCHAR(255)) FROM sys.extended_properties WHERE name='MS_Description'";

Got to the solution based on the answers to this question and this question.

Community
  • 1
  • 1
gbavba
  • 116
  • 2
  • 9