Php connected with SQL Server

Asked

Viewed 1,804 times

0

How can I connect php to sqlserver (Pdo)? It seems simple, but I’m not getting... I’ve enabled the driver all ok Attached is the name of the server, I will run in place ...enter image description here

  • What code are you using for connection?

  • I tried several ja... but without success... the class q I always use in mysql/postgtee is this: http://answall.com/q/182026/32918

2 answers

4

You have two options to try:

$pdo = new PDO ("mssql:host=$hostname;dbname=$dbname","$username","$pw");

or

$pdo = new PDO( "sqlsrv:server=$serverName ; Database=$dbname", "$username", "$pw");  

It depends on the driver you installed in PHP.

[Edited] An example of select with PDO

//retornar o resultset
$sql = 'Select * from products';
$result = $pdo->query($sql);

// imprimir resultados um a um 
while ($campos = $result->fetch()){
    echo $campos['ProductID']. ' - '.$campos['ProductName'].'<br />';
}
  • and how would a select Pfor example ? The server name is the server name ? the one I posted in the photo selected in blue ?

  • Edited response with example.

  • unfortunately without success... The page keeps loading until timeout, has how to help me ?

  • No error? Already took a look at the network?

  • With the second attempt gave the error : Fatal error: Uncaught Exception 'Pdoexception' with message 'could not find driver' in C: xampp htdocs test 1.php:2 Stack trace: #0 C: xampp htdocs test 1.php(2): PDO->_Construct('sqlsrv:server=D...', 'sa', 'Gugofu3107') #1 {main} thrown in C: xampp htdocs test 1.php on line 2. Already the first is : Fatal error: Maximum Execution time of 60 Seconds exceeded in C: xampp htdocs test 1.php on line 0

  • phpinfo in PDO area displays : PDO drivers mssql, mysql, odbc, sqlite, sqlite2

  • The first message is clear, you do not have this driver installed. In the second one is timeout, it may have many reasons, the OS in English has an answer regarding http://stackoverflow.com/questions/23682748/pdo-connection-to-sql-server-times-out

  • You have mssql and not sqlsrv

  • but how would the connection query ? the first ?

  • The ->. fetch command is not recognized... Fatal error: Call to a Member Function fetch() on a non-object in C: xampp htdocs test 1.php on line 7

  • The fetch command exists, the error is saying that you are calling, the fetch method ("the Member") of something that is not object("non-object"), that is, your query must have failed... alias, checks if you have connected, is using the names correctly

Show 6 more comments

1

Good night. If you don’t have the sqlsrv and pdo_sqlsrv extensions, try odbc. See how it looks:

$pdo = new \PDO ("odbc:Driver={SQL Server Native Client 11.0};Server=localhost;Database=catalogName;app=appName;WISD={$_SERVER['REMOTE_HOST']}",$sqlUser,$sqlPass);

Which version of PHP are you using? Can you send a print of phpinfo in the PDO area similar to the image below? See if in the PHP configuration is enabled pdo_sqlsrv, because it seems to me that this driver you already have.

PRINT PHPINFO PDO

  • this server Native client is this or I change it to some statement ?

  • That’s right!!! If you don’t have the odbc driver it’s easy to find for download.

  • see what showed the Pdo of my phpinfo in the first reply comment

  • Did an error appear using odbc? See if the 'SQL Server Native Client 11.0' driver in windows administrative tools. Anything, just test this with SQL Server. If it works, try to install the other drivers, it’s easier to work. $Pdo = new PDO ("odbc:Driver={SQL Server};Server=localhost;Database=catalogName;app=appname;WISD={$_SERVER['REMOTE_HOST']}",$sqlUser,$sqlPass);

  • 1

    @Maurivan, is an alternative, but is one more layer to connect, usually does not get slower? And ODBC only windows, right?

  • It n displays connection error, but n was able to enter data ...

  • 1

    @Rodrigosartorijarouche, yes, gets slower. So the ideal is to install and enable pdo_sqlsrv in php extensions. The odbc driver exists for other Oses as well.

  • 1

    @Maurivan extra information are always useful! Thank you!

  • MY CODE IS THIS http://pastebin.com/FttEiEZb however qnd rodo it n displays error, but it does not insert, because I select* in sql studio Sabis the error ?

  • Cod_admregional is string? If integer you have to remove the quotes...

  • It’s char, but I entered it by the manager and it worked ...

Show 6 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.