Jump to content

Recommended Posts

I'm working with PDO and MS SQL Server.  For some reason, "SELECT * FROM [table][tr][td]" doesn't return any rows.  However,

"SELECT [first col name], [second col name] FROM [table][tr][td]" does work. 

 

"SELECT * FROM [table][tr][td]" does work when connecting to a mysql database using PDO. 

 

Any ideas? Am I missing something really fundamental here?

 

Here's the code:

 

/*********************************************************

SIMPLE SELECT USING PDO COMMANDS AGAINST MYSQL DB -- THIS WORKS.

*********************************************************/

//MYSQL Connection information

$db_myHost = "localhost"; 

$db_myUser= "root";

$db_myPassword = "";

$db_myDatabase = "cdcol";

 

//Instantiate a new PDO object, passing in the connection information to the constructor

$dbPDO = new PDO('mysql:host='.$db_myHost.';dbname='.$db_myDatabase, $db_myUser, $db_myPassword);

 

//Iterate through and display the results

echo '<h2>USING PDO COMMANDS WITH MYSQL DB</h2><p><ul>';

foreach ($dbPDO->query($sql) as $row)

{

      //print_r($row);

echo '<li>'.$row['titel'].'</li>';

}

echo '</ul>';

 

//close the connection

$dbPDO = null;

 

/************************************************************

USING PDO WITH MS SQL SERVER 2000 -- "SELECT *" DOESN'T WORK

************************************************************/

//MSSQL Connection information

$db_myHost = "myserver";

$db_myUser= "testuser";

$db_myPassword = "test";

$db_myDatabase = "northwind";

 

//Instantiate a new PDO object, passing in the connection information to the constructor

try

{

$dbPDO = new PDO('mssql:host='.$db_myHost.';dbname='.$db_myDatabase, $db_myUser, $db_myPassword);

}

catch  (PDOException $e)

{

    echo "Error!: " . $e->getMessage() . "<br/>";

    die();

}

 

//the query

$sql = "SELECT * FROM Employees"; //WEIRD - this does not work

//$sql = "select lastname, firstname from employees"; //Seems you have to specify each column you want.

 

//Iterate through and display the results

echo '<h2>USING PDO COMMANDS WITH MSSQL DB</h2><p>';

echo $sql;

echo '<ul>';

 

$count = $dbPDO->exec($sql);

echo 'count: '.$count; //NO COUNT RETURNED

try

{

$result = $dbPDO->query($sql);

foreach ($result as $row)

{

echo '<li>'.$row['lastname'].'</li>';

}

}

catch (PDOException $e)

{

    print "Error!: " . $e->getMessage() . "<br/>";

    die();

}

 

echo '</ul>';

 

 

//close the connection

$dbPDO = null;

/***********************************************/

 

Thanks for any help you might be able to offer!

Link to comment
https://forums.phpfreaks.com/topic/64763-solved-pdo-with-ms-sql-server/
Share on other sites

Try forcing it to throw an exception on error:

 

try
   {
   $dbPDO = new PDO('mssql:host='.$db_myHost.';dbname='.$db_myDatabase, $db_myUser, $db_myPassword);
   $dbPDO->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
   }
catch  (PDOException $e)
   {
    echo "Error!: " . $e->getMessage() . "
";
    die();
   }

Thanks for the tip.  That DID expose an exception resulting from the fact that, apparently. PDO doesn't play well with SQL Server's ntext data type -- hence, the query failed.  Changing that to a text data type allowed me to move on to uncover the coding errror: I did not type the field names with the proper case.  Changing $row["lastname"] to $row["LastName"] did the trick.  Seems I'm still paying for getting my first coding experience in Visual Basic...

 

Anyway, problem solved.  Thanks, again.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.