Unable to connect the database
- ujjgar
- Member | 3
I am trying to connect to Microsoft SQL server DB from my web
application.
I have been using PDO SQLSRV driver in the past. But in my current project they
don't have that driver installed on the server, so using Nette now and am very
new to Nette as well.
I am using composer to install the package.
The composer has created many directories and i think it is causing some
problem. I will come back to that later.
I followed the tutorial and here is how i am creating DB connection:
function setDBConnToSandbox(){
$uid = "abc";
$pwd = "pwd";
$dbname = "Sandbox";
$serverName = "dwhouse";
//this line is throwing an error saying:
Fatal error: Uncaught Error: Class 'Connection' not found
$conn = new Nette\Database\Connection("sqlsrv:server=$serverName ; Database=$dbname", "$uid", "$pwd");
return $conn;
}
the reason think is because the package installed in different way than
expected.
The connection class has been installed by the composer at following
location:
/vendor/nette/database/src/Database/Connection.php
So if I change my code as below, i get another error:
include("../vendor/nette/database/src/Database/Connection.php");
function setDBConnToSandbox(){
$uid = "abc";
$pwd = "pwd";
$dbname = "Sandbox";
$serverName = "dwhouse";
//this line is throwing an error saying:
Fatal error: Trait 'Nette\SmartObject' not found
$conn = new Connection("sqlsrv:server=$serverName ; Database=$dbname", "$uid", "$pwd");
return $conn;
}
Again, the reason I believe the location of the SmartObject is different
than expected.
It is in /vendor/nette/utils/src/Utils/SmartObject.php
Basically I think Composer did not install the package in the right directory
structure.
It is more likely because I do not know how to use it properly as I am using composer for the first time as well.
Could anyone help me on this?
- David Matějka
- Moderator | 6445
you have to require composer autoloader:
require __DIR__ . '/../vendor/autoload.php';
- ujjgar
- Member | 3
Thanks David. I added the require
line and it works perfectly
fine on my local machine!
So, next I deployed it on a docker container. The build and deployment was successful however I got following runtime error, it is again about the driver.
To give you some idea, dbConnectToSandBox.php
is a file that has
a method setDBConnToSandbox()
and this is where i added the
require
line. this method returns the connection object. I am
using that object in fetchFromAdobe.php
file for querying the
database.
Is there anything extra I have to do for deploying it?
Error: PDOException: could not find driver in
/var/www/html/vendor/nette/database/src/Database/Connection.php:67
Stack trace:
#0 /var/www/html/vendor/nette/database/src/Database/Connection.php(67):
PDO->__construct(‘sqlsrv:server=d…’, ‘usr’, ‘pwd’, Array)
#1 /var/www/html/vendor/nette/database/src/Database/Connection.php(55):
Nette\Database\Connection->connect()
#2 /var/www/html/include/dbConnectToSandBox.php(27):
Nette\Database\Connection->__construct(‘sqlsrv:server=d…’, ‘usr’,
‘pwd’)
#3 /var/www/html/services/fetchFromAdobe.php(79): setDBConnToSandbox()
#4 /var/www/html/services/fetchFromAdobe.php(10): fetchFromAdobe()
#5 {main}
Next Nette\Database\ConnectionException: could not find driver in
/var/www/html/vendor/nette/database/src/Database/Connection.php:67
Stack trace:
#0 /var/www/html/vendor/nette/database/src/Database/Connection.php(70):
Nette\Database\DriverException::from(Object(PDOException))
#1 /var/www/html/vendor/nette/database/src/Database/Connection.php(55):
Nette\Database\Connection->connect()
#2 /var/www/html/include/dbConnectToSandBox.php(27):
Nette\Database\Connection->__construct(‘sqlsrv:server=d…’, ‘usr’,
‘pwd’)
#3 /var/www/html/services/fetchFromAdobe.php(79): setDBConnToSandbox()
#4 /var/www/html/services/fetchFromAdobe.php(10): fetchFromAdobe()
#5 {main}
- ujjgar
- Member | 3
Thanks David.
I will check with my infra team on this.
this is what my understanding was-
my infra team had earlier said that they do not support PDO driver by default
and suggested me to use nette\database.
So my assumption was, nette\database will somehow cover or include the pdo
extension in itself so that I can overcome the docker image's lack of pdo
driver.