0

Trying to see if I can make PDO open my mssql database on my server. With vbscript my call to the connection looks like this:

set MyConn = Server.CreateObject("ADODB.Connection")
MyConn.Open("dsn=MYDSN;uid=MYUID;pwd=MYPWD;DATABASE=MYDATABASE;APP=ASP Script")

Then when trying to port this over to php using PDO I was unable to find any information on using DSN with PDO.

Here is what I have so far:

try {
    $conn = new PDO('mssql:Server=localhost;Database=MYDSN','MYUID','MYPWD') or die('error');
    $conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
    $sql = "SELECT name FROM people";
    $qresult = $conn->prepare($sql);
    $qresult->execute();
    foreach ($qresult->fetch(PDO::FETCH_ASSOC) as $row){
        echo $row['name'].'<br/>';
    }
} catch (PDOException $e) {
    print "Error!: ".$e->getMessage()."<br/>";
    die();
}

But this is what I get

Error!: SQLSTATE[HY000]: General error: 10007 Invalid object name 'name'. [10007] (severity 5) [(null)]

My guess is because I cannot put the dsn anywhere in the pdo code.

kqlambert
  • 2,693
  • 6
  • 33
  • 50
  • Why do you think it's DSN related? – Your Common Sense Aug 19 '13 at 20:36
  • with vbscript I am required to use a dsn to connect to the correct database, so I do not see how pdo would be able to get information from the correct database without a dsn – kqlambert Aug 19 '13 at 20:41
  • I can't get your reasoning, sorry. It seems to me that you are saying PDO cannot use DSN because vbscript can. Anyway, the error is obviously caused by query, not DSN – Your Common Sense Aug 19 '13 at 20:42
  • I am saying that I have not included a dsn in pdo because I am not sure how to add one. And I am guessing that pdo needs to use a dsn in this case – kqlambert Aug 19 '13 at 20:44
  • `'mssql:Server=localhost;Database=MYDSN'` is the one. and you already successfully connected, mind you. – Your Common Sense Aug 19 '13 at 20:45
  • Tried switching the query and made sure all of them worked on vbscript. So I am pretty sure that it is not the query – kqlambert Aug 19 '13 at 21:01
  • You were sure of absent DSN as well – Your Common Sense Aug 19 '13 at 21:03
  • Fair enough, is their a general query you know of that I could run through this – kqlambert Aug 19 '13 at 21:05
  • I have no idea on sqlsrv. To make sure it is not a DSN but query problem you have to get rid of this useless try-catch-die. This way you will have a stack trace along with error message and will be able the see the line number that caused the error. – Your Common Sense Aug 19 '13 at 21:13
  • removed try and catch and the error happens here: $qresult->execute();, here is the error: 'SQLSTATE[HY000]: General error: 10007 Invalid object name 'agents'. [10007] (severity 5) [(null)] – kqlambert Aug 19 '13 at 21:20
  • Do not see how that could be, as my queries are working when on vbscript or in sql server – kqlambert Aug 19 '13 at 21:30
  • When you don't know what's going wrong, try to search error message text with msdn or google – Your Common Sense Aug 19 '13 at 21:31

0 Answers0