PHP, PDO => MSSQL with 2 instances - php

i wrote a simple script to query some data from a MSSQL-Express 2012 Server.
All workes fine with the first instance:
$host = "192.168.13.3\test1";
$user = "sa";
$passwd = "test1";
self::$instance = new PDO ("dblib:host=$host","$user","$passwd");
but if I try to connect to my second Instance like:
$host = "192.168.13.3\test2";
$user = "sa";
$passwd = "test2";
self::$instance = new PDO ("dblib:host=$host","$user","$passwd");
I get this error:
[message:protected] => SQLSTATE[HY000] Unable to connect: Adaptive Server is unavailable or does not exist (severity 9)
[string:Exception:private] =>
[code:protected] => 20009
With MSSQL-Server-Management-Tool I can access both instances without problems.
Has somebody a hint where i can find my failure?
Thanks in advance

Related

F3 Framework: how to make sure that caching is working correctly?

I'm getting the database credentials from AWS secrets manager and storing it in the cache so that it doesn't have to be fetched from AWS on every request.
The problem is that, if I change the secret name for testing, F3 won't be able to connect to the database. That means that it's detecting the secret name getting changed even though I tell F3 to check that only if it wasn't able to find anything cached.
use Aws\SecretsManager\SecretsManagerClient;
$f3->set('CACHE', true);
if ($f3->exists('dbusername')) {
$username = $f3->get('dbusername');
$password = $f3->get('dbpassword');
$host = $f3->get('dbhost');
$port = $f3->get('dbport');
} else {
$secretName = getenv('AWS_SECRET_NAME');
$client = new SecretsManagerClient([
'version' => 'latest'
]);
$secretManager = $client->getSecretValue([
'SecretId' => $secretName,
]);
$db = json_decode($secretManager['SecretString']);
$username = $db->username;
$password = $db->password;
$port = $db->port;
$host = $db->host;
}
$f3->set('dbusername', $username);
$f3->set('dbpassword', $password);
$f3->set('dbhost', $host);
$f3->set('dbport', $port);
I'm testing on my PC, I don't know if that code would work on a server, not sure if the issue from my PC or if I'm not caching correctly.
It turns out that the cache wouldn't work unless a ttl (time to live) is specified. Here's how to cache the values above for 24 hours.
$f3->set('dbusername', $username, 86400);
$f3->set('dbpassword', $password, 86400);
$f3->set('dbhost', $host, 86400);
$f3->set('dbport', $port, 86400);

PHP connection string to remote database ORA-12154

I have 2 computers:
PC 1 - Here is where I installed the XAMPP.
PC 2 - Here is where my database which is Oracle 9i is installed.
I am using PHP 7 and already added PDO_OCI extension.
Here is my connection string:
define("DB_HOST", "192.168.10.30:1521");
define("DB_NAME", "BACKEND");
define("DB_USER", "sa");
define("DB_PASS", "sa_backend");
new PDO('oci:dbname='. DB_NAME . ';host='. DB_HOST .';', DB_USER, DB_PASS);
When I used this code I am getting this error:
Warning: Uncaught PDOException: SQLSTATE[42S02]: pdo_oci_handle_factory: ORA-12154: TNS:could not resolve the connect identifier specified (ext\pdo_oci\oci_driver.c:709)
UPDATE 1
$server = "192.168.10.30";
$db_username = "sa";
$db_password = "sa_backend";
$service_name = "backend";
$sid = "backend";
$port = 1521;
$dbtns = "(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = $server)(PORT = $port)) (CONNECT_DATA = (SERVICE_NAME = $service_name) (SERVER = SHARED) (SID = $sid)))";
$this->dbh = new PDO("oci:dbname=" . $dbtns . ";charset=utf8", $db_username, $db_password, array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_EMULATE_PREPARES => false,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC));
I used this code I found on other stackoverflow link. Now I am getting this error
SQLSTATE[HY000]: pdo_oci_handle_factory: ORA-12520: TNS:listener could not find available handler for requested type of server (ext\pdo_oci\oci_driver.c:728)
I tried to updated the process to 200 even to 400 but the error is still the same.

Connecting To Mongo Replica Set With Doctrine MongoODM Module

We are in the process of updating our API's MongoDB hosting provider from mLab to MongoDB Atlas.
I have updated our connection server to PHP 7.4 with MongoDB PHP extension 1.7.4.
I have updated our API framework from Apigility to Laminas API Tools using the DoctrineMongoODMModule
I can successfully connect using the mongo shell using the following syntax:
mongo "mongodb+srv://test-server-dbteb.mongodb.net/<dbname>" --username <username>
I have looked far and wide to find a sample configuration of the DoctrineMongoODMModule with it's configuration file to connect to a MongoDB Atlas replica set using the mongo+srv:// protocol with no success to this point. Currently the errors are Failed to parse MongoDB URI.
If anyone has had a similar experience, any help would be greatly appreciated.
I had the same issue and im still looking forward to get a real solution.
I directly edited the ConnectionFactory.php in the doctrine-mongo-odm-module (src/DoctrineMongoODMModule/Service/ConnectionFactory.php) with the following code
//...
if (empty($connectionString)) {
$connectionString = 'mongodb+srv://'; //prev: 'mongodb://'
$user = $options->getUser();
$password = $options->getPassword();
$dbName = $options->getDbName();
if ($user && $password) {
$connectionString .= $user . ':' . $password . '#';
}
$connectionString .= $options->getServer() /*. ':' . $options->getPort()*/;
if ($dbName) {
$connectionString .= '/' . $dbName;
}
} else {
// parse dbName from the connectionString
$dbStart = strpos($connectionString, '/', 11);
if ($dbStart !== false) {
$dbEnd = strpos($connectionString, '?');
$dbName = substr(
$connectionString,
$dbStart + 1,
$dbEnd ? ($dbEnd - $dbStart - 1) : PHP_INT_MAX
);
}
}
//...
and in my connection file:
//...
'odm_default' => array(
'server' => '<host>',
'port' => '',
'connectionString' => null,
'user' => '<user>',
'password' => '<psw>',
'dbname' => '<db>',
'options' => array()
),
//...
I think this is the worst possible solution (editing vendor' files) but in my case worked, take that as a temporary fix

Create mysql database using cpanel api

I am trying to create mysql database using cpanel xmlapi.
After search on google i found the below code.
$auth_user = 'XXXXXX';
$auth_pass = 'XXXXX';
$server = 'XXXXXXXX.com';
$json_client = new \xmlapi($server);
$json_client->set_output('json');
$json_client->set_port(2083);
$json_client->password_auth($auth_user, $auth_pass);
$json_client->set_debug(1);
# Create Database
$result = $json_client->api1_query($auth_user, 'Mysql', 'adddb',array($shop->alias));
var_dump($result);
After running this code I got this error:
string(202) "{"cpanelresult":{"apiversion":"2","error":"The “cpanel_jsonapi_module†parameter is required.","data":{"reason":"The “cpanel_jsonapi_module†parameter is required.","result":"0"},"type":"text"}}"

Mongodb-PHP app connection

For connecting Mongodb with PHP application, i installed Mongodb Driver in windows and extension was enabled (checked in phpinfo()). Then i execute the folloeing php code
<?php
// Config
$dbhost = 'localhost';
$dbname = 'test';
// Connect to test database
$m = new Mongo("mongodb://$dbhost");
$db = $m->$dbname;
// select the collection
$collection = $db->shows;
// pull a cursor query
$cursor = $collection->find();
foreach($cursor as $document) {
var_dump($document);
}
?>
and it returns a fatal error. How can solve this?
Fatal error: Uncaught exception 'MongoConnectionException' with message 'Failed to connect to: localhost:27017: No connection could be made because the target machine actively refused it. ' in C:\xampp\htdocs\check\index.php:7 Stack trace: #0 C:\xampp\htdocs\check\index.php(7): MongoClient->__construct('mongodb://local...') #1 {main} thrown in C:\xampp\htdocs\check\index.php on line 7
This is connection and test wode. check whether it is working in your system or not :
<?php
$m = new MongoClient("localhost");
$dbname = $m->selectDB("Test");
$collection = $dbname->selectCollection("Shows");
$data1 = array(
'user_id' => 'abc',
'age' => 20,
'Status' => 'A'
);
$dbname = $collection->insert($data1, array('$data1' => 1));
$results = $collection->find();
echo "<pre>";
foreach($results as $test) {
print_r($test);
}
echo "</pre>";
?>
Have you started mongod server , i could replicate your error only when i closed mongod server .

Categories