Error. Cannot connect to database: SQLSTATE[HYT00] - php

I am getting the following error when trying to connect to my Microsoft SQL Server database using the default port number:
Error. Cannot connect to database: SQLSTATE[HYT00]: [unixODBC][Microsoft][ODBC Driver 17 for SQL Server]Login timeout expired
This is the PHP code I am using to connect to the database:
<?php
class DB_Connect {
private $db;
function construct() {
}
public function connect() {
require_once 'String.php';
try {
$this->db = new PDO('sqlsrv:Server=$server,1433; Database=$db', $user, $pass);
} catch (PDOException $e) {
return "Error. Cannot connect to database: " . $e->getMessage();
}
}
}
?>
I am 100% sure that the credentials are correct since they are workign when I run the script on localhost using xampp.
What I have done till now:
Installed the pdo drivers on the linux machine
Installed the obdc drivers on the linux machine
Installed the sqlsrv drivers on the linux machine
This is the configuration I am using on the server:
PHP Version 7.0.30 connecting to SQL Server 2017 hosted on Gearhost.
Can anyone please shed a light on what might be wrong?

Basically the issues were 2:
First of all, the drivers were not installed correctly. I needed to install the drivers through PECL. This is how I installed them on Redhat Cent OS 7:
Install PHP:
sudo su
wget https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
wget http://rpms.remirepo.net/enterprise/remi-release-7.rpm
rpm -Uvh remi-release-7.rpm epel-release-latest-7.noarch.rpm
subscription-manager repos --enable=rhel-7-server-optional-rpms
yum-config-manager --enable remi-php72
yum update
yum install php php-pdo php-xml php-pear php-devel re2c gcc-c++ gcc
Install Prerequisites
sudo yum-config-manager --enable rhel-server-rhscl-7-rpms
sudo yum install devtoolset-7
scl enable devtoolset-7 bash
Install PHP Drivers for SQL Server
pecl download sqlsrv
tar xvzf sqlsrv-5.2.0.tgz
cd sqlsrv-5.2.0/
phpize
./configure --with-php-config=/usr/bin/php-config
make
sudo make install
Install Apache and restart the service
sudo yum install httpd
sudo apachectl restart
--EDIT--
You can alternatively download the prebuilt binaries from the Github project page, or install from the Remi repo:
sudo yum install php-sqlsrv php-pdo_sqlsrv
In my case I also had to open the firewall connection from the server the database. You check if this is necessary, run sqlcmd from Terminal to connect to the database. If you get a network error with the correct credentials, then you know you've got a firewall problem.
Hope this helps anyone who is struggling connecting to a Linux server to SQL Server.

Can you use this here?
try{
$db = new PDO("sqlsrv:Server,1433=$server;dbname=$db;charset=utf8","$user","$pass");
$query=$db->prepare("Some SQL String Here");
$query->execute();
}catch(PDOException $e ){
echo "Error: ".$e;
}

Related

How to setup Oracle Instant Client with XAMPP in macOS Mojave?

I am using macOS and have successfully installed and set up XAMPP on my system. But the problem is that I want to connect to an oracle database through PHP Easy Syntax like below:
$conn = oci_connect('xx', 'xx', '123.123.xxx.x/xxxx');
if (!$conn) {
$e = oci_error();
trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
}
But i get this error on running the page
Call to undefined function oci_connect()
After reading some article i found that i have to install oracle instant client but i really don't know the steps to download which version and then where to unzip the files in xampp folder. I am really a novice in this area.
This is what I did on macOS Mojave. I used the native installer XAMPP 7.4.9 from
xampp-osx-7.4.9-0-installer.dmg. I did a basic install with the Developer files
option selected - this was the default.
It seems like XAMPP for macOS doesn't have the OCI8 extension built. (I noticed
the XAMPP VM install from xampp-osx-7.4.9-0-vm.dmg also didn't have the OCI8
extension, but I haven't played with it yet.)
Sadly the 'obvious' method of installing OCI8 with the pecl command fails to
correctly find the right PHP version. E.g. running:
sudo /Applications/XAMPP/xamppfiles/bin/pecl install oci8
and answering:
instantclient,/Applications/XAMPP/xamppfiles/lib/instantclient-11.2.0.3.0/
fails with checking PHP version... Unknown option: n. The PECL config looked OK, but there must have been some conflict with the native PHP version.
Anyway, the slightly longer manual install of OCI8 works. The instructions are below.
I prefer to use a more recent Instant Client, so download the latest Instant
Client Basic and SDK packages from
here
and unzip them:
cd $HOME/Downloads
curl -O https://download.oracle.com/otn_software/mac/instantclient/instantclient-basic-macos.zip
curl -O https://download.oracle.com/otn_software/mac/instantclient/instantclient-sdk-macos.zip
Extract the Instant Client (substitute your download directory path):
cd /Applications/XAMPP/xamppfiles/lib
sudo unzip /Users/cjones/Downloads/instantclient-basic-macos.zip
sudo unzip /Users/cjones/Downloads/instantclient-sdk-macos.zip
You can optionally do some cleanup. Get rid of the old Instant Client:
sudo rm -rf /Applications/XAMPP/xamppfiles/lib/instantclient-11.2.0.3.0
and remove new libraries not needed by OCI8:
sudo rm -f /Applications/XAMPP/xamppfiles/lib/instantclient_19_3/{*jdbc*,*occi*,*mysql*,*jar,uidrvci,genezi,adrci}
Download the OCI8 extension from PECL and
extract it:
cd $HOME/Downloads
curl -O https://pecl.php.net/get/oci8-2.2.0.tgz
tar -xzf oci8-2.2.0.tgz
Build and install OCI8. You'll need some kind of compiler available; I have XCode installed:
cd oci8-2.2.0
/Applications/XAMPP/xamppfiles/bin/phpize
./configure --with-php-config=/Applications/XAMPP/xamppfiles/bin/php-config --with-oci8=shared,instantclient,/Applications/XAMPP/xamppfiles/lib/instantclient_19_3
make
sudo make install
Then, tell XAMPP to enable the extension:
sudo /Applications/XAMPP/xamppfiles/xampp oci8
At the prompt, enter the path to the Instant Client directory /Applications/XAMPP/xamppfiles/lib/instantclient_19_3 like:
Please enter the path to your Oracle or Instant Client installation:
[/Applications/XAMPP/xamppfiles/lib/instantclient-11.2.0.3.0] /Applications/XAMPP/xamppfiles/lib/instantclient_19_3
installing symlink...
patching php.ini...
OCI8 add-on activation likely successful.
XAMPP: Stopping Apache...ok.
XAMPP: Starting Apache...ok.
Now when you check http://localhost/dashboard/phpinfo.php you should see the OCI8 section.
You may be interested in how to 'run' an Oracle DB on macOS, which can be done in a VirtualBox VM like: https://blogs.oracle.com/opal/the-easiest-way-to-install-oracle-database-on-apple-mac-os-x. Other people use a Docker container for the same thing.

MongoDB Exception: Server reports wire version 0, but version of libmongoc requires at least 3

Fatal error: Uncaught MongoDB\Driver\Exception\ConnectionException: Server at localhost:27017 reports wire version 0, but this version of libmongoc requires at least 3 (MongoDB 3.0)
I have PHP 7.0.13, MAMP and MongoDB. The MongoDB extension for PHP has been installed.
I have the following:
<?php
ini_set('display_errors', 'On');
require 'vendor/autoload.php';
var_dump(extension_loaded('mongodb'));
echo phpversion('mongodb')."\n";
$m = new MongoDB\Driver\Manager("mongodb://localhost:27017");
// Query Class
$query = new MongoDB\Driver\Query(array('age' => 30));
// Output of the executeQuery will be object of MongoDB\Driver\Cursor class
$cursor = $m->executeQuery('testDb.testColl', $query);
// Convert cursor to Array and print result
print_r($cursor->toArray());
?>
What does 'wire' refer to in this context, and does anyone have a solution for this problem?
I have got the problem on Linux Mint 19 (think that Ubuntu 18+ can have the same problem):
Server at IP:27017 reports wire version 2, but this version of libmongoc requires at least 3 (MongoDB 3.0)
As the message says - server driver version and mine one are different.
This happened because I installed php mongo driver with the command:
sudo apt-get install php7.2-mongodb
The SOLUTION was to completely uninstall php mongo driver:
sudo apt-get remove --auto-remove php-mongodb
and then install php-mongodb from Pecl mongodb php extension:
sudo pecl install mongodb-1.4.4
(If you bump into error pecl: command not found, just install PEAR package in order to use pecl installer. sudo apt-get update && sudo apt-get install php-pear)
After that add the next line to your php.ini file:
extension=mongodb.so
Don't forget to reload the web server:
sudo systemctl reload apache2
That's it. Everything should work!
If anyone searching for solution for Centos 7, here's what you need to do (based on Evgeny Melnikov answer):
yum erase php-pecl-mongodb && pecl uninstall mongodb
pecl install mongodb-1.9.1
Then add extension=mongodb.so to your php.ini file and restart php-fpm.
I have a Raspberry Pi 3B with mongo version. It runs an older version of MongoDB, so I found an equally old version of pymongo and it worked.
mongo --version
MongoDB shell version: 2.4.14
min_wire_version was added sometime after the release of Mongo 2.4.14, so I just installed pymongo drivers that were equally as old.
pip install pymongo==2.4.2 worked for me.
https://pecl.php.net/package-info.php?package=mongodb&version=1.13.0
pecl uninstall mongodb
pecl install mongodb-1.13.0

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 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.

MongoClient Not found connection php mongolab

I'm trying to connect to Mongolab db server, but I get this error:
Fatal error: Class 'MongoClient' not found in /home/viviane/public_html/sk/zizi/head.php on line 33
I suspect it is related to the PHP Mongo Driver, but I don't know how. If I try to connect to a remote mongodb server
$uri = "mongodb://dzyasser:dzyasser#ds049624.mongolab.com:49624/serverdb";
$options = array("connectTimeoutMS" => 30000);
$connection = new MongoClient($uri, $options );
So why am I still getting this error when I'm trying to connect to a remote Mongodb server (Mongolab)?
Its look like you don't have MongoDB PHP extension in your system.
Try to install MongoDB PHP extension in order to solve your problems with the MongoClient class. This is how I setup MongoDB PHP extension in OS X.
Get MongoDB extension from PECL
$ sudo pecl install mongodb
Make a copy of php.ini.default
$ sudo cp /etc/php.ini.default /etc/php.ini
Put this line on php.ini than save
extension=mongodb.so
Check the installation using this command
php --re mongodb
Or create simple php document that contain phpinfo() function
If you get problem with writing the extension, please read this post.
I assume, the Mongo driver is installed and loaded.
If so, since the error message says the MongoClient class cannot be found, it might be caused by a namespace used in your script. If so, try
$connection = new \MongoClient($uri, $options );
Regardless of whether you are connecting locally or remotely, it seems PHP requires the mongo DB service to be running on your server. So install it like so:
a) Install mongodb service
$ sudo apt-get install -y mongodb
b) Add the php drivers if they are missing:
$ sudo apt-get install php5-dev php5-cli php-pear -y
$ sudo pecl install mongo
c) Then open your php.ini file and add to it the extension line "extension=mongo.so":
$ sudo vi /etc/php5/apache2/php.ini
> extension=mongo.so
d) Then restart apache
$ sudo service apache2 restart
Disclaimer: This example was tried on Ubuntu 14.04 LTS with Apache web-service and PHP5 installed. If your installation is different, follow the same approach but issue appropriate commands. If you have no access to SSH or sudoer privileges, contact someone who can help you do this.

PHP PDO exception: could not find driver

Does the MySQL-server and PHP5-MySQLi version have to match in order for a connection to be possible? I'm currently receiving the error below: I am running BSD.
"Fatal error: Uncaught exception 'PDOException' with message 'could not find driver'..."
Here is the the connection info:
$info = "mysql:dbname=myDB;host=localhost";
$user = "dbUser";
$pw = "somePW";
return(new PDO($info, $user, $pw));
Here is my MySQL information:
mysql-server-5.5.24
php5-mysqli-5.4.3
I had this same issue on my CentOS install. I had tried to install imagick and hosed my install. When I removed all of my php files and reinstalled something wasn't working right.
I ran:
yum install php-pdo
yum install php-pdo_mysql
After doing those two lines I ran
service httpd restart
and everything came back up and running.
PDO uses database specific drivers to connect to database systems. It looks like you are missing the pdo_mysql driver that is required to connect to a MySQL database. There is some details on installing the driver on the pdo_mysql manual page, or there may be a BSD package that you can use (I am afraid I'm not familiar enough with BSD to offer specific advice).
Thanks to zerkms and John C for pointing me in the right direction. Below are the commands I used to install the driver:
#cd /usr/ports/databases/php5-pdo_mysql
#make install clean
#apachectl restart

Categories