PDOException: "The auto-commit mode cannot be changed for this driver" - php

I'm trying to get PDO connecting to an SQL server to enter READ UNCOMMITTED, according to various sources (https://msdn.microsoft.com/en-us/library/cc296183(v=sql.105).aspx) this is how you do it.
$pdo = new PDO ("sqlsrv:server=$hostname;database=$dbname",$username,$pw,[PDO::SQLSRV_TXN_READ_UNCOMMITTED]);
This results in a PDOException: "The auto-commit mode cannot be changed for this driver"

When you're using PDO, you have to use this form:
$conn = new PDO("sqlsrv:Server=".$hostname.
";Database=".$database.
";TransactionIsolation=".PDO::SQLSRV_TXN_READ_UNCOMMITTED,
$username, $pw);
https://msdn.microsoft.com/en-us/library/ff628167.aspx

Related

Connect PHP to Ms SQL

I have completed all the steps to make a connection between PHP and Ms SQL but still its not working and showing an error of "Fatal error: Uncaught Error: Call to undefined function sqlsrv_connect()". I have installed MS SQL Driver also. the extension i am using in php.ini is php_pdo_sqlsrv_53_ts_vc9.dll right now but it is not included in xampp extensions. TCP/ IP is also enabled already.
You could use PDO statement to connect
An example bellow:
# Connecting to PostGreSQL
$dbh = new PDO("msql:dbname=$dbname; host=$host", $username, $password);
# Setting an attribute on the database handle and error reporting
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
# Prepares a statement for execution and returns a statement object
$query = 'SELECT * FROM test';
$stmt = $dbh->prepare($query);
$stmt->execute();
# Set the resulting array to associative
$result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
# Returns an array containing all the results from the query
$allRows = $stmt->fetchAll();

Unable to connect to MySQL through PHP, mysql works, but not using mysqli or PDO

In my connect script this works, but will be deprecated.
$dbLink = mysql_connect($dbHostname, $dbUsername, $dbPassword) or die("Unable to connect to MySQL");
$dbselected = mysql_select_db($dbDatabase,$dbLink) or die("Could not select database");
If I change it to use either a PDO or mysqli it fails with
"No database selected"
on my test pc, or
"Access denied for user 'xxxxxx'#'localhost' (using password: NO)"
on my live site.
$dbLink = mysqli_connect($dbHostname, $dbUsername, $dbPassword, $dbDatabase);
Read and tested the similar question with the ini_get("mysqli.default_port").. The new commands connect ok, but then nothing completes.
My PDO code is
$dbLink = new PDO('mysql:host=localhost;dbname=mass', $dbUsername, $dbPassword);
I'd say your main problem is relying on or die() to handle error conditions. This won't work as neither the PDO nor mysqli constructor will return a falsey value.
Instead, you should use the appropriate level of error handling.
Development environment
While developing, always work with the following properties in your php.ini file
display_errors = On
error_reporting = E_ALL
You may need to restart your web server (if you're using one) after making changes to this file.
mysqli
Check the connect_errno property for a connection error state
$mysqli = new mysqli('localhost', 'username', 'password', 'dbname');
if ($mysqli->connect_errno) {
throw new Exception($mysqli->connect_error, $mysqli->connect_errno);
}
$mysqli->set_charset('utf8');
PDO
The PDO constructor should throw an exception if it can't connect. Setting the error mode to exception means that any further errors will also throw an exception.
$pdo = new PDO('mysql:host=localhost;dbname=dbname;charset=utf8', 'username', 'password', [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
]);
I wouldn't bother with any try {...} catch statements while you're developing, at least not in this initial stage as you're likely to end up hiding any serious errors.

PDO doesn't seem to create MySQL database

I'm trying to create a database using the following code:
// Set up DB connection and creates the DB
try {
$connection = new \PDO(
'mysql:host='.Settings\Database::$host.';',
Settings\Database::$username,
Settings\Database::$password);
$connection->exec("CREATE DATABASE IF NOT EXISTS ".Settings\Database::$databaseName." CHARACTER SET utf8 COLLATION utf8_unicode_ci;");
} catch (\PDOException $exception) {
die("Could not connect to database: ".$exception->getMessage());
}
The problem is that no database is being created and I receive no error except for the fact that when I try create a table with PDO i receive this error:
READ EDIT 2
Could not connect to database: SQLSTATE[HY000] [1049] Unknown database 'dbname'
Edit:
I have no problem manually creating the DB with phpMyAdmin and similars.
Edit 2:
Mistakenly I thought that the error was given by the CREATE TABLE... statement. Instead the error is returned by the die() function in the exception handling.
Your call to exec() isn't throwing exceptions. You have to enable that for each PDO connection with an attribute like this:
$connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
If you were getting the error message from exec(), you would have seen this:
Could not connect to database: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'COLLATION utf8_unicode_ci' at line 1
The syntax for CREATE DATABASE uses the keyword COLLATE, not COLLATION.
See http://dev.mysql.com/doc/refman/5.6/en/create-database.html

PDO: Can't connect to mysql when host is set in variable

i'm in trouble. I'm trying to connect remote mysql server with pdo (php 5.4.12). With this code it's connect normally:
$dbh=new PDO('mysql:host=xxx.xxx.xxx.xxx;dbname=newdb', $user, $pass);
But, if i try set host with variable, like this:
$host='xxx.xxx.xxx.xxx';
$dbh=new PDO('mysql:host='.$host.';dbname=newdb', $user, $pass);
It's just thinks sometime, and tell me:
Fatal error: Uncaught exception 'PDOException' with message ' in
D:\wamp\www\test.php on line 21
21 it's line of creating new PDO object.
Ok, i'm try to catch exception (sorry, it's new for me), and now i have this:
Error!: SQLSTATE[HY000] [2002]
Can you help me, please?
Double quoting not helps
$dbh = new PDO("mysql:host=$host;dbname=newdb", $user, $pass);
Nothing changed.
var_dump('mysql:host='.$host.';dbname=newdb');
string 'mysql:host=xx.xx.xxx.xxx;dbname=newdb' (length=37)
Way with {} not helped for me.
Ok, the situation becomes clear. I'm try to connect another server on another IP, and it's normally connected. Can it be mysql server security options?
Try this way
$host = "xxx.xxx.xxx.xxx";
$dbh = new PDO("mysql:host={$host};dbname=newdb", $user, $pass);
The double quotes and the braces to include the var in the string as you can see here
http://www.php.net/manual/en/language.operators.string.php

Can't connect through PHP to an Access Database

I have to establish a connection via PHP to a Access Database, but everytime I try I get this error:
Fatal error: Uncaught exception 'com_exception'
With message:
'Source: Microsoft OLE DB
Service ComponentsDescription: Das Format der Initialisierungszeichenfolge entspricht nicht
den OLE DB-Angaben.' in E:\path\to\phpfile.php:93
Stack trace: #0 E:\path\to\phpfile.php(93): com->Open('DRIVER={Microso...') #1 {main} thrown in
The German text means something like "the format of the initialization string is not common of the OLE DB statements.
My connection code looks like
$db = 'DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("\") & ("\\db\\db.mdb") & ";"';
$conn = new COM("ADODB.Connection");
$conn->Open($db);
I can't figure out what's wrong. In pure ASP it works fine, but not in PHP.
Thanks in advance
Instead of using the "ADODB.Connection" module use PDO since it's currently the best option out there and is a best practice, Here is the documentation: http://php.net/manual/en/book.pdo.php
But if PDO isn't for you then see ODBC (PDO RECOMMENDED) which is a native PHP module. Here is the documentation: http://php.net/manual/en/book.uodbc.php
To start a connection you would do this: $connection = odbc_connect("Driver={Microsoft Access Driver (*.mdb)};Dbq=$mdbFilename", $user, $password);
Use PDO instead. It can connect via ODBC:
$pathToMDB = "path/to/db.mdb";
$db = new PDO("odbc:Driver={Microsoft Access Driver (*.mdb)};Dbq=$pathToMDB", 'admin');

Categories