Invalid Data Source Name - Connection to MySQL with php, PDO and DSN - php

This script
<?php
/* Connect to a MySQL database using driver invocation */
$dsn = 'mysqldg';
$user = 'odbc_dg';
$password = '999999999';
try {
$dbh = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
?>
gives the error *Connection failed: invalid data source name*
I have created an entry in /etc/odbc.ini as follows:
[mysqldg]
Description = DGDB
Driver = mysql537
Database = dg1
Servername = 99.99.99.99
UID = odbc_dg
PWD = 999999
SSLKeyFile = /etc/mysql/ssl/ck.pem
SSLCertFile = /etc/mysql/ssl/cc.pem
SSLCAFile = /etc/mysql/ssl/c1.pem
/etc/odbcinst.ini has the following entry:
[mysql537]
Description = MySQL driver for Plesk
Driver = /usr/lib/odbc2/lib/libmyodcb5w.so
Setup = /usr/lib/odbc2/lib/libmyodbc5w.so
The entry in odbcinst.ini works with a non-DSN connection.
I'm obviously missing something, can anyone help? Thanks.
UPDATED....
I have tried Your Common Sense's code as follows:
<?php
$host = '46.99.199.199';
$db = 'dg';
$user = 'odbc_dg';
$pass = '999999';
$charset = 'utf8mb4';
$options = array(
PDO::MYSQL_ATTR_SSL_KEY => '/etc/mysql/ssl/ck.pem',
PDO::MYSQL_ATTR_SSL_CERT => '/etc/mysql/ssl/cc.pem',
PDO::MYSQL_ATTR_SSL_CA => '/etc/mysql/ssl/c1.pem'
);
$dsn = "mysql:host=$host;dbname=$db;charset=$charset";
try {
$pdo = new PDO($dsn, $user, $pass, $options);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
?>
... but I get a connection fail message - Connection failed: SQLSTATE[HY000] [2002]
I think the problem is that the keys I have supplied only work with ODBC
For example this code, which uses odbc_connect works....
<?php
ini_set ('error_reporting', E_ALL);
ini_set ('display_errors', '1');
error_reporting (E_ALL|E_STRICT);
$user = "odbc_dg";
$pass = "99999";
$connection = "Driver= {mysql537};Server=46.99.199.199;Database=dgdb;UID=dgdb;PWD=999999;sslca=/etc/mysql/ssl/c1.pem;sslkey=/etc/mysql/ssl/ck.pem;sslcert=/etc/mysql/ssl/cc.pem";
$con = odbc_connect($connection, $user, $pass);
$sql="SELECT Id from stk_item";
$rs=odbc_exec($con,$sql);
if (!$rs) {
exit("Error in SQL");
}
echo "<table><tr>";
echo "<th>Companyname</th>";
echo "<th>Contactname</th></tr>";
while (odbc_fetch_row($rs)) {
echo odbc_result($rs, "Id"), "\n";
}
odbc_close($con);
echo "</table>";
?>
My problem is that I want to connect to the remote database via pdo because that's the only type of connection allowed in the Drupal synchronising code.

Related

Mysql multiple database connection doesn't work

I just tried to connect a secondary database like this example bellow but i don't know why refuse to work. Any idea?
I mention that each database connection works properly individualy.
$db_HOST = "localhost";
$db_USER = "db_user";
$db_PASS = "db_pass";
$db_NAME1 = "db_test1";
$db_NAME2= "db_test2";
$db_LINK1 = mysql_connect($db_HOST, $db_USER, $db_PASS) or die("Technical revision. Please try again later!");
mysql_select_db($db_NAME1, $db_LINK1) or die("Couldn't select database");
$db_LINK2 = mysql_connect($db_HOST, $db_USER, $db_PASS, true) or die("Technical revision. Please try again later!");
mysql_select_db($db_NAME2, $db_LINK2) or die("Couldn't select database");
Errors i get in the log file:
PHP Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in /config/global/variables.php on line 27
PHP Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in config/global/hello.php on line 3
Thank you!
Do the following (with PDO instead of mysql_connect as the latter is deprecated):
$db_HOST = "localhost";
$db_USER = "db_user";
$db_PASS = "db_pass";
$db_NAME1 = "db_test1";
$db_NAME2= "db_test2";
try {
$db_LINK1 = new PDO('mysql:host='.$db_HOST.';dbname='.$db_NAME1, $db_USER, $db_PASS);
foreach($db_LINK1->query('SELECT * from FOO') as $row) {
print_r($row);
}
$dbh = null;
} catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
try {
$db_LINK2 = new PDO('mysql:host='.$db_HOST.';dbname='.$db_NAME2, $db_USER, $db_PASS);
foreach($db_LINK2->query('SELECT * from FOO') as $row) {
print_r($row);
}
$dbh = null;
} catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
More info here: php.net/manual/en/pdo.connections.php
If the second connection fails check the exact error message.
You should really use PDO but your code works if you use your link in your db select. For example:
$db1 = mysql_connect($hostname, $username, $password);
$db2 = mysql_connect($hostname, $username, $password, true);
mysql_select_db('database1', $db1);
mysql_select_db('database2', $db2);
that should work. You forgot to select which database should be used on every link.
I thnk I found the problem.
I forgot to change all queries accordind to the new multiple connection.
LE: Solve confirmed! Thank you all!

Could not connect to MySQL in PHP

I have newly purchased server, on that server database connections are not working.
<?php
error_reporting(E_ALL);
$server = "server name";
$user = "username";
$password = "password";
$db = "test";
echo "Before";
$con = mysql_connect($server, $user, $password);
echo "After";
if (!$con){
die('Could not connect:' . mysql_error());
}
mysql_select_db($db, $con);
?>
When run this file its print Before text but not print After text.
Currently you can use the following code:
ini_set("error_reporting", E_ALL & ~E_DEPRECATED);
Using this you can get either deprecated or not.
FYI: The mysql_* functions have been removed in PHP7.x. There are two modules you can use.
The first is MySQLi, just use the code as follows:
<?php
$con = mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
You can also use PDO using code :
<?php
$servername = "localhost";
$username = "username";
$password = "password";
try {
$conn = new PDO("mysql:host=$servername;dbname=myDB", $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();
}
?>

PDO MySQL connection troubleshooting

I'm running the following very basic PDO:MySql connection:
<?php
$host = '127.0.0.1:8888';
$db = 'communities';
$user = 'root';
$pass = 'yY5MF)q/DCzc';
// Create connection
try {
$conn = new PDO('mysqli:host=$host;dbname=$db', $user, $pass);
foreach($conn->query('SELECT * from city_detail') as $row) {
print_r($row);
}
$conn = null;
// Check connection
} catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
echo "Connected successfully";
?>
I've modified php.ini to allow for extension=php_pdo_mysql.dll with no effect. Is there something else that I'm missing. I am using PHP7.0.3. The error message output is Error!: could not find driver Kudos!
You need to use double quotes to get variable data inside string:
$conn = new PDO("mysqli:host=$host;dbname=$db", $user, $pass);
The problem was using msqli instead of mysql.

Php connection with SQL Server 2012

I'm trying to make a connection to sql server 2012 with php, but always shows
Erro: SQLSTATE[IMSSP]: An invalid keyword 'Databases' was specified in the DSN string.
Can someone help me with this?
here is the code that i use to make the connection
<?php
session_start();
$servername = 'SERVERNAME';
$username = 'sa';
$password = '12345678';
$dbname = 'DBNAME';
//connection
try {
$conn = new PDO("sqlsrv:server=$servername;database=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$conn->setAttribute(PDO::SQLSRV_ATTR_DIRECT_QUERY , true);
} catch(PDOException $e) {
echo "Erro: " . $e->getMessage();
}
$conn=null;
?>
I haven't used SQL server 2012 but try changing
$conn = new PDO("sqlsrv:server=$servername;database=$dbname", $username, $password);
To
$conn = new PDO("mssql:host=$servername;dbname=$dbname", $username, $password);

change Mysqli to sqlsrv

I am new to PHP and need to modify some code in order to compile with my Microsoft SQL Server. The original code is like this. I downloaded it from usercake
<?php
/*
UserCake Version: 2.0.2
http://usercake.com
*/
//Database Information
$db_host = "localhost"; //Host address (most likely localhost)
$db_name = "202"; //Name of Database
$db_user = "202"; //Name of database user
$db_pass = "password"; //Password for database user
$db_table_prefix = "uc_";
GLOBAL $errors;
GLOBAL $successes;
$errors = array();
$successes = array();
/* Create a new mysqli object with database connection parameters */
$mysqli = new mysqli($db_host, $db_user, $db_pass, $db_name);
GLOBAL $mysqli;
if(mysqli_connect_errno()) {
echo "Connection Failed: " . mysqli_connect_errno();
exit();
}
//Direct to install directory, if it exists
if(is_dir("install/"))
{
header("Location: install/");
die();
}
?>
I already installed sqlsrv and tested the link. It works with my database. Then I changed the code to this:
<?php
//Database Information
$server = "servername";
$connectionInfo = array("Database"=>"databasename","UID"=>"xxxxxx", "PWD"=>"xxxxxx" );
$db_table_prefix = "uc_";
GLOBAL $errors;
GLOBAL $successes;
$errors = array();
$successes = array();
/* Create a new sqlsrv object with database connection parameters */
$mssqlsrv = new sqlsrv($server, $connectionInfo);
GLOBAL $mssqlsrv;
if(sqlsrv_connect_errno()) {
echo "Connection Failed: " . sqlsrv_connect_errno();
exit();
}
//Direct to install directory, if it exists
if(is_dir("install/"))
{
header("Location: install/");
die();
}
?>
I get the following error message:
Fatal error: Class 'mssql' not found in
I think this line is the problem:
$mssqlsrv = new sqlsrv($server, $connectionInfo);
But I do not know how to fix this.
I would use PDO in this case: http://www.php.net/manual/en/pdo.construct.php
You can create a DSN connection to SQL Server
$dsn = "sqlsrv:Server=servername;Database=databasename"
$user = 'dbuser';
$password = 'dbpass';
try {
$dbh = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
The information to connect to SQL server is available here: http://www.php.net/manual/en/ref.pdo-sqlsrv.connection.php

Categories