Connecting to a remote mysql server using a function - php

I'm using wordpress and I have a remote mysql server for some other data not stored on wordpress db.
Now I want to connect to that remote mysql server in a form of a php function but i don't know how or if it is possible. Basically this function serves as to check if $orderid exist on the remote mysql server.
function check_orderid($orderid) {
// Connect to database
// Check if $orderid exist if yes return true.
}
The remote server does accept remote connections.
Also, Is it okay and will not affect wordpress db connection?

Connecting to a remote server is not different from connecting to a local server, besides the host address. Just replace the 'localhost' with a resolvable dns name, or the ip address from your remote server, and you should be fine.

To make multiple connection you have to use links.
they are created after successful connect:
$link1 = mysqli_connect('localhost', 'my_user', 'my_password', 'my_db');
$link2 = mysqli_connect('localhost', 'my_user2', 'my_password2', 'my_db2');
you can use them like:
mysqli_query($link1, "your first query as string");
mysqli_query($link2, "your second query as string");
By official OO version you should use:
$mysqli1 = new mysqli('localhost', 'my_user', 'my_password', 'my_db');
$mysqli2 = new mysqli('externalIP', 'my_user2', 'my_password2', 'my_db2');
$mysqli1->query("some query");
$mysqli1->query("some other query");
so, in this case you can easily do this:
$externaldbcon = new mysqli('ExtrnalDBIP', 'my_user', 'my_password', 'my_db');
$externaldbcon->query("your query for external DB");

Related

PHP OCI: Connection string (convert from JDBC)

I have below obfuscated connection string in SQL developer which works:
jdbc:oracle:thin:#//xyz-scan.example.com:1521/mydb.example.com
How can I use this in php oci_connect?
$db = 'xyz-scan.example.com:1521/mydb.example.com';
$con = oci_connect('scott', 'tiger', $db, 'AL32UTF8');
Lead to error:
ORA-12545: Connect failed because target host or object does not exist
I can ping the server successfully.
I also tried
$db = '(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)
(HOST = xyz-scan.example.com)(PORT=1521))
(CONNECT_DATA=(SERVER=DEDICATED)
(SID=mydb.example.com)';
And instead of SID with service_name. Nothing works.
Above gives this error:
ORA-12154: TNS:could not resolve the connect identifier specified
How do I convert this connection string to work with php oci? (is there a unique way? For a different db I have one with #ldap://... how would I convert that?
The solution to first issue with scan address is simple:
$db = '(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)
(HOST = xyz-scan.example.com)(PORT=1521))
(CONNECT_DATA=(SERVER=DEDICATED)
(SID=<sid>)';
The second one with ldap is not so simple. As far as I understood oracle has it's own ldap thingy and in it you store the databases TNS entries. This means you query it like you query any ldap system:
<?php
$ds=ldap_connect("oid.mydomain.com", myport); // Connect to oracle ldap
$r=ldap_bind($ds); // Bind to ldap
$sr = ldap_search($ds, "cn=OracleContext,dc=xyz,dc=abc,dc=com", "cn=dbname"); // Run query xyz.abc.com
$info = ldap_get_entries($ds, $sr); // Get entries
ldap_close($ds);
$dbconnectstring = $info[0]["orclnetdescstring"][0]; // Extract db connect string from ldap search result array
$con = oci_connect('scott', 'tiger', $dbconnectstring);
?>
This script will get full TNS connection string which you can then use with oci_connect.

Can't connect my database on my live server

Hello I've been making a site on my local server and I finished it so I'm moving everything over to my live server. I've made a database in phpmyadmin on it and I would like to connect to it. I feel like I have the wrong inputs though because it gives me this error.
This is my code for my database connection.
<?php
return $conn = mysqli_connect('localhost', 'gener105_nate', '(Password)', 'user_data');
if (!$conn) {
die("Connection Failed: " . mysqli_connect_error);
}
I didn't actually enter password I just think I shouldn't show it anyway I think I just have something mixed up since I'm new at this.
Oh and the database is located within a database grouping should i input the path to it?
You need to tell it to connect to gener105_user_data instead of just user_data
return $conn = mysqli_connect('localhost', 'gener105_nate', '(Password)', 'gener105_user_data');

Can't connect to local MySQL server through socket, I'm not the server admin (using hosting plan)

I'm trying to connect to my SQL database and I'm getting the error mentioned above.
This is the PHP code
// This file has database info variables (password etc.)
include "db_config.php";
class DB_CONNECT
{
function __construct()
{
$this->connect();
}
function __destruct(){}
function connect()
{
$connect = mysql_connect($db_server, $db_user, $db_password) or die();
$db = mysql_select_db($db_name) or die();
echo "Connection successful!";
return $connect;
}
}
// Just to try out if the connection is successful
new DB_CONNECT();
This is the error
Warning: mysql_connect(): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) in /hermes/bosnaweb05a/b1/ipg.matejhacincom1/dbtest/db_connect.php on line 16
The funny thing is that I can connect to the database easily with this code which is generated for db testing by my hosting provider:
$link = mysql_connect('my db server', 'db user', 'db pass');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_select_db(my db table);
Could someone let me know what I'm doing wrong? I have no idea anymore.
The reason I mentioned that I'm not the server adming is because I saw a lot of questions like this here, but all of them have instructions how to solve it by using some server commands etc.
Either:
There isn't a MySQL server running on the same machine as the webserver or
The MySQL server is configured to place its socket file somewhere other than /var/run/mysqld/mysqld.sock (in which case you need to specify where it is or
The MySQL server is configured to listen only on TCP/IP (which is inefficient but it can be accessed by using the 127.0.0.1 IP address instead of localhost).
Probably in variables $db_server, $db_user, $db_password you have wrong data. You must pass to object data from db_config.php file. For example through constructor.
Have the same problem on an customer server, last week, then :
Be sure your $db_server is according your hosting ; not a remote server.
Then use 127.0.0.1 instead of localhost
In my case, the MySQL server was different the PHP.
Thank you for all of your answers but what solved my problem was nothing that was in any of your answers.
All I did was used PDO instead of deprecated mysql and it worked. I also for some reason needed to move inclulde "db_config.php" in to the class because variables didn't work otherwise.
Anyway, here's the code that now works:
class DB_CONNECT
{
function __construct()
{
$this->connect();
}
function __destruct(){}
function connect()
{
include "db_config.php";
$connect = new PDO($db_server, $db_user, $db_password) or die();
echo "Connection successful!";
return $connect;
}
}
// Just to try out if the connection is successful
new DB_CONNECT();

Having Trouble Connecting to MySQL Database

The following code successfully connects me to my mySQL database.
$conn = mysql_connect("localhost", "root", "") or die("Failed to Connect!");
$db = mysql_select_db("MyDB");
I have been experimenting on localhost using XAMPP and phpmyadmin, and everything works correctly. However, today I uploaded my website to my domain (I have bought a domain and web hosting through GoDaddy) and I keep getting "Failed to Connect!" whenever this code runs. The HTML/CSS work correctly, but I cannot connect to mySQL. How can I connect to my database on the server?
You'll need to change your connection information here:
mysql_connect("localhost", "root", "")
to include your GoDaddy database details instead. Contact GoDaddy for more information on what to use for your account.
You have not mentioned anything about MySQL database hosting.
If you are hosting the database also in godaddy, you should be able to get the connection string and host name from information mentioned here
First, You need to create database in your hosting server (phpmyadmin cpanel) and supply details in your database connect file.
**Looking at your database credentials I can say that you haven't created one yet since
the username is definitely not 'root' and password will be required.**
Sample db_connect file:
<?php
$hostname = 'e.g: internal-ra.db-server-123';
$username = 'e.g: serverone1234d4';
$password = 'e.g: your_own_password';
try {
$pdo = new PDO("mysql:host=$hostname;dbname=database_name_here", $username, $password);
/*** echo a message saying we have connected ***/
}
catch(PDOException $e){
echo 'Exception -> ';
var_dump($e->getMessage());
}
?>
And try to use so less as possible the key DIE()

Fetching data from multiple databases with MySQLi

i'am using MySQLi to connect and fetch data from my MySQL server with
$link = new mysqli("localhost", "root", "","database_1");
I have a file that used for connection and data collection (dboperations.php) from above database
Now , i need to connect another database (e.g. database_2) and fetch data in the same php file.
Conditions:
Are databases on the same server? YES
Am i authorized to connect with same username and pass? YES
Is there any way to do that? Thanks.
Use mysqli::select_db to switch databases on the same server:
$link = new mysqli('localhost', 'root', '', 'database1');
then
$link->select_db('database2');
SELECT * FROM database_2.table ...

Categories