I get an error when I use this code to list rows of a table
// Specify your table name
$table_name = 'table_stats';
$hostname = '172.16.11.2';
$username = 'user_stats';
$password = 'stats';
$dbname = 'db_stats';
$dbh = new PDO("mysql:$hostname;dbname=$dbname",$username,$password);
$stmt = $dbh->query("SELECT * FROM $table_name", PDO::FETCH_ASSOC);
foreach($stmt as $row) { // PDOStatement implement Traversable interface, so you can just loop over the resultset. Sweet!
print_r($row);
}
?>
and I get this error
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)' in /home/mahmedne/public_html/android/updates/test.php:12 Stack trace: #0 /home/mahmedne/public_html/android/updates/test.php(12): PDO->__construct('mysql:172.16.25...', 'mahmedne_stats', 'stats') #1 {main} thrown in /home/mahmedne/public_html/android/updates/test.php on line 12
I can confirm that database settings are 100% correct , using same values if I connect like
if (!mysql_connect($db_host, $db_user, $db_pwd))
die("Can't connect to database");
Then it works but not with I do new PDO()
Please advise, is it possible that my is providing limited functionality of PHP ?
I am checking this code on web host and not on my local machine
here is th PDO related info that i got using phpinfo()
PDO
PDO support enabled
PDO drivers sqlite, sqlite2, mysql
pdo_mysql
PDO Driver for MySQL enabled
Client API version 5.1.65
Directive Local Value Master Value
pdo_mysql.default_socket /var/lib/mysql/mysql.sock /var/lib/mysql/mysql.sock
pdo_sqlite
PDO Driver for SQLite 3.x enabled
SQLite Library 3.7.7.1
Thanks,
You're missing host= from your DSN string, it should be like this,
new PDO("mysql:host=$hostname;dbname=$dbname",$username,$password);
Related
The script below runs on a Centos server and is trying to connect to a MySQL database on another server which requires SSL parameters. The credentials used in the script work fine using and Microsoft Access DSN connection.
<?php
ini_set ('error_reporting', E_ALL);
ini_set ('display_errors', '1');
error_reporting (E_ALL|E_STRICT);
$pdo = new PDO('mysql:host=99.99.199.199;dbname=dummy1', 'user1', 'pwd1',
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'
));
$statement = $pdo->query("SHOW TABLES;");
$row = $statement->fetch(PDO::FETCH_ASSOC);
echo htmlentities($row['_message']);
?>
The code above gives SSL operation failed with code 1 - here is the full message:
Fatal error: Uncaught PDOException: PDO::__construct(): SSL operation
failed with code 1. OpenSSL Error messages: error:14090086:SSL
routines:ssl3_get_server_certificate:certificate verify failed in
/var/www/vhosts/zzzzz.org/httpdocs/zzodbc/dgodbc1.php:10
Stack trace: #0 /var/www/vhosts/zzzzz.org/httpdocs/zzodbc/dgodbc1.php(10): PDO->__construct('mysql:host=99.9...', 'odbc_guil...', 'pwd1',
Array) #1 {main} Next PDOException: SQLSTATE[HY000] [2002] in
/var/www/vhosts/zzzzz.org/httpdocs/zzodbc/dgodbc1.php:10 Stack trace:
#0 /var/www/vhosts/zzzzz.org/httpdocs/zzodbc/dgodbc1.php(10): PDO->__construct('mysql:host=99.9...', 'odbc_guil...', 'pwd1',
Array) #1 {main} thrown in
/var/www/vhosts/zzzzz.org/httpdocs/zzodbc/dgodbc1.php on line 10
I have verified that the credentials, including the SSL parameters with a DSN connection. I have checked that the SSL Keys are correctly located in the /etc/mysql/ssl directory.
Any help to suggest what I'm doing wrong would be good. Thanks.
I may have been going at this in the wrong way....
Since these keys work with ODBC then I think I should be using using odbc_connect and sending the same string as I use with MS access such as
$user = "user";
$pass = "pwd";
$connection = "Driver={MySQL ODBC 5.1 Driver};Server=46.51.178.163;Database=db1;sslca=/etc/mysql/ssl/c1.pem;sslkey=/etc/mysql/ssl/ck.pem;sslcapath=/etc/mysql/ssl/;sslcert=/etc/mysql/ssl/cc.pem";
$con = odbc_connect($connection, $user, $pass);
But to get this to work I need to install a MySQL connector on the server which I'm grappling with at the moment.
I have solved this problem -thanks for all who have helped. This is what I have learned:
SSL keys are connection type specific - so I had keys that worked with ODBC and it was wrong to expect them to work with PDO
ODBC drivers ( php extensions ) need to be installed on the server - they aren't automatically present. Here is an excellent video showing how to do this.
You need command line access to the server to install the driver ( and also to upload the SSL keys to a secure location ) - they are in /etc/mysql/ssl.
I installed the driver in /usr/lib/odbc2/lib rather than in the long folder name in the video. I also installed the in the /usr tree because when I tried the locations in the video I got file not found errors. The two driver files are libmyodbc5a.so and libodbc5w.so. Only the ...5w.so file seems to be required.
Once these files are in place then you need to add an entry to odbcinst.ini in the /etc folder. I used nano so the command line nano odbcinst.ini brings up the file which had a model entry for PostgresSQL. If the server is 64 bit then these are the entries I made in odbcinst.ini:
[mysql537]
Driver64 = /usr/lib/odbc2/lib/libmyodbc5w.so
Setup64 = /usr/lib/odbc2/lib/libmyodbc5w.so
UsageCount = 1
You must have the ...64 paths otherwise the driver isn't found ( i.e Driver64 = NOT Driver= ). I made this mistake first off.
Provided the driver files are found at the paths in odbcinst.ini then things should work. (I thought I needed entries in odbc.ini but I now believe you only need something here if you are using a DSN).
the folder odbc2 was one I created inside /etc/lib which already exists. I did that to avoid any permission issues by creating a new folder.
Here is the code that works ( the connection string is exactly the same as the string used in a Microsoft Access connection ):
<?php
ini_set ('error_reporting', E_ALL);
ini_set ('display_errors', '1');
error_reporting (E_ALL|E_STRICT);
$user = "odbcmmm";
$pass = "999999999";
$connection = "Driver={mysql537};Server=99.99.199.199;Database=db_name;UID=odbc_db_name;PWD=password;sslca=/etc/mysql/ssl/c1.pem;sslkey=/etc/mysql/ssl/ck.pem;sslcapath=/etc/mysql/ssl/;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");
}
I hope this is useful.
I've installed the php5-odbc library.
Trying to connect to a .mdb file but keep getting error Data source name not found, and no default driver specified.
Code:
$dbName = "../../../var/Import/PartsPlaceDB.mdb";
if (!file_exists($dbName)) {
die("Could not find database file.");
}
$db = new PDO("odbc:DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=$dbName; Uid=; Pwd=;");
Outputs:
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[IM002] SQLDriverConnect: 0 [unixODBC][Driver Manager]Data source name not found, and no default driver specified' in [...]
PDO Drivers:
foreach(PDO::getAvailableDrivers() as $driver)
{
echo $driver.'<br />';
}
Output:
mysql
odbc
sqlite
The problem is recurrent with 64-bit version. It looks like your code only work on Windows 32-bit.
To solve the problem, you might install this tool: Microsoft Access Database Engine 2010 Redistributable. I had the same problem for months but this tool solved mine.
I guess you're doing the location wrong?
Instead of this
$dbName = "../../../var/Import/PartsPlaceDB.mdb";
Why not do it like this
$dbName = "..\..\..\var\Import\PartsPlaceDB.mdb";
I am trying to access a MDB file using PHP on a CentOS machine, preferably using PDO. I have installed the ODBC and pdo_odbc modules in PHP and restarted the the web server (Apache). I also installed mdbtools and unixODBC.
print_r(PDO::getAvailableDrivers());
Shows:
Array
(
[0] => mysql
[1] => odbc
[2] => pgsql
[3] => sqlite
)
I try to access the DBA file (which is saved locally) with this code:
try
{
$driver = "/usr/lib64/libmdbodbc.so.0";
$dbName = "/PATH/FILE.MDB";
if (!file_exists($dbName)) {
die("Could not find database file.");
}
$db = new PDO("odbc:DRIVER=$driver; DBQ=$dbName; Uid=; Pwd=;");
$sql = "SHOW TABLES";
$result = $db->query($sql);
$row = $result->fetch();
var_dump($row);
}
catch(Exception $e)
{
echo "\n\nEXCEPTION: $e\n\n";
}
And this outputs:
EXCEPTION: exception 'PDOException' with message 'SQLSTATE[IM002] SQLDriverConnect: 0 [unixODBC][Driver Manager]Data source name not found, and no default driver specified' in /PATH/odbctest.php:14
Stack trace:
#0 /PATH/odbctest.php(14): PDO->__construct('odbc:DRIVER=/us...')
#1 {main}
What do I need to change? What should I use as a driver name? I have been looking all over the place and all I find is a lot of really old stuff from the 2004 era, but a lot of the links have disappeared over the years. This is the most relevant StackOverflow question, but does not include any code: PDO Microsoft Access. I really only need to read from the file.
EDIT:
With some tinkering it seems that I now at least have PDO happy with the driver by correcting the odbcinst.ini file which now contains the following:
[MDBTools]
Description = MDBTools Driver
Driver64 = /usr/lib64/libmdbodbc.so.0
Setup = /usr/lib64/libmdbodbc.so.0
FileUsage = 1
UsageCount = 1
Unfortunately, I am still getting an error:
exception 'PDOException' with message 'SQLSTATE[08001] SQLDriverConnect: 1 [unixODBC]Could not find DSN in connect string' in /PATH/odbctest.php:15
Stack trace:
#0 /PATH/odbctest.php(15): PDO->__construct('odbc:Driver=MDB...')
#1 {main}
I am making the PDO call as follows:
$driver = "MDBTools";
$dbName = "/PATH/DATABASE.MDB";
$db = new PDO("odbc:Driver=$driver; DBQ=$dbName; Uid=; Pwd=;");
I tried adding the data source to the odbc.ini file even though I can't do this in the production version and am still getting the same error. I did find this which is interesting: DSN-less connection with PHP ODBC using MDBTools Driver. Any suggestions?
$driver = "MDBTools";
$dbName = "/PATH/DATABASE.MDB";
$db = new PDO("odbc:Driver=$driver;DBQ=$dbName", "", "");
When I run the following PHP code:
<?php
$username = "root";
$password = "allen123";
$conn = new PDO('mysql:host=localhost;dbname=test', $username, $password);
?>
I get the following error.
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [2002] No >such file or directory' in /Users/idrisk/Colourity/site/index.php:4 Stack trace: #0 >/Users/idrisk/Colourity/site/index.php(4): PDO->__construct('mysql:host=loca...', 'root', >'allen123') #1 {main} thrown in /Users/idrisk/Colourity/site/index.php on line 4
Any ideas? I downloaded the PDO drivers via Macports by the way
EDIT
To use mysqlnd with a local MySQL server, edit /opt/local/etc/php55/php.ini and set mysql.default_socket, mysqli.default_socket and pdo_mysql.default_socket to the path
to your MySQL server's socket file.
For mysql5, use /opt/local/var/run/mysql5/mysqld.sock
For mysql51, use /opt/local/var/run/mysql51/mysqld.sock
For mysql55, use /opt/local/var/run/mysql55/mysqld.sock
For mysql56, use /opt/local/var/run/mysql56/mysqld.sock
For mariadb, use /opt/local/var/run/mariadb/mysqld.sock
For percona, use /opt/local/var/run/percona/mysqld.sock
---> Cleaning php55-mysql
---> Updating database of binaries: 100.0%
---> Scanning binaries for linking errors: 100.0%
---> No broken files found.
It looks like you have an invalid socket in your php.ini file. Change the pdo_mysql.default_socket setting to an empty string to use the default socket.
So I just finally installed the PDO drivers, and now I have another issue. When I try to use
<?php
$username = "root";
$password = "allen123";
$conn = new PDO('mysql:host=localhost;dbname=test', $username, $password);
?>
Just to connect to the DB, I get the following error
Fatal error: Uncaught exception 'PDOException' with message
'SQLSTATE[HY000] [2002] No such file or directory' in
/Users/idrisk/Colourity/site/index.php:4 Stack trace: #0
/Users/idrisk/Colourity/site/index.php(4):
PDO->__construct('mysql:host=loca...', 'root', 'allen123') #1 {main}
thrown in /Users/idrisk/Colourity/site/index.php on line 4
I've looked at other posts on SO about this issue, but they all seem different. Any ideas?
EDIT
So I installed the drivers via Macports and this is what I get once It was installed
To use mysqlnd with a local MySQL server, edit /opt/local/etc/php55/php.ini and set mysql.default_socket, mysqli.default_socket and pdo_mysql.default_socket to the path
to your MySQL server's socket file.
For mysql51, use /opt/local/var/run/mysql51/mysqld.sock
For mysql55, use /opt/local/var/run/mysql55/mysqld.sock
For mysql56, use /opt/local/var/run/mysql56/mysqld.sock
For mariadb, use /opt/local/var/run/mariadb/mysqld.sock
For percona, use /opt/local/var/run/percona/mysqld.sock`
The other answers say to use something else.