connecting to multiple databases on different servers in php - php

I am trying to connect to two different databses using php
error_reporting(E_ALL);
$con= mysqli_connect("localhost", "phpapp", "phpapp", "hazard") or die("error connecting database 1".mysqli_error($con));
$con_vpn= mysqli_connect("xxx.xxx.xxx.xxx", "user", "pass", "db_name") or die("error connecting database 2".mysqli_error($con_vpn));
When I run the application it is showing error : error connecting database 2. It is not even printing the error.
thanks in advance:)

That's because you're trying to use a handle from a failed connection. Since the connection failed, that handle is invalid. That's why there mysqli_connect_error(), which will return the error message from the LAST attempted connection.
$con_vpn = mysqli_connect(....) or die(mysqli_connect_error());
Note that the connect_error function takes no parameters - it doesn't need any.

Related

PHP Oracle connection

I am attempting to write some connection code with PHP to a Oracle database my school is hosting.
I'm using oci_connect() at the moment to make this connection, but it is failing.
$conn = oci_connect('username', 'password', 'hostname/SID');
I can access the oracle database through sqlDeveloper, as well as phpmyadmin, so I know the login information is correct.
I checked the oracle version with select * from v$version;, it shows as 12c Enterprise.
What is wrong with my php code for connecting? Is there a better way to make an oracle connection through PHP?
This is the test code I'm running, from http://php.net/manual/en/function.oci-error.php
<?php
echo "running";
$conn = oci_connect("username", "paswwrod", "address/SID");
if (!$conn) {
$e = oci_error(); // For oci_connect errors do not pass a handle
trigger_error(htmlentities($e['message']), E_USER_ERROR);
}
echo "ending";
?>
The string "running" gets echoed, but "ending" does not, the script just stops working when it attempts oci_connect()
have you also tried including the port number to the oracle db server like so?
$conn = oci_connect("user", "pass", "localhost:1234/xe");

MySQL Log-In with PHP - How often?

Okay this might be an obvious question, but as soon as i call MySQL with PHP (whhich means i log in) and then close the PHP tag, (shown below) Do I have to call the database again later?
$mysqli = new mysqli("host", "username", "pw", "dbname");
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
$res = $mysqli->query("SELECT EmbedURL FROM Videos ORDER BY RAND() LIMIT 8");
etc...
And then I need to call it again later in the script to fetch something else fromt he database. Do i have to log into the database again?
Thank you!
The database connection will stay open if you do not close it manually. The only possible cause I can imagine that you could loose the connection is the connection timed out or was closed by the databse because you have to many active connections.
It looks like the database variable is nested deeply in your code. You will also loose the reference of the connection if you close the current code block (}).
Here is a post about MySQL connection pooling which could be insteresting for your problem: Connection pooling in PHP

PHP error finding database when trying to connect to it with mysqli_connect()

I am reviewing my old work and have started to change the mysql syntax to mysqli however I am having issues trying to connect to my database. It throws the following error: No database selected.
This is my code:
$con = mysqli_connect("localhost","root","password","db1");
// Check connection
if (mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
Your assistance would be greatly appreciated.
Your code is correct, probably you are giving a database name that is wrong, check if that database actually exists.

Connecting to a mysql database without keeping details in the script

I am attempting to create a separate login file for database connections as I am not too fond of having all the access details on each page that requires database access.
I have created a separate file on my server that contains the variables required for a successful login and then use the;
include_once('path_to_file/filename.php');
to get the variables and then use;
$dbconnection = mysqli_connect("$hostname","$username","$password","$database") or die ("Could not connect to the server");
but the connection fails every time. I tried including the connection script in the file I am attempting to include but then I get this message:
Can't connect to local MySQL server through socket '/tmp/mysqld.sock' (2)
I'm not really sure how to fix this, but every page in my server more or less access the database and I think it has to be a security risk having login details replicated everywhere!
Anyone have any suggestions or alternatives?
databaseloging format is:
<?php
# parameters for connection to MySQL database
$hostname="hostname";
$database="databasename";
$username="username";
$password="password";
?>
P.S. I have also tried require and got the same result.
Also when using multiple MySQL connections in PHP, you have to supply a fourth argument telling PHP to actually create new connections like this (this is very important, if you are using two connections to the same host):
$db1 = mysql_connect($host1, $user1, $passwd1, true);
$db2 = mysql_connect($host2, $user2, $passwd2, true);
If the fourth argument is not used, and the parameters are the same, then PHP will return the same link and no new connection will be made.
After this you should use "mysql_query" with an extra parameter than defines which connection to use:
$res1 = mysql_query($sql1, $db1) or die(mysql_error($res1));
$res2 = mysql_query($sql2, $db2) or die(mysql_error($res2));
http://www.php.net/manual/en/function.mysql-connect.php

PHP connecting to DB : OOP vs Procedural 1 dont work 2 works

I am scratching my empty head with this issue:
When I have my logging data as procedural, everything works fine. I require the file once and can proceed with my select queries. If the connection to the DB file is however written as OOP. I get the error that No database selected
====
<?php
// SETTING VALUES AS CONSTANTS
DEFINE('DB_USER', 'root');
DEFINE('DB_PASSWORD', '');
DEFINE('DB_HOST', 'localhost');
DEFINE('DB_NAME', 'tra');
// CONNECTING JUST TO THE SERVER
$dbc = #mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if (!$dbc) // IF IT CAN'T CONNECT, ISSUE A MESSAGE
{
die('Could not connect: ' . mysql_error());
}
// CONNECTING NOW TO THE DB
mysql_select_db("tra", $dbc);
?>
So that up there works fine. I have got that in a separate file, (out of the root path)
But if I try to do it OOP, then, the rest of the queries that are to work once I have the access to the DB won't work. I sometimes get the query written as output, I mean as if not executing, just the plain text:
$mysqli = new mysqli("localhost", "root", "");
$mysqli->select_db("tra");
I have tried tons of varieties with the OOP version, first instantiating separately such as
$mysqli = new mysli();
$mysqli->connect("127.0.0.1", "root", "", "tra");
or also
$mysqli = new mysqli("localhost", "root", "");
$mysqli-> select_db("tra");
but none of the tries with OOP will work. I mean, I have the book on my lap, so I am writing it ad litteram. The issue must be elsewhere.
The PHP version is 5.3.8 about the latest one.
The sql query that follows after the connection and selection of the DB (which works if the connection file is procedural, as I say) is:
$sql ="SELECT FName
FROM work_assignment, developer
WHERE developer.country = '".$country."'
AND work_assignment.from_language = '".$from."'
AND work_assignment.into_language = '".$into."'
AND work_assignment.developer_id = developer.developer_id
";
Any ideas as to why?
Thanks a lot
From your comment I surmise you're mixing the mysql and mysqli extensions. They are two separate extensions with nothing in common, except that they both connect to MySQL. If you're connecting to the database using a mysqli object, use that object to run your queries. mysql_query will have no active connection, because you did not establish one using a mysql function.

Categories