PDO: Database connection on remote website - php

This is an example of the database connection I'm using with my PDO queries:
$dsn = "mysql:host=localhost;dbname=some_db;charset=utf8";
$opt = array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
);
$pdo = new PDO($dsn,'Username','Password', $opt);
How would I modify it if my database is located on www.mysite.com and I want to access the database from a different website?

You need to open up port 3306 to accept connections.
$dsn = "mysql:host=mysite.com;dbname=some_db;charset=utf8";
$opt = array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
);
$pdo = new PDO($dsn,'Username','Password', $opt);

Related

Why does PDO execute() start a transaction?

This is a test snippet from my application:
<?php
$dsn = "mysql:host=localhost;dbname=".$dbName.";charset=utf8mb4";
$options =
[
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_PERSISTENT => false,
PDO::ATTR_EMULATE_PREPARES => false,
PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true,
PDO::ATTR_AUTOCOMMIT => false,
PDO::ATTR_ORACLE_NULLS => PDO::NULL_EMPTY_STRING
];
$conn = new PDO($dsn, $dbUser, $dbPass, $options);
var_dump($conn->inTransaction());
$sql = "SELECT
documentTypeID
FROM documentTypes";
$statement = $conn->prepare($sql);
var_dump($conn->inTransaction());
$result = $statement->execute();
var_dump($conn->inTransaction());
Output:
bool(false)
bool(false)
bool(true)
Why is the third var_dump true? I didn't intend to start a transaction. My understanding of transactions is that SELECT statements don't need or affect them. What am I missing?
After this happens in my app, I try to beginTransaction(), which fails because one has already started. Checking to see if inTransaction() is true before I begin one seems sloppy, but if that's how this is supposed to work, I guess I'll do it.

How to get php-native handle from Doctrine\DBAL\Connection?

Given the code
$connectionParams = array(
'dbname' => $this->dbname,
'user' => $this->dbuser,
'password' => $this->dbpass,
'host' => $this->dbhost,
'driver' => 'mysqli',
);
$conn = \Doctrine\DBAL\DriverManager::getConnection($connectionParams);
var_dump($conn);
How can I get the underlying mysqli handle from $conn (which is a Doctrine\DBAL\Connection)?
I have found *a way* to access it, but its obviously not the way it's supposed to be done, so I'm up for suggestions:
$conn = \Doctrine\DBAL\DriverManager::getConnection($connectionParams);
foreach((array)$conn->getWrappedConnection() as $mysqli){
// TODO: find official way of getting the handle.
// here we are casting it to (array) to access its PRIVATE PROPERTIES
// it's a fugly hack.
break;
}
var_dump($mysqli);
You can get it this way:
$mysqli = $conn->getWrappedConnection()->getWrappedResourceHandle();

how to create a remote mysql connection using php pdo [duplicate]

This is an example of the database connection I'm using with my PDO queries:
$dsn = "mysql:host=localhost;dbname=some_db;charset=utf8";
$opt = array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
);
$pdo = new PDO($dsn,'Username','Password', $opt);
How would I modify it if my database is located on www.mysite.com and I want to access the database from a different website?
You need to open up port 3306 to accept connections.
$dsn = "mysql:host=mysite.com;dbname=some_db;charset=utf8";
$opt = array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
);
$pdo = new PDO($dsn,'Username','Password', $opt);

PDO Insert error on execute

I can't find my mistake, I'm getting Execute fail error
$db = new PDO('mysql:host=localhost; dbname=xxxxxx', 'yyyyyy', 'zzzzzz', array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8")) or die ("fail");;
$query = "INSERT INTO multiTicker (mtgox,btcstamp,btce,btcchina,myDateTime) VALUES (:mtgox,:btcstamp,:btce,:btcchina,:myDateTime)";
$st = $db->prepare($query) or die ("Query fail");
$st->execute(array(':mtgox' => $mtgox,
':btcstamp' => $btcstamp,
':btce' => $btce,
':btcchina' => $btcchina,
':myDateTime' => $myDateTime)) or die ("Execute fail");
Looks like your DSN is incorrect (you have a space in it). Try this PDO constructor and stop using or die()!
$db = new PDO('mysql:host=localhost;dbname=xxxxxx;charset=utf8', 'yyyyyy', 'zzzzzz', array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_EMULATE_PREPARES => false,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC));
$query = "INSERT INTO multiTicker (mtgox,btcstamp,btce,btcchina,myDateTime) VALUES (:mtgox,:btcstamp,:btce,:btcchina,:myDateTime)";
$st = $db->prepare($query);
$st->execute(array(
':mtgox' => $mtgox,
':btcstamp' => $btcstamp,
':btce' => $btce,
':btcchina' => $btcchina,
':myDateTime' => $myDateTime
));
You don't have to set the default fetch mode to PDO::FETCH_ASSOC but I find it's handy.

Is it possible to use Doctrine with persistent PDO connections?

I'm trying to improve performance on a volkszaehler.org implementation by enabling persistent DB connections. Having hacked included Doctrine's Connection class to have PDO::ATTR_PERSISTENT => true, I'm getting the PDO error General error: PDO::ATTR_STATEMENT_CLASS cannot be used with persistent PDO instances"
Is there any way to fix this?
You could pass your own PDO instance to Doctrine, setting the persistent connection yourself:
$dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass, array(
PDO::ATTR_PERSISTENT => true
));
$config = new \Doctrine\DBAL\Configuration();
$connectionParams = array(
'dbname' => 'mydb',
'user' => 'user',
'password' => 'secret',
'host' => 'localhost',
'pdo' => $dbh,
);
$conn = \Doctrine\DBAL\DriverManager::getConnection($connectionParams, $config);
Be sure to know the implications of using persistent connections with PDO: What are the disadvantages of using persistent connection in PDO

Categories