I'm trying to work with PDO on my localhost. I'm running MAMP on OSX 10.7.4.
I've checked phpinfo(), and as far as I can see I should be fine.
I checked the php.ini to see that "extension=pdo_mysql.so" is in fact uncommented.
I read some were that I had to make the file PROJECTFOLDER/config/parameters.ini with the following content so i did, but with no luck. (Changed it to reflect my setup of cause)
database_driver = pdo_mysql
database_host = localhost
database_port =
database_name = databasename
database_user = msqlusername
database_password = mysqlpassword//if not make blank
mailer_transport = smtp
mailer_host = localhost
mailer_user =
mailer_password =
locale = en
secret = ThisTokenIsNotSoSecretChangeIt
Any ideas as to how I can get PDO up and running?
BTW I'm using the following code to make the connection:
try {
$host = 'localhost';
$dbname = 'ifjernsyn';
$user = 'root';
$pass = 'root';
# MS SQL Server and Sybase with PDO_DBLIB
$DBH = new PDO("mssql:host=$host;dbname=$dbname, $user, $pass");
$DBH = new PDO("sybase:host=$host;dbname=$dbname, $user, $pass");
# MySQL with PDO_MYSQL
$DBH = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
# SQLite Database
$DBH = new PDO("sqlite:my/database/path/database.db");
}
catch(PDOException $e) {
echo $e->getMessage();
}
So there is not mssql and sybase 's driver.
You need PDO_DBLIB to access Microsoft SQL Server and Sybase databases.
Related
I can't connect to my databases, whenever i try it's giving me this error
I tried to connect to the default databases like mysql, and it worked fine just like the pic shows
I'm using wamp server the latest version, here's code if needed :
$servername = "localhost";
$username = "root";
$password = "";
$my_db="mydb";
$link=mysqli_connect($servername, $username, $password, $my_db);
if (mysqli_connect_error()) {
die("there is an error");
} else {
echo "connected to ".$my_db;
}
Wampserver 3.2.0 new instalation or upgrading
This might help others
Probably xamp using mariaDB as default too.
Wamp server comes with mariaDB and mysql, and instaling mariaDB as default on 3306 port.
To make mysql work!
On instalation it asks to use mariaDB or MySql, mariaDB is checked as default and you cant change it, check mysql option and install.
when instalation done both will be runing mariaDB on default port and mysql on another port.
Right click on wamp icon where its runing should be on right bottom corner, goto tools and see your mysql runing port.
And include in your database connection same as folowng :
$host = 'localhost';
$db = 'test';
$user = 'root';
$pass = '';
$charset = 'utf8mb4';
$port = '3308';
$dsn = "mysql:host=$host;dbname=$db;port=$port;charset=$charset";
$options = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
];
try {
$pdo = new PDO($dsn, $user, $pass, $options);
} catch (\PDOException $e) {
throw new \PDOException($e->getMessage(), (int)$e->getCode());
}
Note : I am using pdo.
See here for more : https://sourceforge.net/projects/wampserver/
When trying to connect to my mssql database, I get the error
"SQLSTATE[01002] Adaptive Server connection failed (severity 9)"
Below is the php code I'm running
<?php
try {
$hostname = "hostname.database.windows.net";
$port = 1433;
$dbname = "database-dev";
$username = "dbuser";
$pw = "dbpassword";
$dbh = new PDO ("dblib:host=$hostname:$port;dbname=$dbname","$username","$pw");
} catch (PDOException $e) {
echo "Failed to get DB handle: " . $e->getMessage() . "\n";
exit;
}
$stmt = $dbh->prepare("select name from master..sysdatabases where name = db_name()");
$stmt->execute();
while ($row = $stmt->fetch()) {
print_r($row);
}
unset($dbh); unset($stmt);
?>
And below is my odbc.ini, odbcinst.ini and freetds.conf, you can see my phpinfo() here ("http://wingedw.com/matiks/connect.php") the driver is set to freetds and the pdo and pdo_dlib modules have been added to php 5, any clues as to why im getting error, im sure the credentials are right.
odbc.ini
[MSSQLServer]
Driver = FreeTDS
Description = Any description
Trace = No
Server = servername
Port = 1433
Database = dbname
wTDS_Verison = 7.1
odbcinst.ini
[FreeTDS]
Driver = /usr/lib/i386-linux-gnu/odbc/libtdsodbc.so
Setup = /usr/lib/i386-linux-gnu/odbc/libtdsS.so
UsageCount = 1
freetds.conf
[global]
# TDS protocol version
; tds version = 7.1
# Whether to write a TDSDUMP file for diagnostic purposes
# (setting this to /tmp is insecure on a multi-user system)
; dump file = /tmp/freetds.log
; debug flags = 0xffff
# Command and connection timeouts
; timeout = 100
; connect timeout = 100
# If you get out-of-memory errors, it may mean that your client
# is trying to allocate a huge buffer for a TEXT field.
# Try setting 'text size' to a more reasonable limit
text size = 64512
# A typical Sybase server
[egServer50]
host = symachine.domain.com
port = 5000
tds version = 7.1
# A typical Microsoft server
[MSSQLServer]
host = servername
port = 1433
tds version = 7.1
Turns out everything was configured correctly, the issue was using dblib over odbc, see code below.
try {
$hostname = "hostname.database.windows.net";
$port = 1433;
$dbName = "databasename";
$dbuser = "dbuser#hostname";
$dbpass = "password";
$dbh = new PDO('odbc:DRIVER=FreeTDS;SERVERNAME=mssql;DATABASE=' . $dbName,
$dbuser, $dbpass);
//echo "COnnected";
} catch (PDOException $e) {
echo "Failed to get DB handle: " . $e->getMessage() . "\n";
exit;
}
2021 - BACK TO THE FUTURE:
I know that this post is old, but my reply can point a desperate soul in the right direction:
You are using odbc and the command "isql" works just fine
You are using FreeTDS and the command "tsql" works just fine
Trouble is running the php scripts that have calls to odbc via FreeTDS -> keeps giving errors
Kindly read:
https://www.linuxquestions.org/questions/blog/tix-592494/freettds-libiodbc-iodbc-php-7-4-21-on-slackware-current-5-13-5-38621/
Cheers happy souls!
I am fairly new to using PHP. I downloaded XAMPP, and installed everything. PHP 5.5.27 is the version. I ran a test php program which was jsut echo "Hello World". It worked fine. I also was able to connect to MYSQL database using PHP.
$link = mysqli_connect("localhost", "u/n", "pass", "databasename";
Problem i am having and need help is with connecting to sql server. How do i do that? I saw an example online and tried it:
$serverName = "servername";
$connectionInfo = array("Database"="name", "UID"=>"U/N", "PWD"=>"pass";>
$conn = sqlsrv_connect($serverName, $connectionInfo);
But everytime i run this it tells me:
Call to undefined function sqlsrv_connect()
Can someone help me understand what is going on?
Consider using PHP's Data Objects (PDO) to connect to SQL Server (in fact you can use it to connect to MySQL or any other database).
Using the MSSQL sqlsrv API (various dlls must be set):
<?php
$server = 'servername';
$database = 'databasename';
$username = 'username';
$password = 'pass';
try {
$conn = new PDO("sqlsrv:Server=$server;Database=$database",
$user, $password);
}
catch(PDOException $e) {
echo $e->getMessage()."\n";
exit;
}
?>
Using the ODBC Driver or DSN API (requiring MSSQL ODBC Driver installed which usually ships with database or Windows in general):
<?php
$server = 'servername';
$database = 'databasename';
$username = 'username';
$password = 'pass';
try {
$dbh = new PDO("odbc:Driver={SQL Server};Server=$server;
database=$database",$username,$password);
}
catch(PDOException $e) {
echo $e->getMessage()."\n";
exit;
}
?>
i have problem connecting to azure on my mac. I've read many articles here and added multiple extension to my php.ini file but nothing seems to work. I just want to simply run this code and connect to database. All the variables in the code have the actual values are correct.
At the moment it gives me following error : "PDOException Object ( [message:protected] => could not find driver".
i've looked throuh mutiple articles on this issue, and added extensions to php.ini. i've pasted all of the below for people to check. i also now that i have PDO attached to my server. Unforunately, i cant post the screemshot here, but my pdo_mysql, pdo_pgsql, pdo_sqlite in the phpinfo() call.
i would really appreciate any info and help on this matter. thank you!
$server = "tcp:*********.database.windows.net,1433";
$user = "jus***#********";
$pwd = "password";
$db = "testdb";
try
{
$conn = new PDO( "sqlsrv:Server= $server ; Database = $db ", $user, $pwd);
$conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
}
catch(Exception $e)
{
die(print_r($e));
}
;Extensions
;extension=apcu.so
extension=imap.so
extension=yaz.so
extension=mcrypt.so
extension=gettext.so
extension=pgsql.so
extension=pdo_pgsql.so
extension=pdo_mysql.so
extension=php_pdo.dll
extension=php_pdo_mysql.dll
extension=php_pdo.dll
extension=php_pdo_firebird.dll
extension=php_pdo_informix.dll
extension=php_pdo_mssql.dll
extension=php_pdo_mysql.dll
extension=php_pdo_oci.dll
extension=php_pdo_oci8.dll
extension=php_pdo_odbc.dll
extension=php_pdo_pgsql.dll
extension=php_pdo_sqlite.dll
;extension=imagick.so
In addition to the extensions that you have enabled in php.ini you will need to install:
Microsoft Drivers 3.0 for PHP for SQL Server
You have to also make sure you are not using the wrong php.ini
Avoid these spaces in your connection string, and if you anything you should catch PDOException.
$server = "tcp:*********.database.windows.net,1433";
$user = "jus***#********";
$pwd = "password";
$db = "testdb";
$dsn = "sqlsrv:Server=$server;Database=$db";
try {
$conn = new PDO($dsn, $user, $pwd );
$conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
} catch (PDOException $e) {
die(print_r($e));
}
i'm having issues connecting to my database on a web host that I have, i'm using the following:
$dsn = 'mysql:host=mysql1.hosting.digiweb.ie;dbname=mydbname';
$user = 'myusername';
$password = 'mypassword';
According to the website: Host Name mysql1.hosting.digiweb.ie (ip address)
as the title says i'm getting a could not find driver error, am i entering the host incorrectly i tried entering whats above and also the ip address - Thanks!
Edit:
Here's all my code
<?php
$dsn = 'mysql:host=localhost;dbname=';
$user = '';
$password = '';
try {
// Connect and create the PDO object
$dbh = new PDO($dsn, $user, $password);
$dbh->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e) {
echo 'Database connection failed - ';
echo $e->getMessage();
exit;
}
echo 'works';
?>
go to your php.ini file and uncomment this line
extension=php_pdo_mysql.dll
and then restart your apache
Change your extension dir to be absolute in php.ini. I changed it from
extension_dir = "ext"
to
extension_dir = "C:/{PATH TO PHP DIRECTORY}/ext"
and it worked.