I have connected my appication with aws rds with the script below:
function db_connect() {
$result = new mysqli($_SERVER['RDS_HOSTNAME'], $_SERVER['RDS_USERNAME'], $_SERVER['RDS_PASSWORD'], $_SERVER['RDS_DB_NAME'], $_SERVER['RDS_PORT']);
if (!$result) {
throw new Exception('Could not connect to database server');
} else {
return $result;
}
}
It was working fine then i had to change it due to a new feature which someone else wrote the code for:
$dbOptions = array(
'db_host' => 'RDS_HOSTNAME',
'db_user' => 'RDS_USERNAME',
'db_pass' => 'RDS_PASSWORD',
'db_name' => 'RDS_DB_NAME'
);
I didn't add the port because he didn't and i never connected to database with an array and key like this, how do i go about adding port like at what position.
I was forgetting the $_SERVER variable:
$dbOptions = array(
'db_host' => $_SERVER['RDS_HOSTNAME'],
'db_user' => $_SERVER['RDS_USERNAME'],
'db_pass' => $_SERVER['RDS_PASSWORD'],
'db_name' => $_SERVER['RDS_DB_NAME']
);
Related
If I connect to the database using 'localhost' as hostname, It works
$flag = [
'database_host' => 'localhost',
'database_port' => '',
'database_name' => 'dbdemo',
'database_user' => 'userdemo',
'database_password' => 'uijk',
'database_table' => 'tabledemo',
];
$link = mysqli_connect($flag['database_host'],$flag['database_user'],$flag['database_password'],$flag['database_name']);
But when I set up the hostname by using his ip (which I get through echo $_SERVER['SERVER_ADDR'];), It returns the error
Connection refused
I'm confused, is it not basicaly the same thing?
I have PHP-Solr extension version 1.0.3-alpha installed on my server. I'm initializing the connection using:
$options = array(
'hostname' => 'hostname',
'login' => '',
'password' => '',
'port' => '',
'path' => 'solr/core1');
$client = new SolrClient($options);
It returns the following object:
class SolrClient#17 (1) { private $_hashtable_index => int(13444) }
But whenever I try to run a query like:
$qryArray = 'key_id:123456';
$client->deleteByQuery($qryArray);
It throws an exception as follows:
Solr HTTP Error 7: 'Couldn't connect to server
Can anyone help me to find what would causing this issue?
I have tried var_dump($client->getDebug());, but it returns NULL.
$servername1 = "localhost1";
$username1 = "user1";
$password1 = "pwd";
$dbname1 = "dbs1";
$servername2 = "localhost2";
$username2 = "user2";
$password2 = "ped2";
$dbname2 = "dbs2";
// Create connection
$conn = new mysqli($servername1, $username1, $password1, $dbname1);
// Check connection
if ($conn->connect_error) {
$conn = new mysqli($servername2, $username2, $password2, $dbname2);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);}
}
Can this type of connection be implemented?? when i implement this deliberately making an error in first database credentials i get a warning that Unknown MySQL server host 'localhost1'
and doesn't load the page. why is this not working?
I make a list of databases and then iterate through them and try to connect. It should be no problem using multiple databases. Using localhost1 and localhost2 (only localhost is possible which is 127.0.0.1) is not possible. You should use IP addresses.
Here is the code:
<?php
$Databases = array(
0 => array(
'db_kind' => 'postgres',
'host' => $server1[$server1["current"]]["host"],
'port' => $server1[$server1["current"]]["port"],
'db_name' => 'database1',
'username' => 'xxx',
'password' => 'yyy',
"type"=>DB_TYPE_3,
),
1 => array(
'db_kind' => 'postgres',
'host' => $server1[$server1["current"]]["host"],
'port' => $server1[$server1["current"]]["port"],
'db_name' => 'database2',
'username' => 'xxx',
'password' => 'yyy',
"type"=>DB_TYPE_2,
),
2 => array(
'db_kind' => 'postgres',
'host' => $server1[$server1["current"]]["host"],
'port' => $server1[$server1["current"]]["port"],
'db_name' => 'database3',
'username' => 'xxx',
'password' => 'yyy',
"type"=>DB_TYPE_1,
),
);
for($i=0;$i<count($Databases);$i++)
{
if($last_db_ind>=count($Databases)){
$last_db_ind=0;
}
if(($db =& db_connect($last_db_ind++))==false){
echo 'Connection failed for DB: '. $Databases["db_name"] . ', DB index: '.$last_db_ind;
continue;
}
?>
You should either use "localhost" or an IP address to connect to the SQL server. In your case it tries to connect to "localhost1" (as in you variable) which doesn't exists (unless you changed your host file). Try using IPs (127.0.0.1 for localhost and any other for the backup)
Cheers,
JCK
You Can try Ping first.
OR
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ")
" . $mysqli->connect_error;
}
OR
extension_loaded("mysql");
Notice: Array to string conversion in C:\xampp\xampp\htdocs\classes\DB.php on line 21
Warning: PDO::__construct(): php_network_getaddresses: getaddrinfo failed: No such host is known. in C:\xampp\xampp\htdocs\classes\DB.php on line 21
SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: No such host is known.
It gives me this error when its on XAMPP, but when i put it on a live server it works fine, I know the DB credentials are correct, I am using the current version of XAMPP (as of yesterday) for windows and using Apache 2 on the live server, both with MySql.
Line 18-25:
// Takes values from 'config.php' and uses them to connect
private function __construct() {
try {
$this->_pdo = new PDO('mysql:host=' . Config::get('msql/host') . ';dbname=' . Config::get('mysql/db'), Config::get('mysql/username'), Config::get('mysql/password'));
} catch(PDOException $e) {
die($e->getMessage());
}
}
And this is where the array is set:
// Sets the config valuses
$GLOBALS['config'] = array(
'mysql' => array(
'host' => '127.0.0.1',
'username' => 'root',
'password' => 'password',
'db' => 'site'
),
'remember' => array(
'cookie_name' => 'hash',
'cookie_expiry' => 604800
),
'session' => array(
'session_name' => 'user'
)
);
And the config class:
class Config {
public static function get($path = null) {
if($path) {
$config = $GLOBALS['config'];
$path = explode('/', $path);
foreach($path as $bit) {
if(isset($config[$bit])) {
$config = $config[$bit];
}
}
return $config;
}
return false;
}
}
Typos:
$this->_pdo = new PDO('mysql:host=' . Config::get('msql/host') . ';dbname=' .
^^^^^---no Y
$GLOBALS['config'] = array(
'mysql' => array(
^---has a Y
$this->_pdo = new PDO("mysql:host=".config::get('mysql/host').";dbname=".config::get('mysql/database') ,config::get('mysql/user'),config::get('mysql/password'));
Please check! Is there any space between "mysql:host=".config::get('mysql/host')."? I was getting the same error because of space!
$dbhost = "localhost:3306"; adding the MySQL port worked for me
I'm trying to figure out how to code a loop in a PHP script that:
gets $hostname, $username $password and $platform from an included script for a bunch of MySQL databases (different remote servers with different access credentials) I just have read-only access to
runs the PHP script on each of the databases
I have all the variables stored in an array that looks like this:
$servers = array(
'server1' => array(
'hostname' => '<serverurl>'
'username' => 'readonly',
'password' => 'pword',
'platform' => 'platform'
),
'server2' => array(
'hostname' => 'serverurl'
'username' => 'readonly',
'password' => 'pword',
'platform' => 'platform'
),
},
I'm having trouble figuring out how to pass those values into a loop statement in my PHP script though - how would I make it run on every server in the array?:
$dbhandle = mysql_connect($hostname, $username, $password) or die(mysql_error('Unable to connect to MySQL'));
echo 'Connected to MySQL<br>';
mysql_select_db($platform, $dbhandle) or die(mysql_error('Unable to connect to database'));
echo 'Connected to database<br>';
Sorry for the noob question/if this is a repeat - I couldn't find anything similar when I searched. Is there a good site for me to look up this sort of thing? Thanks!
<?php
$dbhandles = array();
foreach($servers as $server => $details) {
$dbhandles[$server] = mysql_connect($details['hostname'], $details['username'], $details['password']) or die(mysql_error('Unable to connect to MySQL'));
}