How to connect to oracle from php with tnsnames? - php

I am getting this error:
Server Response='12154 ORA-12154: TNS:could not resolve the connect identifier specified
I am on Ubuntu 14.04
My environment variables are:
ORACLE_HOME = /usr/lib/oracle/12.1/client64​
LD_LIBRARY_PATH = /usr/lib/oracle/12.1/client64/lib
TNS_ADMIN = /usr/lib/oracle/12.1/client64/network/admin
tnsnames.ora and sqlnet.ora are within /usr/lib/oracle/12.1/client64/network/admin
PS: I can connect through sqlplus with:
sqlplus64 user/pass#dbname
This is the code:
<?php
$conn = oci_connect('user', 'pass', 'dbname');
?>

It never worked on that way, what I did was to use Easy Connect string:
$conn = oci_connect('user', 'pass', 'host/servicename'); however the first way should have worked because in another environments it works.

tnsnames.ora file should look as follows
DBNAME=
(DESCRIPTION=
(ADDRESS=
(PROTOCOL=TCP)
(HOST=YOUR IP ADDRESS)
(PORT=YOUR PORT NUMBER)
)
(CONNECT_DATA=
(SERVICE_NAME=YOUR DBNAMEPROD)
)
)
conn.php file should look as follows
$USERNAME = "hr"; // Login Username
$PASSWORD = "hr"; // Login Passowrd
$DATABASE = "DBNAME"; // Connect string to connect to your database found in tnsnames.ora
$conn = oci_connect($USERNAME, $PASSWORD, $DATABASE);
if(!$conn){
echo "Your Connection Has an error";
}
else{
echo "Your Connection is Successful"
}

Related

How can I bypass error 111 MySQL in PHP connection?

I have bought a database MySQL to use on my domain/host.
I can connect to the database via wampserver (localhost) without problems.
But when I am trying to connect to the host website it gives me an error.
Connection failed: Can't connect to MySQL server on 'mysql-******.net' (111 "Connection refused")
My code:
<?php
$servername = "mysql-*********.net";
$username = "username8954";
$password = "dolphyn1235";
$dbname = "animals";
$dbServerPort = "26313";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname, $dbServerPort);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
Any ideas how to solve this?
Check in your MySQL Server configuration files (my.cnf), find bind-address = 127.0.0.1 then comment them with # at the begining of the line. and then restar your MySQL.

How to fix error in dbconn file when upload php mysql project to free webhosting site?

I've been trying to upload my PHP MySQL(in Dreamweaver) project to a free web-hosting site.
When I logged in, there is an error that appear in dbconn.php file.
The error is shown below:
and here's the code in my dbconn.php file:
<?php
/* php& mysqldb connection file */
$user = 1350048; //mysqlusername to db
$pass = "password"; //mysqlpassword to db
$host = "eskl.freeoda.com"; //server name or ipaddress
$dbname= 1350048; // db name in server freeoda
$dbconn= mysql_connect($host, $user, $pass);
if(isset($dbconn)){
mysql_select_db($dbname, $dbconn) or die("<center>Error: " . mysql_error() . "</center>");
}
else{
echo "<center>Error: Could not connect to the database.</center>";
}
?>
I would really appreciate if anyone can teach me how to solve this.. thanks in advance!
As Kerbholz already stated, don't use mysql_* functions, they are really outdated.
Instead use mysqli:
$servername = "eskl.freeoda.com";
$username = "1350048";
$password = "password";
$database = "1350048";
// Create connection
$conn = new mysqli($servername, $username, $password, $database);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
For your error it got mostly something to do your host doesn't allow remote connections. Try to change the serverhost to localhost or 127.0.0.1

Cannot change php from localhost details to my servers to connect sql database

I am new to php/sql and i have a login system which runs on my local host using xammp and it all works fine. I now want to upload it to my website but the code no longer works... I have created a sql db on my hosting service and tried to change the code.
the code that is used on the local host is
<?php
$dbServername = "localhost";
$dbUsername = "root";
$dbPassword = "";
$dbName = "loginsystem";
$conn = mysqli_connect($dbServername, $dbUsername, $dbPassword, $dbName);
and this is the code that i have got from my hosting.
<?php
$host_name = 'db682827654.db.1and1.com';
$database = 'db682827654';
$user_name = 'dbo682827654';
$password = '<Enter your password here.>';
$conn = mysql_connect($host_name, $user_name, $password, $database);
if (mysql_errno()) {
die('<p>Failed to connect to MySQL: '.mysql_error().'</p>');
} else {
echo '<p>Connection to MySQL server successfully established.</p >';
}
?>
however this brings up an errror message. I have changed the password to the password for my database but its still not connecting.
This is the error message.
Failed to connect to MySQL: Access denied for user 'dbo706265806'#'217.160.62.78' (using password: YES)
Any help would be greatly appreciated
use mysqli_connect (not mysql_connect) like localhost:
<?php
$dbServername = "db682827654.db.1and1.com";
$dbUsername = "dbo682827654";
$dbPassword = "<Enter your password here.=)>";
$dbName = "db682827654";
$conn = mysqli_connect($dbServername, $dbUsername, $dbPassword, $dbName);
?>
On your computer you are using mysqli_connect, but on the server you are trying to use mysql_connect. Just use the same file from your computer and simply change $dbServername, $dbUsername, $dbPassword and $dbName to match those that your hosting provided.

Connect to AWS mysql over SSL in PHP?

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);

MSSQL using PDO to connect

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.

Categories