带有MSSQL的PHP未安装在centos 7中


PHP with MSSQL not installing in centos 7

我想在CentOS Linux系统中通过PHP连接MSSQl服务器。但是低于错误,

Fatal error: Call to undefined function mssql_connect() in /var/www/h.....

为此,我查阅了一些网站并找到了解决方案。但这也不起作用。在这里,我试图安装php5-sybase,但遇到了一些错误,

yum install php5-sybase
Loaded plugins: fastestmirror, langpacks
apt.sw.be_redhat_el2.1_en_mirrors-rpmforge| 1.9 kB  00:00:00
remi-safe                                 | 2.9 kB  00:00:00

 One of the configured repositories failed (Unknown),
 and yum doesn't have enough cached data to continue. At this point the only
 safe thing yum can do is fail. There are a few ways to work "fix" this:
     1. Contact the upstream for the repository and get them to fix the problem.
     2. Reconfigure the baseurl/etc. for the repository, to point to a working
        upstream. This is most often useful if you are using a newer
        distribution release than is supported by the repository (and the
        packages for the previous distribution release still work).
     3. Disable the repository, so yum won't use it by default. Yum will then
        just ignore the repository until you permanently enable it again or use
        --enablerepo for temporary usage:
            yum-config-manager --disable <repoid>
     4. Configure the failing repository to be skipped, if it is unavailable.
        Note that yum will try to contact the repo. when it runs most commands,
        so will have to try and fail each time (and thus. yum will be be much
        slower). If it is a very temporary problem though, this is often a nice
        compromise:
            yum-config-manager --save --setopt=<repoid>.skip_if_unavailable=true
Cannot find a valid baseurl for repo: rpmforge

如何解决此问题。

要在CentOS上安装适用于SQL Server的PHP驱动程序,我建议如下:

sudo su
curl https://packages.microsoft.com/config/rhel/7/prod.repo > /etc/yum.repos.d/mssql-release.repo
exit
sudo ACCEPT_EULA=Y yum install msodbcsql
sudo yum install unixODBC-devel
yum groupinstall "Development Tools"
sudo pecl install sqlsrv pdo_sqlsrv
sudo echo "extension= pdo_sqlsrv.so" >> `php --ini | grep "Loaded Configuration" | sed -e "s|.*:'s*||"`
sudo echo "extension= sqlsrv.so" >> `php --ini | grep "Loaded Configuration" | sed -e "s|.*:'s*||"`

现在,您应该能够使用像以下这样的简单PHP脚本连接到SQL Server:

<?php
$serverName = "localhost";
$connectionOptions = array(
    "Database" => "SampleDB",
    "Uid" => "sa",
    "PWD" => "your_password"
);
//Establishes the connection
$conn = sqlsrv_connect($serverName, $connectionOptions);
if($conn)
    echo "Connected!"
?>
EPEL存储库中提供了提供mssql和pdo_dblib扩展的

php mssql

正如在百胜错误输出中所解释的,您应该禁用rpmforge(可能还有apt.sw.be_redhat_el2.1-en_mirors-rpmforge)

注意:此扩展已弃用,将在PHP7中删除。因此,为了更好地维护,我建议使用PDO驱动程序。

yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
yum install php-mssql  

我不知道是否有人会来这里,但我的问题是没有启用可选的rpms。我花了一整天的时间来卸载和重新安装。但后来我在的某个地方发现了这个脚注

RHN用户注意事项您还需要启用"可选"存储库来使用EPEL包,因为它们依赖于该存储库中的包。这可以通过为RHN Classic启用RHEL可选子通道来实现。有关基于证书的订阅,请参阅《Red Hat Subscription Management Guide》。对于EPEL 7,除了"可选"存储库(rhel-7-server-options-rpms)外,还需要启用"附加"存储库。

如果您使用实例连接到外部sql服务器,则只需要在您的盒子中安装php-sql驱动程序。

sudo pecl install sqlsrv
sudo pecl install pdo_sqlsrv
sudo yum install php-sqlsrv

如果您在浏览器上遇到分段故障错误或502网关错误

您可以执行以下步骤来删除此错误:

1. make sure extension loaded are having executable permission. 
you can check this information in **/usr/lib64/php/modules**
2. add extension=/usr/lib64/php/modules in your php.ini (**/etc/php.ini**) file

在我的情况下,我的php版本是7.3,这些步骤将解决我的问题。如果这些步骤没有帮助,您可以检查并升级您的php版本。