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
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);
Trying to connect sql database in xampp
<?php
$host = 'localhost';
$user = 'root';
$password = '';
$db ='members';
$connection = mysqli_connect($host,$user,$password,$db);// you can select db separately as you did already
if($connection){
// do all your stuff that you want
}else{
echo "db connection error because of".mysqli_connect_error();
}
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
Cast the Current Object ($This) to a Descendent Class
Using Value of a Column as Index in Results Using Pdo
How to Bind SQL Variables in PHP
How to Remove the Leading Character from a String
Getting the Name of a Child Class in the Parent Class (Static Context)
Email Validation Using Regular Expression in PHP
How to Customize Fos Userbundle Urls
How to Skip Invalid Characters in Xml File Using PHP
Change Foreign Characters to Their Roman Equivalent
PHP Exec - Check If Enabled or Disabled
What Causes an Http 405 "Invalid Method (Http Verb)" Error When Posting a Form to PHP on Iis
Permission Denied with Bash.Sh to Run Cron
Resetting MySQL Root Password with Xampp on Localhost
Laravel No Supported Encrypter Found. the Cipher And/Or Key Length Are Invalid