简体   繁体   中英

PHP with MSSQL not installing in centos 7

I want to connect MSSQl server through PHP in CentOS Linux system. But getting below error,

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

For that, i refered some site and found solutions. But that also not working. Here i am trying to installing php5-sybase, but getting some error,

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

How to fix this issue.

To install the PHP Driver for SQL Server on CentOS, here is what I would recommend:

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*||"`

Now you should be able to connect to SQL Server using a simple PHP script like this one:

<?php
$serverName = "localhost";
$connectionOptions = array(
    "Database" => "SampleDB",
    "Uid" => "sa",
    "PWD" => "your_password"
);
//Establishes the connection
$conn = sqlsrv_connect($serverName, $connectionOptions);
if($conn)
    echo "Connected!"
?>

php-mssql which provides mssql and pdo_dblib extensions is available in EPEL repository.

And as explained in yum error output, you should disable rpmforge (and probably apt.sw.be_redhat_el2.1_en_mirrors-rpmforge)

Notice: this extension is deprecated and will be removed in PHP 7. So, for better maintainability, I recommend to use the PDO driver.

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

I got the repository URL from EPEL and installed. but i got

[root@localhost tmp]# yum install php5-sybase
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
No package php5-sybase available.
Error: Nothing to do
[root@localhost tmp]#

what is the issue?

I don't know if anyone will come here, but the issue with me was that the optional rpms were not enabled. I spent all day on this uninstalling and reinstalling. But then I found this footnote somewhere

NOTE for RHN users You need to also enable the 'optional' repository to use EPEL packages as they depend on packages in that repository. This can be done by enabling the RHEL optional subchannel for RHN-Classic. For certificate-based subscriptions see Red Hat Subscription Management Guide. For EPEL 7, in addition to the 'optional' repository (rhel-7-server-optional-rpms), you also need to enable the 'extras' repository (rhel-7-server-extras-rpms).

if you are connecting to an external sql server with your instance, you just need to install php-sql driver in your box.

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

if you face segmentation fault error or 502 gateway error on browser

you can do this steps to remove this error:

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

in my case my php version is 7.3 and this steps will resolve my issue. if this steps not help, you can check and upgrade your php version.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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