I am trying to connect to a mssql server on my mac through pdo_dblib. I have the freetds.conf updated to the host I want to connect. My phpinfo tells me that I have all the driver hooked up and good to go. Below is the code that I wrote to test if I can connect to the server.
<?php
$servername = "IP";
$port = "port";
$username = "username";
$password = "password";
$myDB = "database";
try {
$conn = new PDO("dblib:dbname=$myDB;host=$servername:$port", "$username", "$password");
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}
?>
However I get an error:
Connection failed: SQLSTATE[] (null) (severity 0)
I tried using SQL Developer to connect to the mssql server and it worked. I have been trying to solve this problem for a whole day. What might be the problem? Thanks!
Remove the quotes from the variables in the connection string -
$conn = new PDO("dblib:dbname=$myDB;host=$servername:$port", $username, $password);
I found out the answer!
You have to uncomment TDS version protocol in /usr/local/Cellar/freetds/0.91/etc/freetds.conf.
Found the solution in Why won't my server connect to a remote MSSQL server using PHP mssql_connect?
but why do I have to do this...no idea...wish some one could give some explanation.
Related
I have the following code:
<?php
//Step1
$db = mysqli_connect('localhost','root','[mypassword]','users')
or die('Error connecting to MySQL server.');
?>
<html>
<head>
</head>
<body>
<h1>PHP connect to MySQL</h1>
</body>
</html>
I am just trying to connect to my MySQL database. It is administered using phpMyAdmin. I am very unfamiliar with MySQL and I have never used it before. [mypassword] is the password I use to successfully connect to mySQL from the Mac terminal. "users" is a name of a table I have created in phpMyAdmin. I am using cPanel. I keep on getting the error:
Error connecting to mySQL server.
In phpMyAdmin it says Server: localhost:3306. I have tried for a very long time to fix this problem but with no results. What am I doing wrong?
I have also tried the following:
<?php
$servername = "localhost";
$username = "root";
$password = "[myPassword]";
try {
$conn = new PDO("mysql:host=$servername;dbname=users", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}
?>
After visiting the webpage it says
Connection failed: SQLSTATE[28000] [1045] Access denied for user
'root'#'localhost' (using password: YES)
Be sure that your mySql instance is up. You can download sequelpro and use that to connect to your mySQL. If that doesn't work then its a mysql setting/config that is wrong.
You can try this code
<?php
$servername = "localhost";
$username = "username";
$password = "password";
// Create connection
$conn = mysqli_connect($servername, $username, $password);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?>
How to Connect to mysql using php
Use a try catch to handle errors on the connection proccess, but i strongly advise you to use PDO by alternative from mysqli PDO Book
Use something like this:
<?php
try
{
if ($db = mysqli_connect($hostname_db, $username_db, $password_db))
{
//do something
}
else
{
throw new Exception('Unable to connect');
}
}
catch(Exception $e)
{
echo $e->getMessage();
}
And check if the code print some connection error.
Reference: how to use throw exception in mysql database connect
I'm able to connect phymyadmin of remote pc, but when i try to connect to remote db, I'm getting connection refused error.
I have seen similar kind of question, but not yet answered also it is not active now.SQLSTATE[HY000] [2002] Connection refused with right port
<?php
$servername = "192.168.1.12";
$username = "root";
$password = "root";
try {
$conn = new \PDO("mysql:host=$servername;dbname=my_db", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
}
catch(\PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}
?>
In remote pc, bind address in mysql configuration file was set to 127.0.0.1 instead of 192.168.1.12
I am trying to connect to (remote) AWS MYSQL database using php and ssl.
I created a database user and could connect with it from php and also on console.
Then I modified the user to use SSL with "require ssl" in the database.
I can stil connect on the console by:
mysql -h host -u username -p --ssl
But in php I get an ERROR 2002.
$con=mysqli_init();
mysqli_ssl_set($con,NULL,NULL,"cacert.pem",NULL,NULL);
$link = mysqli_real_connect($con, "host", "username", "password");
As far as I know the ssl_set parameters doesn't matter with AWS databases.
How to connect to Amazon RDS via SSL?
I think it has to do with the mysqli_ssl_set method, without it a connection is made and access is denied by database and this is the correct behaviour. It might be some configuration error on client side, but I have no idea what I shoud check.
The error is fired by mysqli_real_connect, as mysqli_ssl_set always returns true, just after it tries to connect will it receive an error.
I noticed it did not work for me until I specified the port. The following worked:
$db = mysqli_init();
$db->options(MYSQLI_OPT_SSL_VERIFY_SERVER_CERT, true);
$db->ssl_set(NULL, NULL, 'rds-combined-ca-bundle.pem', NULL, NULL);
$db->connect($host, $user, $pass, $db, 3306);
$servername = "only-ssl-db.ct5b4uz1gops.eu-central-1.rds.amazonaws.com";
$username = "username";
$password = "password";
$dbname = "dbname";
$con = mysqli_init();
if (!$con){
die("mysqli_init failed");
}
mysqli_ssl_set($con,NULL,NULL,'rds-combined-ca-bundle.pem',NULL,'DHE-RSA-AES256-SHA');
if (!mysqli_real_connect($con,$servername, $username, $password, $dbname))
{
die("Connect Error: " . mysqli_connect_error());
}
// Some queries...
echo "Database connected";
printf("Client version: %d\n", mysqli_get_client_version());
mysqli_close($con);
I am much confuse about 000webhost database uploading I have set my database at their and accessing it with username and password perfectly. Now trying to connect with through PHP code but i am failed, getting 500 error code and no details of the error although I have also logged exception message in my connection code nuto it only showing "A Connection error has occurred" and nothing more:
//DB Configuration
$hostName = 'localhost';
$dbUserName = 'id143984_jw_kioskuser';
$dbPassword = '***********';
$databaseName = 'id143984_jw_kiosk';
try{
$dbHandler = new PDO('mysql:host='. $hostName . ';dbname='. $databaseName, $dbUserName, $dbPassword);
}
catch(PDOException $ex){
echo '<h1>A Connection error has occurred.</h1><pre>', $e->getMessage() ,'</pre>';
echo $e->getMessage();
}
My current DB interface at 000webhost saying me that "Use localhost as connection hostname"
while in another post of 000WEbhost saying "Never use 'localhost' as your MySQL hostname! You can find your MySQL hostname by logging on to members area, entering control panel and clicking on MySQL icon."
HOw do i make successfull connection?
Finally i managed to get the answer through mysql query thanks to Show MySQL host via SQL Command Answer at stack
I run below query and got the host name and use it in my php it worked now.
select ##hostname;
show variables where Variable_name like '%host%';
$hostName = 'mysql6.000webhosting.com';
$dbUserName = 'id143984_jw_kioskuser';
$dbPassword = '***********';
$databaseName = 'id143984_jw_kiosk';
$con = mysql_connect($hostName,$dbUserName,$dbPassword);
if (!$con)
{
die('Could not connect: ' . mysql_error());
}`enter code here`
mysql_select_db("$databaseName",$con);
this should give you the real reason and note the correct host name
I am fairly new to using PHP. I downloaded XAMPP, and installed everything. PHP 5.5.27 is the version. I ran a test php program which was jsut echo "Hello World". It worked fine. I also was able to connect to MYSQL database using PHP.
$link = mysqli_connect("localhost", "u/n", "pass", "databasename";
Problem i am having and need help is with connecting to sql server. How do i do that? I saw an example online and tried it:
$serverName = "servername";
$connectionInfo = array("Database"="name", "UID"=>"U/N", "PWD"=>"pass";>
$conn = sqlsrv_connect($serverName, $connectionInfo);
But everytime i run this it tells me:
Call to undefined function sqlsrv_connect()
Can someone help me understand what is going on?
Consider using PHP's Data Objects (PDO) to connect to SQL Server (in fact you can use it to connect to MySQL or any other database).
Using the MSSQL sqlsrv API (various dlls must be set):
<?php
$server = 'servername';
$database = 'databasename';
$username = 'username';
$password = 'pass';
try {
$conn = new PDO("sqlsrv:Server=$server;Database=$database",
$user, $password);
}
catch(PDOException $e) {
echo $e->getMessage()."\n";
exit;
}
?>
Using the ODBC Driver or DSN API (requiring MSSQL ODBC Driver installed which usually ships with database or Windows in general):
<?php
$server = 'servername';
$database = 'databasename';
$username = 'username';
$password = 'pass';
try {
$dbh = new PDO("odbc:Driver={SQL Server};Server=$server;
database=$database",$username,$password);
}
catch(PDOException $e) {
echo $e->getMessage()."\n";
exit;
}
?>