Amazon RDS PHP MSSQL - php

I'm trying to open a connection to Amazon RDS MS-SQL from Hosting with MSSQL library with following code:
<?php
$dbhost = "xxxxxxx.rds.amazonaws.com";
$dbport = 1433;
$dbname = "xxxxx";
$dbuser = "xxxxxx";
$dbpass = "xxxxxxx";
try {
$dbh = new PDO ("dblib:host=$dbhost:$dbport;dbname=$dbname","$dbuser","$dbpass");
} catch (PDOException $e) {
echo "Failed to get DB handle: " . $e->getMessage() . "\n";
exit;
}
and I am getting this error:
Failed to get DB handle: SQLSTATE[HY000] Unable to connect: Adaptive Server is unavailable or does not exist (severity 9)

Related

Connect to local SQL Server(IP address) via browser PHP

I'm developing an website in HTML,PHP,etc... on shared hosting.
I have a local machine that has SQL Server and SSMS 2014 with information that i need to get on that website.
On my shared host i have this php settings:
Drivers of ODBC:
I have already connected via ip address to db in other local machine but when i try connect on browser i get the same error over and over.
I tried with sqlsrv_connect: (im not showing the ip for security reasons)
$serverName = "127.0.0.1";
$uid = "admin";
$pwd = "admin***";
$databaseName = "mydb";
$connectionInfo = array( "UID"=>$uid,
"PWD"=>$pwd,
"Database"=>$databaseName);
// Connect using SQL Server Authentication.
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( ($errors = sqlsrv_errors() ) != null) {
foreach( $errors as $error ) {
echo "SQLSTATE: ".$error[ 'SQLSTATE']."<br />";
echo "code: ".$error[ 'code']."<br />";
echo "message: ".$error[ 'message']."<br />";
}
}
$tsql = "SELECT mor1_emp,mor2_emp,mor3_emp FROM rh_company";
sqlsrv_close( $conn);
I tried with PDO odbc:
try {
$dsn = "mysql:dbname=testdb;host={$db_host1}";
$connection = new PDO("odbc:Driver={SQL Server};Server=127.0.0.1;Database=mydb; Uid=admin;Pwd=admin***;");
} catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
}
I tried with PDO mssql:
try {
$hostname = "127.0.0.1"; //host
$dbname = "mydb"; //db name
$username = "admin"; // username like 'sa'
$pw = "admin***"; // password for the user
$dbh = new PDO ("mssql:host=$hostname;dbname=$dbname","$username","$pw");
} catch (PDOException $e) {
echo "Failed to get DB handle: " . $e->getMessage() . "\n";
}
$stmt = $dbh->prepare("SELECT * FROM table");
$stmt->execute();
while ($row = $stmt->fetch()) {
print_r($row);
}
The error that i'm always getting with sqlsrv_connect:
SQLSTATE: IMSSP
code: -49
message: This extension requires the Microsoft ODBC Driver 11 or 13 for SQL
Server. Access the following URL to download the ODBC Driver 11 or 13 for SQL Server for x64: http://go.microsoft.com/fwlink/?LinkId=163712
SQLSTATE: IM002
code: 0
message: [unixODBC][Driver Manager]Data source name not found, and no default driver specified
With PDO(ODBC):
SQLSTATE[IM002] SQLDriverConnect: 0 [unixODBC][Driver Manager]Data source name not found, and no default driver specified
With PDO(mssql): could not find driver
With PDO the right syntax is :
$dbh = new PDO ("**sqlsrv**:Server=$hostname,$port;Database=$dbname","$username","$pw");

MySQL and PHP connection failing

I have an issue with PHP script not connecting to MySQL database. Below code always generates error:
$dbconn = #mysql_connect('SFDC1', '<username>', '<password>');
if (!$dbconn)
{
die('Error connecting to DB!');
}
There is no issue if I connect to the database using MySQL workbench with same credentials. Issue only occurs during the communication between PHP and MySQL.
Any help on debug of this issue?
Try this one;
$dbhost = "localhost";
$dbuser = "username";
$dbpass = "pass";
$dbname = "DBname";
if(!#mysql_connect($dbhost,$dbuser,$dbpass)) {
echo "Cannot connect";
die();
} else
mysql_select_db($dbname);// or report();
Use this code to connect mysql.
<?php
$dbconn = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$dbconn ) {
die('Could not connect: ' . mysql_error());
}
?>

SQLSTATE[HY000] [2002] Connection refused

I am trying to create a simple mailing list webpage. Using LAMP. Here is the code I have for connecting to my database:
<?php
function insert()
{
$servername = "127.0.0.1";
$username = "root";
$password = "(my password here)";
$dbname = "(my db name here)";
try
{
// preparing database handle $dbh
$dbh = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
// set the PDO error mode to exception
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e)
{
echo ("Could not connect to server.\n");
echo ("getMessage(): " . $e->getMessage () . "\n");
}
}
?>
The function continues after that but this connection attempt gets cung up on the catch, and throws the "SQLSTATE[HY000] [2002] Connection refused" error. I have tried:
-Changing the $servername to localhost, but this gives me a different error: "SQLSTATE[HY000] [2002] No such file or directory"
-trying to specify all different ports
-checked all my info, database name and password.
-I can log into phpmyadmin and see that my database is fine
-looked at all other questions on this topic, with no help found.

How to connect to SQL Server 2012 via PHP 5.5.7 Red Hat

I am trying to connect MS SQL Server 2012 with php. My OS is Linux Red Hat and I am using PHP 5.5.7 version. This is my connection function:
function connect_ms_sql()
{
try {
$hostname = "xxx";
$port = 111;
$dbname = "xxx";
$username = "xxx";
$pw = "xxx";
$dbh = new PDO ("dblib:host=$hostname:$port;dbname=$dbname", "$username", "$pw");
} catch (PDOException $e) {
echo "Failed to get DB handle: " . $e->getMessage() . "\n";
exit;
}
unset($dbh);
}
When I run this function, it shows me error: Failed to get DB handle: could not find driver. How I can fix it? I tried with FreeTDS, same error. Please help me!

connect PostgreSQL DB and php

I inastall php 5.4, apache and postgresql 9.2 on Windows 7 OS.
Now, I am trying connect to postgresql DB from php file, this is php connect code:
$dbconn = pg_connect("host=localhost dbname=postgres user=postgres password=pass");
This gives: Call to undefined function pg_connect()
In php.ini file I have: extension="path\to\extension\php_pgsql.dll"
What may causes this error?
I too have tried to connect to a database using pg_connect but I am getting Error: call to undefined function. I have used PDO to connect to database. This seems to work.
<?php
try {
$dbuser = 'postgres';
$dbpass = 'your password';
$host = 'localhost';
$dbname='dbname';
$connec = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);
}catch (PDOException $e) {
echo "Error : " . $e->getMessage() . "<br/>";
die();
}
?>

Categories