PHP7 Not connecting to Microsoft SQL Server 2012 - php

I have been searching for an answer for last 3 days before posting.
I have installed SQL Server 2012 and, Apache24, PHP7.0
The Apache started fine, PHP7 Started fine
I can browse localhost/info.php and it shows perfect
Installed ODBC
Installed the SQL extensions drivers
Configured PHP.ini and apache conf file
SQL services started through SQL configuration manager
also allowed in the network configuration the TCP\IP and named pipes
I can't login to the SQL server using Microsoft SQL server manager
So all seems to be fine but every time I try to do a simple PHP code to connect my PHP to the SQL server I get the below error
Warning: mysqli_connect(): (HY000/2002): No connection could be made
because the target machine actively refused it. in
C:\Apache24\htdocs\index.php on line 13 Could not connect
Have disabled the firewall also and even added a rule with few ports
I am running on windows 10 Enterprise
Also when I change the server name to WALEED\SQLEXPRESS I get the below
Warning: mysqli_connect(): php_network_getaddresses: getaddrinfo
failed: No such host is known. in C:\Apache24\htdocs\index.php on line
13
Tried to search in the SQL server log but couldn't find any errors
All extensions for my PHP are working fine on info.php
Index file :
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$dbhost = 'localhost';
$dbuser = 'admin';
$dbpass = 'admin2017';
$db = 'sunto';
$conn = mysqli_connect($dbhost,$dbuser,$dbpass,$db);
if(! $conn){
die('Could not connect') ;
}
echo 'Successfully Connected';
$sql = 'Connected Successfully';
$retvalue = mysqli_query($sql);
if(! $retvalue){
die('Cannot connect to SQL: ');
}
echo 'DataBase test_db13 has successfully created';
mysqli_close($conn);
?>

In the comments you said:
what is strange when i Try to connect with sqlsrv_connect(
"localhost", $connectionInfo ); it is fine connected but when i try to
connect using mysqli. it shows error
There's nothing strange about that. The clue's right there in the name of the method you're using:
mysqli_connect()
That code library is for connecting to MySQL databases only.
You need to use the sqlsrv_ library instead if you want to connect to SQL Server. They're completely different database platforms, with completely different PHP libraries to use for working with them. As you saw yourself, it works when you use sqlsrv_connect.
I'm not really sure why you then tried to use mysqli after that, or why you expected that to be successful.

Related

How can I get PHP to connect locally to MySQL?

I'm trying to test a PHP website locally on an Ubuntu 16.04 machine, but I'm having trouble connecting to MySQL database using mysqli_connect().
I have Apache 2.4 running with mod_php7.0.
I installed the Ubuntu package php7.0-mysql.
Accessing a php.info() file at a localhost address in the same directory as the website indicates that my php.ini file is /etc/php/7.0/apache2/php.ini.
In that file, I removed the comment from the line ;extension=php_mysqli.dll.
The file I'm trying to access contains the line
$con=mysqli_connect("localhost","db_user","db_password","db_name");
When I try to access the site, I get the error
Warning: mysqli_connect(): php_network_getaddresses: getaddrinfo failed: Name or service not known"
with reference to the line above.
I have MySQL running locally with the given user, password, and database defined. I've tried using 127.0.0.1 instead of localhost, but the error is the same.
The error appears to be that PHP can't make a local connection to MySQL. How can I figure out what's wrong?
Edit To give more context, I'm trying to create a localhost version of a website that works on its production/dev servers. As far as I can tell, I've recreated all relevant aspects of the server setup, but obviously I've missed something.
The biggest difference between the local setup and production/dev version is that I have PHP7 locally but PHP5.5.9 on production/dev. My best guess at this point is that this difference is what's responsible for the localhost failure, but I don't know enough about PHP to narrow down what it might be.
Use the following to connect to MySQL database on localhost.
<?php
$servername = "localhost";
$username = "root";
$password = "mysql-password";
$dbname = "database-name";
$conn = mysqli_connect($servername, $username, $password,$dbname);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
?>

Remote MySQL in Disk station connection refused

I am trying to log-in remotely to MySQL server which is running on my Disk station, over the network. I confirm that MySQL is running on the disk station, I have php running on the disk station and I can check via remote connection to disk station phpMyAdmin and MySQL queries are answered.
On my laptop, I have created the following PHP script and when I try to run this script on my laptop, I get the following error message
<?php // login.php
$hn = 'bio12' ; //disk station name
$db = 'mysql' ; //database I want to connect to
$un = 'mysqluser' ; // user name
$pwd = 'abctest123' ; // password for the user
$conn = mysql_connect($hn, $un, $pwd);
if(! $conn){
die('Could not connect') ;
}
echo 'Successfully Connected';
?>
Error message: Warning: mysql_connect(): No connection could be made because the target machine actively refused it. in C:\xampp\htdocs\login.php on line 15
Could not connect
I have tried using the IP address instead of 'bio12' but without success.
Interesting enough, when I use the same code and try to connect to local mysql (I am running XAMP on my laptop) and using the 'localhost' instead of 'bio12' it works just fine.
What am I doing wrong?
You need to allow remote connections. Find line in your mysql config (my.cnf file)
bind-address = 127.0.0.1
and comment it with "#" at the beginning, please also see this thread: How to allow remote connection to mysql

MySQL connect failed. Can't connect to MySQL server on 'http' (4)

I try to connect my android application using JSON Parser to the web hosting. But whenever I try to connect (even just open using url in the browser) I will get the error message.
<?php
$dbHost = 'http://sql4.000webhost.com/localhost';
$dbUser = 'a6410240_cbetTD';
$dbPass = 'xxxxxx';
$dbName = 'a6410240_cbetTD';
$conn = mysql_connect ($dbHost, $dbUser, $dbPass) or die ('MySQL connect failed. ' . mysql_error());
mysql_select_db($dbName,$conn);
?>
This is my database.php file. The full error message is
Warning: mysql_connect() [function.mysql-connect]: Can't connect to MySQL server on 'http' (4) in /home/a6410240/public_html/database.php on line 8.
I have tried change the $conn but still it didn't worked for me.
Thanks
If your database and application is on same server then use "locahost" in $dbhost.
And if your database and application is on different servers then you need to use IP address or hostname in $dbhost and the database user should be added on database server provided with required privileges.
The problem you are having was already mentioned in one of the comments, this one to be precise.
For your solution to work, all you need to do is omit the part http:// at the beginning and probably /localhost at the end.
The host is only the domain you are referring to. In this case sql4.000webhost.com. With /localhost you tried to already connect to a database, although your configured database is supposed to be a6410240_cbetTD.
MySQL use TCP port 3306 by default
($dbport) and hostname or IP address ($dbhost). For LAMP (Linux-Apache-MySQL-php) you can find a lot of tutorials.
Usually MySQL server listens internal port (which can't be reached via Internet) for security purposes.
If you familiar with docker, you can simply download examples of LAMP solutions from hub.docker.com.

Connecting MySQL database to website with PHP

As the title indicates I'm trying to link my MySQL database to my website using php.
I'm using GoDaddy hosting for the MySQL database and I got this code directly from them and it's still not working for some reason, giving the error below.
Here's my code :
<?php
$hostname = "trdlibrary.db.6253425.hostedresource.com";
$username = "*******";
$password = "*******";
$dbname = "trdlibrary";
$connect = mysqli_connect($hostname, $username, $password, $dbname, 3306) OR DIE ("Unable to connect to database! Please try again later or contact an administrator for help.");
?>
Here's the error :
Warning: mysqli_connect(): (HY000/2002): A connection attempt failed
because the connected party did not properly respond after a period of
time, or established connection failed because connected host has
failed to respond. in C:\xampp\htdocs\searchengine\dbconnect.php on
line 6 Unable to connect to database! Please try again later or
contact an administrator for help.
Can anyone see the issue?.. or is this something I need to contact GoDaddy's support line about.
-- EDIT --
I forgot to mention that I am running the web server off my local machine using XAMPP (so I can run the PHP scripts) but the MySQL server is off-site with GoDaddy, so 'localhost' won't work currently.
-- EDIT #2 --
So I learned that sadly, remote access of MySQL databases to GoDaddy requires a more expensive and 'higher tier' hosting package from them. So I just answered my own question, thanks to those who replied.
You have to setup remote access to databases on Godaddy. You can refer to this page to see how to enable it.

Trouble connecting to SQL Server with PHP

I'm trying to connect to my local SQL Server 2008 R2 (have also tried it with 2005, same result exactly) with PHP. I'm using PHP 5.1 which still supports php_mssql and mssql_connect().
for some reason PHP just won't find my server, I can connect via ODBC flawlessly an that's fine, but I would like to connect to SQL Server directly.
I have connected PHP to SQL Server a million times on different servers, this one seems to be the only one giving me issue.
This is my little test code to try and get the connection working.
//define connection garbage
$db['hostname'] = "USER90C6\SQLEXPRESS";
$db['username'] = "user";
$db['password'] = "password";
$db['database'] = "kal_auth";
//connection string
$conn = mssql_connect($db['hostname'], $db['username'], $db['password']);
//does it work? :o
if($conn)
{
echo "works";
}
else
{
echo "fails";
}
The error this code produces:
Warning: mssql_connect() [function.mssql-connect]: Unable to connect to server: USER90C6\SQLEXPRESS in C:\xampp\htdocs\test.php on line 9
fails
Is there something seriously wrong with my setup? or am I missing something.
Did you enable TCP/IP on the server's configuration tool?
Is the firewall blocking any ports you might be using?
Are your MDAC (microsoft data access components) updated?
turn on mssql.secure_connection in php.ini
I think you don't miss anything.. Your connection string seems to be right (you receive a "Unable to connect" error..).
In my opinion, you problem can be a version incompatibility or a user privileges mistake. First of all: look at DLL driver you are using in PHP and check it's compatibility with you MSSQL version.
Maybe can be a good idea a fresh PHP install, with the latest stable release, if it is possible. Give a look at: http://www.php.net/manual/en/mssql.requirements.php
Good luck.

Categories