Connect sqlsrv in Xampp
EDIT-
First you need to download the driver
http://msdn.microsoft.com/en-us/sqlserver/ff657782.aspx
Now go to your XAMPP installation and search for php.dll
It will display correct PHP dll you have.
1) move following files to xampp/php/ext directory.
php_sqlsrv_53_nts_vc9.dll
php_pdo_sqlsrv_53_nts_vc9.dll
2) If you have php5ts.dll then move following files to xampp/php/ext directory.
php_sqlsrv_53_ts_vc9.dll
php_pdo_sqlsrv_53_ts_vc9.dll
above files should be used if your PHP version is compiled with Visual C++ 9.0 . Else following files should be used.
1) If you have php.dll then move following files to xampp/php/ext directory.
php_sqlsrv_53_nts_vc6.dll
php_pdo_sqlsrv_53_nts_vc6.dll
2) If you have php5ts.dll then move following files to xampp/php/ext directory.
php_sqlsrv_53_ts_vc6.dll
php_pdo_sqlsrv_53_ts_vc6.dll
Now we have to load files that we added recently. Open the php ini file and add entry in the area of dynamic extensions as follow.
extension=php_sqlsrv_53_nts_vc9.dll
extension= php_pdo_sqlsrv_53_nts_vc9 .dll
Save the ini files and restart XAMPP
$check= @$CI->load->database($config, TRUE); // ommit the error
if ($check->call_function('error') !== 0) {
// Failed to connect
}
I don't know for sure what you are trying to do but in codeigniter you don't need to initialise database, CI automatically does it for you
so-
$this->load->database();
$db_obj = $this->db->load('SQL_Test',TRUE);
$connected = $db_obj->initialize();
this is not needed.
You just need to load the model and in model start performing queries.
$this->load->database();
In controller you need to load the model like-
$this->load->model('my_model');
then call the model function in which you have written the queries.
$this->my_model->myfunction();
XAMPP/SQLSRV: Unable to find Sqlsrv in PHPINFO(); - errors coming from connection
Solved. There were two problems.
My Xampp installation was corrupt. It wouldn't recognize ANY new DLLs I noticed from using
echo phpinfo();
on my page.The ini file configuration was attempting to use the x86 dll's instead of the x64's for some reason, even when the extensions were written in the ini as x64.
Steps to solve:
I reinstalled and reconfigured Xampp with default settings once more, and dll's were now recognized.
I removed the x86 dll's from the extensions directory of Xampp.
The final php.ini is as such:
extension=php_sqlsrv_81_ts_x64.dll
extension=php_pdo_sqlsrv_81_ts_x64.dll
extension=php8ts.dll
extension=php_pdo_odbc.dll
extension=php_pdo.dll
Utilizing the final .dll may be redundant, but it still works as intended. Make sure you verify the correct version of sqlsrv or pdo_sqlsrv, as compatibility issues with your phpversion may occur.
How to connect xampp php 7.1.27 to a MsSQL server using pdo?
I found the answer from the comments:
Download and install ODBC Driver
Configure php.ini file by uncommenting this line:
extension=php_odbc.dll
Restart xampp
use this method to connect :
odbc_connect ( "Driver={SQL Server};Server=$servername;Database=$dbname" , $username , $password);
Connect XAMPP to microsoft sql server management studio 2012 in one machine
You first need to install or enable(if already installed) the sqlserver php driver and then you could try something like below which i have just taken from Microsoft documentation
<?php
$serverName = "(local)";
$database = "AdventureWorks";
// Get UID and PWD from application-specific files.
$uid = file_get_contents("C:\AppData\uid.txt");
$pwd = file_get_contents("C:\AppData\pwd.txt");
try {
$conn = new PDO( "sqlsrv:server=$serverName;Database = $database", $uid, $pwd);
$conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
}
catch( PDOException $e ) {
die( "Error connecting to SQL Server" );
}
echo "Connected to SQL Server\n";
$query = 'select * from Person.ContactType';
$stmt = $conn->query( $query );
while ( $row = $stmt->fetch( PDO::FETCH_ASSOC ) ){
print_r( $row );
}
// Free statement and connection resources.
$stmt = null;
$conn = null;
?>
I hope it helps you
Cannot connect to a SQL database via XAMPP - Driver's SQLSetConnectAttr failed
Try updating SQL Server
drivers!!
You probably can find those drivers by Googling "Download ODBC Driver for SQL Server
".
In my case, verion 17 or above fixed the issue.
Also, try the PDO-SQL-Server example (maybe PDO implementation works).
Example:
<?php
function mssqlConnect() {
$ServerName = "xxx";
try {
$conn = new PDO( "sqlsrv:server=$ServerName;Database = database", 'user', 'password');
$conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
} catch( PDOException $e ) {
die( "Connection could not be established - " . $e->getMessage() );
}
echo "Connection established";
return $conn;
}
function findEmployee($name) {
$conn = mssqlConnect();
$stmt = $conn->prepare('SELECT * FROM employees WHERE name = :name');
if ($stmt->execute(array('name' => $name))) {
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
// do something with $row
}
} else {
echo 'Query error - ' . join(' ', $stmt->errorInfo());
}
// Close connection which execute created.
$stmt->closeCursor();
}
?>
Related Topics
Access MySQL Field's Comments with PHP
File Upload PHP $_Files Undefined Index Error
Switch of PHP Versions Not Working on MAC
How to Allow a User to Download a File Which Is Stored Outside of the Webroot
Check If Username Exists in MySQL Table via PHP
How to Select a Result from the Select2 Search Results
Expected Response Code 250 But Got Code "", with Message ""
How to Run PHP Code from File_Get_Contents or File in a Function
PHP Code Can Insert Image to Excel File and Open It Correctly in Ms Excel
Php/Apache: PHP Fatal Error: Call to Undefined Function MySQL_Connect()
How to Determine If an Array Has Any Elements or Not
Why Ob_Start() Must Come Ahead of Session_Start() to Work in PHP
Attach Image to Facebook Event (PHP Sdk, Rest or Graph API)
PHP HTML Decoding Help - Converting: a 'Quote' Is <B>Bold</B>
Nginx + PHP-Fpm 7.1 - 504 Gateway Time-Out