How to connect Eloquent to SQL Server? - php

I'm trying to connect Eloquent to my SQL Server DB.
But it keeps throwing me QueryException: SQLSTATE[HY000] unrecognized msgno (localhost) (severity 11) (SQL: select top 1 * from [groups] where [groups].[id] = -6)
I've tried executing this query and got SQL Error [208] [S0002]Then I rewrote this query to:
SELECT TOP 1 * FROM [db].[schema].[groups] WHERE [db].[schema].[groups].[id] = -6
and it executed correctly. After that I think that my configuration may be incorrect.
Here is my Eloquent/Capsule configuration:
[
"driver" => "sqlsrv",
"host" => "localhost",
"database" => "[db].[schema]",
"username" => "sa",
"password" => "dbpass",
"charset" => "utf8",
"collation" => "",
"prefix" => "",
];
SQL Server Version: Microsoft SQL Server 2019 (CTP2.2) - 15.0.1200.24 (X64) Dec 5 2018 16:51:26 Enterprise Edition (64-bit) on Linux (KDE neon)
Eloquent version: 5.6.28
PHP Version: 7.2.10

Don't name your database name "[db].[schema]",
Use the name of the database you're actually using.
If your database is named InfinteCarrots use
Also, have the host np:DOMAINNAME\SQLEXPRESS
"driver" => "sqlsrv",
"host" => "np:EEBOFFICE\\SQLEXPRESS";
"database" => "InfiniteCarrots",
These are the connection details I use at this moment to connect with SQL server on a windows server.
If you are connecting to a remote server you will want to use the domain name for it and have the connection set up to use encryption always.
'driver' => 'sqlsrv',
'host' => 'example.com',
'port' => 1433,
'database' => 'database_name',
'username' => 'database_user',
'password' => 'database_password',

Related

connection to localhost works but not ip

If I connect to the database using 'localhost' as hostname, It works
$flag = [
'database_host' => 'localhost',
'database_port' => '',
'database_name' => 'dbdemo',
'database_user' => 'userdemo',
'database_password' => 'uijk',
'database_table' => 'tabledemo',
];
$link = mysqli_connect($flag['database_host'],$flag['database_user'],$flag['database_password'],$flag['database_name']);
But when I set up the hostname by using his ip (which I get through echo $_SERVER['SERVER_ADDR'];), It returns the error
Connection refused
I'm confused, is it not basicaly the same thing?

Having trouble with sqlsrv on CentOS 7. Looking for assistance or possibly ODBC alternative

I'm having a try at connecting to a MSSQL DB using PHP on a Linux system.
Using tsql -S <dbConfig> or tsql -H <dbHost> successfully connects to the database (with user credentials of course). However, using the same host and config within PDO returns a failed to connect to host adapter error.
The issue seems to be living somewhere between PHP PDO and, I'm assuming freetds.
The debug looks like this:
PDOException in Connector.php line 55:
SQLSTATE[01002] Adaptive Server connection failed (MYHOST.database.windows.net) (severity 9)
in Connector.php line 55
at PDO->__construct('dblib:host=MYHOST.database.windows.net;dbname=RecipeDB;charset=utf8', 'MYUSER#MYHOST', 'MYPASSWORD', array('0', '2', '0', false)) in Connector.php line 55
at Connector->createConnection('dblib:host=MYHOST.database.windows.net;dbname=RecipeDB;charset=utf8', array('driver' => 'sqlsrv', 'host' => 'MYHOST.database.windows.net', 'database' => 'RecipeDB', 'username' => 'MYUSER#MYHOST', 'password' => 'MYPASSWORD', 'charset' => 'utf8', 'prefix' => '', 'name' => 'remoteRecipes'), array('0', '2', '0', false)) in SqlServerConnector.php line 32
at SqlServerConnector->connect(array('driver' => 'sqlsrv', 'host' => 'MYHOST.database.windows.net', 'database' => 'RecipeDB', 'username' => 'MYUSER#MYHOST', 'password' => 'MYPASSWORD', 'charset' => 'utf8', 'prefix' => '', 'name' => 'remoteRecipes')) in ConnectionFactory.php line 61
at ConnectionFactory->Illuminate\Database\Connectors\{closure}()
It seems I'd managed to remove the line containing [global] from my /etc/freetds.conf file. Once I replaced it PDO was able to make a call to the database.
Below is what it looks like for me:
[global]
# TDS protocol version
; tds version = 4.2
tds version = 8.0
client charset = UTF-8
# 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 = 10
; connect timeout = 10
# 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
[testConnection]
host = MYHOST.database.windows.net
port = 1433
tds version = 8.0

Dokku deployed Silex can't find PdoServiceProvider

I have a project done with Silex, and I was using herrera-io/silex-pdo as the PDO provider, but I faced random crashes with socket errors (I connect to the DB via socket), since that lib is abandoned, I changed to csanquer/pdo-service-provider, and it works just fine on my localhost server, but when I deploy to remote, I get the following error:
PHP Fatal error: Class 'Csanquer\Silex\PdoServiceProvider\Provider\PdoServiceProvider' not found in /app/web/index.php on line 52
Here is the code around the line 52:
use Csanquer\Silex\PdoServiceProvider\Provider\PdoServiceProvider;
$app->register(
// you can customize services and options prefix with the provider first argument (default = 'pdo')
new PdoServiceProvider('pdo'), // Line 52
array(
'pdo.server' => array(
// PDO driver to use among : mysql, pgsql , oracle, mssql, sqlite, dblib
'driver' => 'mysql',
'host' => 'unix_socket=/app/mysqld.sock',
'dbname' => 'db_beta',
'port' => 3306,
'user' => 'user',
'password' => 'pass',
),
// optional PDO attributes used in PDO constructor 4th argument driver_options
// some PDO attributes can be used only as PDO driver_options
// see http://www.php.net/manual/fr/pdo.construct.php
'pdo.options' => array(
\PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'UTF8'"
),
// optional PDO attributes set with PDO::setAttribute
// see http://www.php.net/manual/fr/pdo.setattribute.php
'pdo.attributes' => array(
\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
),
)
);
Thanks in advance for any help, or any clue of what may be going wrong!
Turns out the problem was with the use instructions. To fix, simply change:
use Csanquer\Silex\PdoServiceProvider\Provider\PdoServiceProvider;
To:
use Csanquer\Silex\PdoServiceProvider\Provider\PDOServiceProvider;
And:
new PdoServiceProvider('pdo') To: new PDOServiceProvider('pdo')
Now it works!

MSSQL incorrect charset in php arrays

Need to create connect to mssql from ubuntu with php(laravel). I'm using freetds and sybase mssql adapter for it. Successfully connected to base and can select data from it. But, when i'm trying to get an array of data from table, characters have invalid charset. With one field all is ok.
Here is my freetds.conf
[global]
tds version = 8.0
client charset = UTF-8
text size = 20971520
And laravel database config
'sqlsrv' => array(
'driver' => 'sqlsrv',
'host' => 'some-ip\DB',
'database' => 'DBTEST',
'username' => 'web-testUser',
'password' => 'hereIsMyPassword',
'charset' => 'utf8',
'prefix' => '',
),
And simple function to get an array of data
Route::get('/', function()
{
return var_dump(User::all()); //if i'll try to simple return data without using var_dump it'll crash
});

Error using Cake PHP SQL Server Driver

Upgraded a (dev) Cake PHP site and had to change database drivers from ADO to SQL Server's own driver as ADO is no longer supported in Cake 1.3, I . The new SQL Server driver is installed in PHP, shows in PHPinfo() and should work, but when I try to load a page using the database, I get this error:
Warning (2): sqlsrv_query() expects parameter 1 to be resource, boolean given
[APP\plugins\datasources\models\datasources\dbo\dbo_sqlsrv.php, line 184]
Warning (512): SQL Error: An invalid parameter was passed to sqlsrv_query.
[CORE\cake\libs\model\datasources\dbo_source.php, line 684]
Query: SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES
What appears to be happening is my connection isn't established in the dbo_sqlsrv.php driver; a "$this->connection" variable is supposed to be the connection resource then passed to sqlsrv_query() and it's apparently a bool which is wrong (I tried to Echo the variable and it displays nothing).
This is where the connection SHOULD be set:
sqlsrv_connect($this->config['host'] . $port, $params)
Printing the variables that were passed in gives me this:
SRV, 1433 Array ( [Database] => DB [CharacterSet] => char
[MultipleActiveResultSets] => [UID] => sa [PWD] => password )
Each of those parameters is correct, is there a specific way I should format or change my database configuration array for this driver or is there something I am missing?
This error was due to outdated SQL Server drivers. Installing the latest from below allows a SQL Server connection:
http://www.microsoft.com/download/en/details.aspx?id=20098
I think there is some issue with your connection. PHP can't establish connection with sql Server.
var $default = array(
'driver' => 'sqlsrv',
'persistent' => false,
'host' => 'Myhome-PC\SQLEXPRESS', // or ip-address(192.168.1.13)
'login' => 'username',
'password' => 'password',
'database' => 'db',
'prefix' => 'tbl',
'port' => NULL,
'returnDatesAsStrings' => True
);
I think the Connection should be this
Here is the sqlsrv_dbo which I used.
And your $param should be something like this
$connectionInfo = array("UID" => $config['login'],
"PWD" => $config['password'],
"Database" => $config['database'],
'MultipleActiveResultSets' => 1,
'ConnectionPooling' => 0
);
if(isset($config['ReturnDatesAsStrings'])){
$connectionInfo['ReturnDatesAsStrings'] = $config['ReturnDatesAsStrings'];
}
$this->connection = sqlsrv_connect($config['host'] . $port, $connectionInfo);

Categories