简体   繁体   English

具有MSSQL数据库的LAMP Server中具有SLIM PHP的API

[英]API with SLIM PHP in LAMP Server with MSSQL database

I have Lamp server in my Ubuntu. 我的Ubuntu中有Lamp服务器。 I worked on Api's using slim framework and with mysql database and its working fine. 我使用瘦框架和mysql数据库研究Api的工作原理。 My problem is I cant connect my api to MSSQL. 我的问题是我无法将api连接到MSSQL。 I already install freetds in ubuntu I also include this in freetds.conf 我已经在ubuntu中安装了freetds我也在freetds.conf中包含了它

 [myserver]
    host = myhost
    port = myport

here's my connection: 这是我的联系:

function getConnection() {
   $dbhost="myserver.database.windows.net";
   $dbuser="user";
   $dbpass="mypass";
   $dbname="myDB";
   $dbh = new PDO("dblib:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);  
   $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
   return $dbh;
}

here's my api in slim: 这是我苗条的api:

$app = new Slim\App();

$app->get('/clients', 'getClients'); 

$app->run();

function getClients() {
$sql = "select * FROM mytable";

try {
    $db = getConnection();
    $stmt = $db->query($sql);  
    $clients = $stmt->fetchAll(PDO::FETCH_OBJ);
    $db = null;
    echo '{"client": ' . json_encode($clients) . '}';
} catch(PDOException $e) {
    echo '{"error":{"text":'. $e->getMessage() .'}}'; 
}
}

I think you are missing something.... 我认为您缺少某些东西。

I think you need to let PDO know what port 我认为您需要让PDO知道哪个端口

http://php.net/manual/en/ref.pdo-dblib.php http://php.net/manual/zh/ref.pdo-dblib.php

$hostname = "myhost";
$port = 10060;
$dbname = "tempdb";
$username = "dbuser";
$pw = "password";
$dbh = new PDO ("dblib:host=$hostname:$port;dbname=$dbname","$username","$pw");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM