this post has been edited to reflect the findings thus far between myself and iamkrillin, as we have been the only two posters
I have the following VB.NET code connecting correctly, running from my PC
Dim strConnection As String = "Server=dev.xxxxx.vmc;Database=report1;integrated security=SSPI;" & _
"persist security info=False;Trusted_Connection=Yes;"
Dim ObjDa As SqlDataAdapter = New SqlDataAdapter(pStrQuery, strConnection)
Try
Dim dsReturn As DataSet = New DataSet
ObjDa.Fill(dsReturn)
ObjDa.Dispose()
Return dsReturn
Catch ex As Exception
Return Nothing
End Try
I have the following PHP code running from our iSeries
$conn = array( 'host' => 'dev.xxxxx.vmc',
'username' => 'vmc\adam',
'password' => 'xxxxxx)',
'dbname' => 'report1',
'pdoType' => 'dblib' );
try {
$db = new Zend_Db_Adapter_Pdo_Mssql($conn);
$db->getConnection();
} catch (Zend_Db_Adapter_Exception $e) {
}
The getConnection function, is throwing an error:
SQLSTATE[] (null) (severity 0)
And when I look up this error HERE, it appears to be a bug PRE 5.2.10, and we are running 5.2.17. But, some of the other comments say it is still a bug in 5.3.
*edit
It seems that if using a domain account, windows auth must be enabled. However, it is not through our PHP. So I need to set up a database specific user for our PHP connection.
In your VB snippet, you are connecting to SQL Server, and in your PHP snippet you are connecting to MySQL. If you need to use SQL Server from PHP, look at this. If you are on a non windows platform you can try FreeTDS. Here is an example of how to get started with it
Related
I have a script that creates 2 connections to the database using PDO.
The first connection is made to a SQL Server and the second one is to MySQL.
The script was working fine until yesterday. I can't think of anything that could have changed. but the script now is failing
This is the exception that I am getting when trying to connect to SQL Server
SQLSTATE[IMSSP]: Invalid value 1 specified for option PDO::SQLSRV_ATTR_QUERY_TIMEOUT.
I am running PHP 6.6 on Apache 2.4.12
I reviewed the script and the connection should be working with no issues.
This is my connection string
$connString = 'sqlsrv:Server=MyIP,1433;Database=MyDBname';
$pdo_opt[PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION;
$pdo_opt[PDO::ATTR_DEFAULT_FETCH_MODE] = PDO::FETCH_ASSOC;
$pdo_opt[PDO::MYSQL_ATTR_INIT_COMMAND] = 'SET NAMES utf8';
$pdo_opt[PDO::MYSQL_ATTR_LOCAL_INFILE] = true;
$pdo_opt[PDO::SQLSRV_ATTR_ENCODING] = PDO::SQLSRV_ENCODING_UTF8;
try {
$pdo = new PDO($connString, $username, $password, $pdo_opt);
} catch(Exception $e){
exit($e->getMessage());
}
How can I correct this issue? or where should I start to investigate this issue?
I figured it out. I fixed it by adding this line of code
$pdo_opt[PDO::SQLSRV_ATTR_QUERY_TIMEOUT] = 30;
I am using mysql. And i want to connect with database using aura sql.
<?php
$connection_factory = include '/Aura/scripts/instance.php';
$connection = $connection_factory->newInstance(
// adapter name
'mysql',
// DSN elements for PDO; this can also be
// an array of key-value pairs
'host=localhost;dbname=db_aura',
// username for the connection
'root',
// password for the connection
''
);
$result = $connection->fetchAll('SELECT * FROM tbl_test');
?>
The above code shows the error
Parse error: parse error in C:\wamp\www\1\Aura\src\Aura\Sql\Connection\AbstractConnection.php on line 65
Finally I got it...
The code given above in my question is correct. Error occured due the version of my php i used. PHP version above 5.4 only support this. Now am using php 5.4.12 and aura sql is able to connect with mysql and working well.. Thanks.
I am trying to connect to a mongo replica set using PHP,
my code looks like this:
$options = array(
'replicaSet' => 'Repset',
'readPreference' => 'primaryPreferred',
);
$connection = new MongoClient("mongodb://ip-10-1-2-3.ec2.internal:27017,ip-10-1-2-4.ec2.internal:27017/", $options);
When I do this I get an error back:
No candidate servers found
if I simplify the connect string to :
$connection = new MongoClient("mongodb://ip-10-1-2-4.ec2.internal:27017/");
Then it connects to the server, but this is not the right way, given I am using replica Sets
What am I doing wrong, the connect script seems right but obviously isn't.
Would appreciate any help.
Have you tried to delete space before $options?
I am working on my first ever .php application! All I am doing is posting some data to a database: simple stuff.
Right now, everything works fine, I can send my data to the DB no problems. However, I want to return an error message to my Objective-c application. I have the try and catch statements to make sure I connect to the DB, and if there is an error I would like to communicate that back to my application.
Here is some of what I have so far:
$dsn='mysql:dbname=******;host=******;';
$user='******';
$pass='******';
try {
/* obtain a database connection handle */
$pdo = new PDO($dsn, $user, $pass);
}
catch (PDOException $exception) {
echo("Failed to connect to the database. Error: " . $exception->getMessage());
}
Also, is it possible to set a limit for how long the PDO will attempt to connect to the database?
Right now when I purposely put in something invalid, it tries to connect for a long time.
If you're trying to do what I think you are, it doesn't work like that.
If you want to pass the error message to an Objective-C application, you will need to pipe, write contents to a file, or write the error to STDERR and capture it from the Objective-C application.
What exactly are you trying to accomplish? Calling a PHP script from an Objective-C script?
According to the PDO documentation, you can pass an array of driver options to it:
PDO::__construct() ( string $dsn [, string $username [, string $password [, array $driver_options ]]] )
In this same page there is an example of some driver specific options, also i believe you can use the PDO::ATTR_TIMEOUT and/or one of these:
http://www.php.net/manual/es/pdo.constants.php
I have a written a script in PHP that reads from data from a MySQL database. I am new to PHP and I have been using the PDO library. I have been developing on a Windows machine using the WAMP Server 2 and all has been working well. However, when I uploaded my script to the LINUX server where it will be used I get the following error when I run my script.
Fatal error: Call to undefined function query()
This is the line where the error is occuring ...
foreach($dbconn->query($sql) as $row)
The variable $dbconn is first defined in my dblogin.php include file which I am listing below.
<?php
// Login info for the database
$db_hostname = 'localhost';
$db_database = 'MY_DATABASE_NAME';
$db_username = 'MY_DATABASE_USER';
$db_password = 'MY_DATABASE_PASSWORD';
$dsn = 'mysql:host=' . $db_hostname . ';dbname=' . $db_database . ';';
try
{
$dbconn = new PDO($dsn, $db_username, $db_password);
$dbconn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (PDOException $e)
{
echo 'Error connecting to database: ' . $e->getMessage();
}
?>
Inside the function where the error occurs I have the database connection defined as a superglobal as such ...
global $dbconn;
I am a bit confused as to what is happening since all worked well on my development machine. I was wondering if the PDO library was installed but from what I thought it was installed by default as part of PHP v5.
The script is running on an Ubuntu (5.0.51a-3ubuntu5.4) machine and the PHP version is 5.2.4. Thank you for any suggestions. I am really lost on this.
Always, but always, develop on the same platform as your production platform. Try your best to mirror the versions of server software (PHP, MySQL, etc). There will always be differences between installs, and especially in the way different OS platforms handle things. Use the old 'phpinfo()' script on each server to compare the differences, if any.
At any rate, even if both the Win and Linux platforms here have the exact same versions of everything, have you checked your configuration? In other words, is the MySQL host name in your DSN a local MySQL host (linked to your Win development platform), or the actual host the Ubuntu server uses? Or are they both "localhost" in either case? Double check this. Also, have you mirrored all data on both servers?
Do you know for a fact that the Ubuntu server has PDO? You essentially said you assume they are the same. Don't assume, verify.
As sobedai mentioned, look at the output of var_dump($dbconn), check that it is actually a PDO object, and go from there.
This is a typical PDO deployment for me:
// 'logger' is a custom error logging function with email alerts
try {
$dbh= new PDO(dsn);
if ( is_a($dbh, "PDO") ) {
$sql= 'SELECT field FROM table';
$stmt= $dbh->query($sql);
if ( is_a($stmt, "PDOStatement") ) {
// handle resultset
}
else {
// weirdness, log it
logger("Error with statement: {$sql}" .$dbh->errorCode());
}
}
else {
// some weirdness, log it
logger('Error making connection: '. $dbh->errorCode());
}
}
catch (PDOException $e) {
logger('PDOException caught in '.__FILE__.' : '. $e->getMessage());
}
catch (Exception $e) {
logger('General Exception caught in '.__FILE__.' : '. $e->getMessage());
}
Note I'm using PDO::errorCode in there, referencing the SQL-92 state codes.
Also, refer to the manual as much as possible.
Generally when this happens, the object you're trying to reference is not actually an instance of a class.
Since you've been developing on WAMP and testing on LAMP, include paths immediately come to mind.
Check the php includes path is correct on your LAMP stack.
Check that all the necessary libs are installed (just do a quick phpinfo() page or you can use php -i from the command line).
And last but not least - do:
echo var_dump($dbconn);
confirm that it is indeed the object you think it is.