[英]php mssql linux
I have a php file with a mssql connection, which works on windows.我有一个带有 mssql 连接的 php 文件,它适用于 Windows。
Now I would like to run my php on linux.现在我想在 linux 上运行我的 php。
I installed unixodbc and msodbcsql.我安装了 unixodbc 和 msodbcsql。
The server is running RedhatEnterprise 6 and php 5.4服务器正在运行 RedhatEnterprise 6 和 php 5.4
I cant find any description how to connect to a mssql database with linux.我找不到任何关于如何使用 linux 连接到 mssql 数据库的描述。
What else do I have to install and configure?我还需要安装和配置什么?
You can certainly connect directly to Microsoft SQL Server database from a Linux server, using PHP's PDO system and the dblib driver.您当然可以使用 PHP 的 PDO 系统和 dblib 驱动程序从 Linux 服务器直接连接到 Microsoft SQL Server 数据库。
First, install the EPEL repository and then install the php-mssql package.首先,安装 EPEL 存储库,然后安装 php-mssql 包。 Then use the following code to set up your connection:然后使用以下代码设置连接:
$dbhost = "your.db.server";
$dbname = "yourdbname";
$dbuser = "yourdbuser";
$dbpass = "yourdbpassword";
$db = new PDO("dblib:$dbhost;dbname=$dbname", $dbuser, $dbpass);
foreach ($db->query("select * from mytable") as $row) {
echo "{$row['field1]'}<br/>";
}
Additional StackOverflow resources on this topic include:关于此主题的其他 StackOverflow 资源包括:
Connecting to mssql using pdo through php and linux 通过 php 和 linux 使用 pdo 连接到 mssql
I faced the same challenge very recently, and after a lot of head scratching ultimately, I decided to go about things a little differently, as follows.我最近面临着同样的挑战,经过多次摸索,最终我决定以不同的方式处理事情,如下所示。
I set up a Windows machine (it could be a virtual machine) running PHP and Apache to handle the MSSQL requests from the Linux box.我设置了一台运行 PHP 和 Apache 的 Windows 机器(它可能是一个虚拟机)来处理来自 Linux 机器的 MSSQL 请求。
The PHP on Linux uses a CURL command to send a request to Windows, and the response contains the data (compressed). Linux 上的 PHP 使用 CURL 命令向 Windows 发送请求,响应包含数据(已压缩)。
A little off-beat perhaps, but working well.也许有点不合时宜,但运作良好。
You need to have the modules sqlsrv and pdo_sqlsrv active.您需要激活模块 sqlsrv 和 pdo_sqlsrv。
sudo apt-get install php-fpm pcp-cli php-dev php-pear
sudo pecl install sqlsrv pdo_sqlsrv
The correct location (on Linux) to put these two lines is in /etc/php/<version>/mods-available/pdo.ini
there, add放置这两行的正确位置(在 Linux 上)在/etc/php/<version>/mods-available/pdo.ini
那里,添加
extension=sqlsrv.so
extension=pdo_sqlsrv.so
Then restart/reload php-fpm or the computer.然后重新启动/重新加载 php-fpm 或计算机。
Then run ./test.php (where port 2017 is the SQL-server-port)然后运行 ./test.php (其中端口 2017 是 SQL-server-port)
#!/usr/bin/php
<?php
$serverName = "localhost,2017";
$connectionOptions = array(
"database" => "MY_DB_NAME",
"uid" => "sa",
"pwd" => "TOP_SECRET"
);
// Establishes the connection
$conn = sqlsrv_connect($serverName, $connectionOptions);
if ($conn === false) {
die(formatErrors(sqlsrv_errors()));
}
// Select Query
$tsql = "SELECT @@Version AS SQL_VERSION";
// Executes the query
$stmt = sqlsrv_query($conn, $tsql);
// Error handling
if ($stmt === false) {
die(formatErrors(sqlsrv_errors()));
}
?>
<h1> Results : </h1>
<?php
while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
echo $row['SQL_VERSION'] . PHP_EOL;
}
sqlsrv_free_stmt($stmt);
sqlsrv_close($conn);
function formatErrors($errors)
{
// Display errors
echo "Error information: <br/>";
foreach ($errors as $error) {
echo "SQLSTATE: ". $error['SQLSTATE'] . "<br/>";
echo "Code: ". $error['code'] . "<br/>";
echo "Message: ". $error['message'] . "<br/>";
}
}
?>
And to query the db with SQL-server pdo :并使用 SQL-server pdo查询数据库:
$servername = "";
$username = "";
$password = "";
$database = "";
$port = "1433";
try
{
$dbh = new PDO("sqlsrv:server=$servername,$port;Database=$database;ConnectionPooling=0", $username, $password,
array(
PDO::ATTR_PERSISTENT => true,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
)
);
}
catch (PDOException $e)
{
echo ("Error connecting to SQL Server: " . $e->getMessage());
}
$stmt = $dbh->prepare("SELECT name FROM master.sys.databases WHERE name = db_name()");
$stmt->execute();
while ($row = $stmt->fetch()) {
print_r($row);
}
unset($dbh); unset($stmt);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.