hello everybody I'm new to coding world and I need your help I would like to connect to a server with xampp (3.2.2) and php version (7.2.4) windows 10... I have installed the drivers php_pdo_sqlsrv_72_ts_x86.dll and php_sqlsrv_72_ts_x86.dll to php.ini but also to php\ext
and when I am trying to connect to a server with this code doesn't respond
thank you very much for your time
$servename = "new";
$username = "user";
$password = "tree";
$database = "tree";
$connectionInfo =array("Database" =>"tree","USER" => "user", "PWD" => "tree");
$conn=sqlsrv_connect($servename,$connectionInfo);
if($conn)
{echo"connection established";}
else
{echo"connection failure";die (print_r(sqlsrv_errors(),true));}
Error message:
Fatal error: Uncaught Error: Call to undefined function sqlsrv_connect()
Here you can use it with help of PDO,
Please check below code and try to replace with your original data
<?php
$dsn = "sqlsrv:Server=**servernamehere**;Database=**Db name here**";
$conn = new PDO($dsn, "", "");
$conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$sql = "SELECT * FROM table_name";
foreach ($conn->query($sql) as $row) {
print_r($row);
}
?>
Related
In PHP 7, As I try to connect to the Beanstalk I have set up, I receive the error:
Fatal error: Uncaught Error: Class 'mysql' not found in C:\Apache24\htdocs\php_file.php, Stack trace: #0 {main} thrown in C:\Apache24\htdocs\php_file.php on line 41.
This server uses AWS RDS running MYSQL, and I am using apache 2.4 to localhost (for testing).
The code I'm using is:
$servername = "MY BEANSTALK CONNECTION";
$username = "Username";
$password = "PSSWD";
$dbname = "DBNAME";
$conn = new mysql($servername, $username, $password, $dbname);
$sql = "SELECT * FROM column";
$result = $conn->query($sql);
$conn->close();
My updated code uses mysqli, but is still getting the same error.
From PHP documentation mysql class is deprecated since version 5.5 and removed in version 7. that's why you're receiving the class not found error.
Try using mysqli. The PHP documentation has good examples on this. Look at example 2. I've pasted part of the example in this post for you.
https://www.php.net/manual/en/function.mysql-connect.php
<?php
$mysqli = new mysqli("example.com", "user", "password", "database");
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: " . $mysqli->connect_error;
}
$res = $mysqli->query("SELECT 'choices to please everybody.' AS _msg FROM DUAL");
$row = $res->fetch_assoc();
echo $row['_msg'];
?>
Use mysql_connect($servername, $username, $password) instead.
I am trying to connect to an existing SQL Server database using PDO with the drivers provided by Microsoft.
I have seen examples using odbc, dblib, mssql, etc., however I believe the connection string with these drivers should use 'sqlsrv'?
Are there any good examples of how to properly do this? If I should be doing this via some other method please let me know. Thanks!
Well that's the best part about PDOs is that it's pretty easy to access any database. Provided you have installed those drivers, you should be able to just do:
$db = new PDO("sqlsrv:Server=YouAddress;Database=YourDatabase", "Username", "Password");
Mind you that in my experience and also of other (PHP - Why is new SQLSRV driver slower than the old mssql driver?) that using PDO_SQLSRV is way slower than through PDO_ODBC.
If you want to use the faster PDO_ODBC you can use:
//use any of these or check exact MSSQL ODBC drivername in "ODBC Data Source Administrator"
$mssqldriver = '{SQL Server}';
$mssqldriver = '{SQL Server Native Client 11.0}';
$mssqldriver = '{ODBC Driver 11 for SQL Server}';
$hostname='127.0.0.1';
$dbname='test';
$username='user';
$password='pw';
$dbDB = new PDO("odbc:Driver=$mssqldriver;Server=$hostname;Database=$dbname", $username, $password);
This works for me, and in this case was a remote connection:
Note: The port was IMPORTANT for me
$dsn = "sqlsrv:Server=server.dyndns.biz,1433;Database=DBNAME";
$conn = new PDO($dsn, "root", "P4sw0rd");
$conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$sql = "SELECT * FROM Table";
foreach ($conn->query($sql) as $row) {
print_r($row);
}
Figured this out. Pretty simple:
new PDO("sqlsrv:server=[sqlservername];Database=[sqlserverdbname]", "[username]", "[password]");
try
{
$conn = new PDO("sqlsrv:Server=$server_name;Database=$db_name;ConnectionPooling=0", "", "");
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e)
{
$e->getMessage();
}
$servername = "";
$username = "";
$password = "";
$database = "";
$port = "1433";
try {
$conn = new PDO("sqlsrv:server=$servername,$port;Database=$database;ConnectionPooling=0", $username, $password,
array(
PDO::ATTR_PERSISTENT => true,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
)
);
} catch (PDOException $e) {
echo ("Error connecting to SQL Server: " . $e->getMessage());
}
I want to use PHP to connect to sql server database.
I installed xampp 1.7.0(php 5.2) and SQLSRV20. I've added the extensions in php.ini and I get this error:
Warning: mssql_connect() [function.mssql-connect]: Unable to connect to
server: 10.85.80.229 in C:\xampp\htdocs\xampp\test.php on line 07
Code:
<?php
$myServer = "10.85.80.229";
$myUser = "root";
$myPass = "pass";
$myDB = "testdb";
$dbhandle = mssql_connect($myServer, $myUser, $myPass)
or die("Couldn't connect to SQL Server on $myServer");
?>
What does this error message mean and how do I connect to SQL Server?
enable mssql in php.ini
;extension=php_mssql.dll
to
extension=php_mssql.dll
<?php
$serverName = "ServerName";
$uid = "sqlusername";
$pwd = "sqlpassword";
$databaseName = "DBName";
$connectionInfo = array( "UID"=>$uid,
"PWD"=>$pwd,
"Database"=>$databaseName);
/* Connect using SQL Server Authentication. */
$conn = sqlsrv_connect( $serverName, $connectionInfo);
$tsql = "SELECT id, FirstName, LastName, Email FROM tblContact";
/* Execute the query. */
$stmt = sqlsrv_query( $conn, $tsql);
if ( $stmt )
{
echo "Statement executed.<br>\n";
}
else
{
echo "Error in statement execution.\n";
die( print_r( sqlsrv_errors(), true));
}
/* Iterate through the result set printing a row of data upon each iteration.*/
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_NUMERIC))
{
echo "Col1: ".$row[0]."\n";
echo "Col2: ".$row[1]."\n";
echo "Col3: ".$row[2]."<br>\n";
echo "-----------------<br>\n";
}
/* Free statement and connection resources. */
sqlsrv_free_stmt( $stmt);
sqlsrv_close( $conn);
?>
http://robsphp.blogspot.ae/2012/09/how-to-install-microsofts-sql-server.html
if your using sqlsrv_connect you have to download and install MS sql driver for your php. download it here http://www.microsoft.com/en-us/download/details.aspx?id=20098
extract it to your php folder or ext in xampp folder
then add this on the end of the line in your php.ini file
extension=php_pdo_sqlsrv_55_ts.dll
extension=php_sqlsrv_55_ts.dll
im using xampp version 5.5 so its name php_pdo_sqlsrv_55_ts.dll & php_sqlsrv_55_ts.dll
if you are using xampp version 5.5 dll files is not included in the link...hope it helps
Try this code
$serverName = "serverName\sqlexpress"; //serverName\instanceName
$connectionInfo = array( "Database"=>"dbName", "UID"=>"userName", "PWD"=>"password");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
$server_name = "your server name";
$database_name = "your database name";
try
{
$conn = new PDO("sqlsrv:Server=$server_name;Database=$database_name;ConnectionPooling=0", "user_name", "password");
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e)
{
$e->getMessage();
}
Use localhost instead of your IP address.
e.g,
$myServer = "localhost";
And also double check your mysql username and password.
for further investigation: print out the mssql error message:
$dbhandle = mssql_connect($myServer, $myUser, $myPass) or die("Could not connect to database: ".mssql_get_last_message());
It is also important to specify the port: On MS SQL Server 2000, separate it with a comma:
$myServer = "10.85.80.229:1443";
or
$myServer = "10.85.80.229,1443";
$dbhandle = sqlsrv_connect($myServer, $myUser, $myPass)
or die("Couldn't connect to SQL Server on $myServer");
Hope it help.
first download below software
https://www.microsoft.com/en-us/download/details.aspx?id=30679 - need to install
https://www.microsoft.com/en-us/download/details.aspx?id=20098 - when you run this software . it will extract dll file.
and paste two dll file(php_pdo_sqlsrv_55_ts.dll,extension=php_sqlsrv_55_ts.dll) this location C:\wamp\bin\php\php5.6.40\ext\ (pls make sure your current version)
2)edit php.ini file
add below line
extension=php_pdo_sqlsrv_55_ts.dll
extension=php_sqlsrv_55_ts.dll
Please refer screenshort
add dll in your php.ini file
MS SQL connect to php
Install the drive from Microsoft website
After install, you will get some files. Store it in your system temp folder
Check your php version, thread or non thread, and window bit - 32 or 64
(Thread or non thread, this is get you by phpinfo())
According to your system & xampp configration (php version and all) copy 2 files (php_sqlsrv & php_pdo_sqlsrv) to xampp/php/ext folder .
Add to php.ini file :
extension=php_sqlsrv_72_ts_x64
extension=php_pdo_sqlsrv_72_ts_x64
(php_sqlsrv_72_ts_x64.dll and php_pdo_sqlsrv_72_ts_x64.dll are the files which you copied in 4th step)
Next here is the Php Code to connect to database :
$serverName ="DESKTOP-me\\MSSQLSERVER01"; (servername\\instanceName)
// Since UID and PWD are not specified in the $connectionInfo array,
// The connection will be attempted using Windows Authentication.
$connectionInfo = array("Database"=>"testdb","CharacterSet"=>"UTF-8");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn ) {
//echo "Connection established.<br />";
}else{
echo "Connection could not be established.<br />";
die( print_r( sqlsrv_errors(), true));
}
//$sql = "INSERT INTO dbo.master ('name') VALUES ('test')";
$sql = "SELECT * FROM dbo.master";
$stmt = sqlsrv_query( $conn, $sql );
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {
echo $row['id'].", ".$row['name']."<br />";
}
sqlsrv_free_stmt( $stmt);
For those who want to connect to database(MSSQL) using Windows Auth, mssql_connect' was removed in the 7.0 PHP version. So you can use this instead;
$serverName="serverName";
$uid = '';
$pwd = "";
$dbase = "nameOfDB";
$conninfro = array("Database"=>$dbase, "UID"=>$uid, "PWD"=>$pwd);
$con = sqlsrv_connect( $serverName, $conninfro);
if( $con === false){
die( print_r( sqlsrv_errors(), true));
}
Take a look at the matrix to understand the compatibility of different versions of Windows/SQL Server/PHP drivers.
Download PHP driver dll -
https://github.com/microsoft/msphpsql/releases/
Copy the dll files to
/ext folder.
Edit php.ini and add dll names under Dynamic
Extensions.
Restart the webserver -Apache or IIS
Eg: using 8.1 version with SQL Server 2019.
extension=php_sqlsrv_81_ts
extension=php_pdo_sqlsrv_81_ts
Database Connection test Code - https://www.php.net/manual/en/function.sqlsrv-connect.php
I've been having the same problem (well I hope the same). Anyways it turned out to be my version of ntwdblib.dll, which was out of date in my PHP folder.
http://dba.fyicenter.com/faq/sql_server_2/Finding_ntwdblib_dll_Version_2000_80_194_0.html
For the following code you have to enable mssql in the php.ini as described at this link: http://www.php.net/manual/en/mssql.installation.php
$myServer = "10.85.80.229";
$myUser = "root";
$myPass = "pass";
$myDB = "testdb";
$conn = mssql_connect($myServer,$myUser,$myPass);
if (!$conn)
{
die('Not connected : ' . mssql_get_last_message());
}
$db_selected = mssql_select_db($myDB, $conn);
if (!$db_selected)
{
die ('Can\'t use db : ' . mssql_get_last_message());
}
$serverName="ServerName";
$uid = 'sa';
$pwd = "password";
$dbase = "dbname";
$conninfro = array("Database"=>$dbase, "UID"=>$uid, "PWD"=>$pwd);
$con = sqlsrv_connect( $serverName, $conninfro);
if( $con === false){
die( print_r( sqlsrv_errors(), true));
}
you can use PDO.
1-you must use php_pdo_sqlsrv_xx_nts.dll and php_sqlsrv_xx_nts.dll extensions.
note that you must use the proper version of those for your PHP.
2- use this code
$servername = "IP";
$username = "";
$password = "";
$db='';
try {
$conn = new PDO("sqlsrv:Server=$servername;Database=$db", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}
and then use this code:
$stmt=$conn->prepare("select * from table");
$stmt->execute();
$result=$stmt->fetchall();
Try this to be able to catch the thrown exception:
$server_name = "your server name";
$database_name = "your database name";
try {
$conn = new PDO("sqlsrv:Server=$server_name;Database=$database_name;ConnectionPooling=0", "user_name", "password");
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
$e->getMessage();
}
I am trying to connect to an existing SQL Server database using PDO with the drivers provided by Microsoft.
I have seen examples using odbc, dblib, mssql, etc., however I believe the connection string with these drivers should use 'sqlsrv'?
Are there any good examples of how to properly do this? If I should be doing this via some other method please let me know. Thanks!
Well that's the best part about PDOs is that it's pretty easy to access any database. Provided you have installed those drivers, you should be able to just do:
$db = new PDO("sqlsrv:Server=YouAddress;Database=YourDatabase", "Username", "Password");
Mind you that in my experience and also of other (PHP - Why is new SQLSRV driver slower than the old mssql driver?) that using PDO_SQLSRV is way slower than through PDO_ODBC.
If you want to use the faster PDO_ODBC you can use:
//use any of these or check exact MSSQL ODBC drivername in "ODBC Data Source Administrator"
$mssqldriver = '{SQL Server}';
$mssqldriver = '{SQL Server Native Client 11.0}';
$mssqldriver = '{ODBC Driver 11 for SQL Server}';
$hostname='127.0.0.1';
$dbname='test';
$username='user';
$password='pw';
$dbDB = new PDO("odbc:Driver=$mssqldriver;Server=$hostname;Database=$dbname", $username, $password);
This works for me, and in this case was a remote connection:
Note: The port was IMPORTANT for me
$dsn = "sqlsrv:Server=server.dyndns.biz,1433;Database=DBNAME";
$conn = new PDO($dsn, "root", "P4sw0rd");
$conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$sql = "SELECT * FROM Table";
foreach ($conn->query($sql) as $row) {
print_r($row);
}
Figured this out. Pretty simple:
new PDO("sqlsrv:server=[sqlservername];Database=[sqlserverdbname]", "[username]", "[password]");
try
{
$conn = new PDO("sqlsrv:Server=$server_name;Database=$db_name;ConnectionPooling=0", "", "");
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e)
{
$e->getMessage();
}
$servername = "";
$username = "";
$password = "";
$database = "";
$port = "1433";
try {
$conn = new PDO("sqlsrv:server=$servername,$port;Database=$database;ConnectionPooling=0", $username, $password,
array(
PDO::ATTR_PERSISTENT => true,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
)
);
} catch (PDOException $e) {
echo ("Error connecting to SQL Server: " . $e->getMessage());
}
I am fairly new to using PHP. I downloaded XAMPP, and installed everything. PHP 5.5.27 is the version. I ran a test php program which was jsut echo "Hello World". It worked fine. I also was able to connect to MYSQL database using PHP.
$link = mysqli_connect("localhost", "u/n", "pass", "databasename";
Problem i am having and need help is with connecting to sql server. How do i do that? I saw an example online and tried it:
$serverName = "servername";
$connectionInfo = array("Database"="name", "UID"=>"U/N", "PWD"=>"pass";>
$conn = sqlsrv_connect($serverName, $connectionInfo);
But everytime i run this it tells me:
Call to undefined function sqlsrv_connect()
Can someone help me understand what is going on?
Consider using PHP's Data Objects (PDO) to connect to SQL Server (in fact you can use it to connect to MySQL or any other database).
Using the MSSQL sqlsrv API (various dlls must be set):
<?php
$server = 'servername';
$database = 'databasename';
$username = 'username';
$password = 'pass';
try {
$conn = new PDO("sqlsrv:Server=$server;Database=$database",
$user, $password);
}
catch(PDOException $e) {
echo $e->getMessage()."\n";
exit;
}
?>
Using the ODBC Driver or DSN API (requiring MSSQL ODBC Driver installed which usually ships with database or Windows in general):
<?php
$server = 'servername';
$database = 'databasename';
$username = 'username';
$password = 'pass';
try {
$dbh = new PDO("odbc:Driver={SQL Server};Server=$server;
database=$database",$username,$password);
}
catch(PDOException $e) {
echo $e->getMessage()."\n";
exit;
}
?>