I'm trying to connect to a Teradata server using PHP, PDO and ODBC. Example code:
<?php
$username = "tuser";
$password = "tpassword";
$dbcName = "tdata";
$dsn = "odbc:Driver=Teradata;DBCName=$dbcname";
try{
$pdo = new PDO($dsn, $username, $password, [
PDO::ATTR_TIMEOUT => 20,
]);
} catch (PDOException $e) {
die($e);
}
But after executing the script, an error does appear
[WSock32 DLL] HostUnreach: Teradata server can't currently be reached
over this network.
Ping to Teradata server and telnet with Teradata server is successful. How to connect to server properly?
I'm using a Windows Server 2012 with PHP 7.
Related
I have a SQL Server running the OS Windows Server 2012 R2. It is running Microsoft SQL Server 2012.
On another computer, I am running an IIS web server, and I have installed PHP version 7.2.13, and Microsoft Driver 5.3 for PHP for SQL Server.
I am trying to connect to a database on the SQL Server via PHP:
<?php
// Now connect to an Azure SQL database by setting Authentication to ActiveDirectoryPassword
$azureServer = "vgissql";
$azureDatabase = "dbo.pctProjectTracking";
$azureUsername = "myuid";
$azurePassword = "mypassword";
$connectionInfo = "Database = $azureDatabase; Authentication = ActiveDirectoryPassword;";
try
{
$conn = new PDO( "sqlsrv:server = $azureServer ; $connectionInfo", $azureUsername, $azurePassword );
echo "Connected successfully with Authentication=ActiveDirectoryPassword.\n";
$conn = null;
}
catch( PDOException $e )
{
echo "Could not connect with Authentication=ActiveDirectoryPassword.\n";
print_r( $e->getMessage() );
echo "\n";
}
?>
If I try opening this php file in a browser, I get:
Could not connect with Authentication=ActiveDirectoryPassword. could not find driver
Any ideas on what could be wrong here?
I set up a MySQL RDS instance in AWS. After instantiating the instance I tried to connect it from my EC2's command line and it works with no issue. However, when I try connecting through my PHP code I get erros. Below is the sample code that I tried to test my connectivity.
<?php
try{
$dbh = new pdo( 'aws-test-db.hibizibi.us-west-2.rds.amazonaws.com;dbname=test',
'root',
'password',
array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
die(json_encode(array('outcome' => true)));
}
catch(PDOException $ex){
die(json_encode(array('outcome' => false, 'message' => 'Unable to connect')));
}
I get {"outcome":false,"message":"Unable to connect"}. My original code is using idiorm (which uses PDO). The portion that connects with database looks like below:
# configure ORM
ORM::configure('mysql:host='.DB_SERVER.';dbname='.DB_NAME);
ORM::configure('username', DB_SERVER_USERNAME);
ORM::configure('password', DB_SERVER_PASSWORD);
From the above I get the below error trace:
SQLSTATE[HY000] [2005] Unknown MySQL server host 'aws-test-db.hibizibi.us-west-2.rds.amazonaws.com:3306' (11)
#0 /var/www/html/main/admin/applications/vendors/idiorm/idiorm.php(255): PDO->__construct('mysql:host=aws-...', 'root', 'password', NULL)
#1 /var/www/html/main/admin/applications/vendors/idiorm/idiorm.php(237): ORM::_setup_db('default')
UPDATE
I updated my test code to try connecting using mysql extension and it worked with mysql but not PDO
<?php
$servername = "aws-test-db.hizibizi.us-west-2.rds.amazonaws.com";
$username = "root";
$password = "password";
$dbname='test';
try{
$dbh = new pdo(
'mysql:'.$servername.';dbname='.$dbname.';',
$username,
$password,
array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)
);
die(json_encode(array('outcome' => true)));
} catch(PDOException $ex) {
echo json_encode(array('outcome' => false, 'message' => $ex->getMessage()))."\n";
}
// Create connection
$conn = mysqli_connect($servername, $username, $password);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully\n";
Now, I am getting {"outcome":false,"message":"SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket '\/var\/lib\/mysql\/mysql.sock' (2)"} while trying t connect through PDO. My attempt through mysql is successful. Seems like the problem is specifically with PDO. Is there anything I can check ?
This is a very old question, but I had the exact same issue and wanted to document it here for anyone who finds this later.
The Problem
You can connect to your database (Amazon RDS) manually from the command line.
You can connect to your database via mysqli in PHP.
You can not connect to your database via PDO in PHP.
The Solution
For me, after trying almost everything, I randomly decided to try and create a new database user. This worked and I was now able to connect via PDO.
This prompted me to investigate the issue a little further and I was able to narrow the issue down to a backslash \ character in my MySQL password.
There seems to be some kind of conflict between ENV Vars (with \), PHP and PDO.
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.
I'm trying connect to Oracle.
This is my code:
$dsn="oci:dbname=//oracleserver:1521/xe;charset=AL32UTF8";
$user="portal2";
$password="portal2";
try{
$arrConnectOptions = array(PDO::ATTR_PERSISTENT => true);
$arrConnectOptions += array(PDO::ATTR_CASE => PDO::CASE_UPPER);
$arrConnectOptions += array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION);
$arrConnectOptions += array(PDO::ATTR_ORACLE_NULLS => PDO::NULL_EMPTY_STRING);
$dbh = new PDO($dsn, $user, $password, $arrConnectOptions);
$sql = "select count(*) from tbl_message";
foreach ($dbh->query($sql) as $row) {
var_dump($row);
}
unset($dbh);
echo "OK";
}catch (PDOException $e){
print("Error:".$e->getMessage());
echo "NG";
header("HTTP/1.1 500 NG");
die();
}catch (Exception $ex){
print("Error: ".$ex->getMessage());
}
$dbh = null;
When I execute on Linux command line: php check.php, It's ok
But when I try to open on web browser. I got error message
"Unable to load the webpage because the server sent no data.
Error code: ERR_EMPTY_RESPONSE"
P/S: Server is running. (Centos 7)
Please help me
Do you have dispaly_errors set to 1 and error_reporting set to E_ALL? My best guess is that since it is common for PHP CLI and the web server to use different PHP.ini files, you have the OCI driver enabled in the CLI ini and not in the web server ini. If you web server ini isn't set to display the errors you could get the error above.
I am working on a website that runs by MYSQL and Linux Php 5.3 - and i need to work with this as well as a remote MSSQL database.
I read that PDO this is the way to connect to MSSQL.
It though seems there are both a PDO and a more familiar mssql_connect solution.
I have little to no experience with either PDO or mssql_connect.
On the PHP documentation i find:
Mssql_connect - The familiar expression:
<?php
// Create a link to MSSQL
$link = mssql_connect('KALLESPC\SQLEXPRESS', 'sa', 'phpfi');
// Select the database 'php'
mssql_select_db('php', $link);
?>
PDO - Which i haven't tried before - which needs a driver !(?) :
<?php
/* Connect to an ODBC database using driver invocation */
$dsn = 'mysql:dbname=testdb;host=127.0.0.1';
$user = 'dbuser';
$password = 'dbpass';
try {
$dbh = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
?>
So what to choose and why ?
Although I have not tried it yet. So I can't say for sure if it works or not.
PHP Manual says use pdo::dblib http://php.net/manual/en/ref.pdo-dblib.php
Microsoft does have it's own set of drivers but you have to be on a windows machine to use them. http://www.microsoft.com/en-us/download/details.aspx?id=20098
MSSQL connection with PDO:
$db_handle = new PDO("sqlsrv:server=$server; Database=$database", $user, $pass);
MySQL connection with PDO:
$db_handle = new PDO("mysql:host=$server;dbname=$database", $user, $pass);
I don't see what your confusion is?