I'm new in sql server.i have application that use MySQL and now i want to use sql server instead of MySQL in that application. my php is:
<?php
$myServer = "localhost";
$myUser = "";
$myPass = "";
$myDB = "UNIVERSITY";
//connection to the database
$dbhandle = mssql_connect($myServer, $myUser, $myPass)
or die("Couldn't connect to SQL Server on $myServer");
//select a database to work with
$selected = mssql_select_db($myDB, $dbhandle)
or die("Couldn't open database $myDB");
//declare the SQL statement that will query the database
$query = "SELECT clgname";
$query .= "FROM dbo.clg ";
//execute the SQL query and return records
$result = mssql_query($query);
$numRows = mssql_num_rows($result);
if($numRows==0){
echo "false";
}
else{
echo "true";
}
//close the connection
mssql_close($dbhandle);
?>
i also remove the semi-colon before the
extension=php_mssql.dll
but i see error:
Fatal error: Call to undefined function mssql_connect() in C:\xampp\htdocs\1.php on line 8
please help. thanks.
Your PHP doesn't compile with mssql extension, just ask hosting provider to enable it if they support ,of if you have own server, just compile PHP with mssql extension --with-mssql=DIR where DIR is the FreeTDS install prefix. And FreeTDS should be compiled using --enable-msdblib .
You can use PDO to make this work. I use it with MSSQL and works great. Example:
try {
$mssql = new PDO('mssql:host=localhost;dbname=UNIVERSITY', 'username', 'passwd');
$sth = $mssql->prepare("SELECT * FROM STUDENTS");
$sth->execute();
$students_list = array();
$students_list = $sth->fetchAll();
$mssql = null;
} catch (PDOException $e) {
$students_list = null;
}
EDIT: si-le is right, you also have to install the mssql module and add the line to php.ini for this to work
Looks like the mssql extension isn't being enabled properly...
If you are running WAMP locally, then you may have a few extra steps to complete.
I found a blog about this, and made my own here;
http://pjgcreations.blogspot.co.uk/2013/01/enabling-ms-sql-extensions-in-wamp.html
The basic idea is;
Download ntwdblib.dll:
http://www.pjgcreations.co.uk/BlogAttachments/ntwdblib.DLL
Click on the WAMP icon -> PHP -> PHP Extensions then check "php_mssql"
and "php_pdo_mssql". (Wamp will restart and give you few errors, ignore
them)
Restart WAMP one more time to ensure settings are saved.
Finally, place ntwdblib file in the two following directories:
"wamp\bin\php\php5.3.1" (or the PHP Directory relating to the version you are using) and "wamp\bin\apache\apache2.2.11\bin" (Or the Apache Directory relating to the version you are using)
Restart wampserver, and you're finished!
Hope this helps!
Related
I have a SQL server on MS azure. Using a php file and some basic select statements (eg. select * from table) I have been able to grab data from the database, so I know my details are correct.
However when I put a connect statement in any other files the page appears blank. For example, any form submission pages.
This is the connect statement I have:
<?php
$myServer = "hostname";
$myUser = "username";
$myPass = "password";
$myDB = "dbname";
$dbhandle = mssql_connect($myServer, $myUser, $myPass)
or die("Couldn't connect to SQL Server on $myServer");
?>
I'm sure it's probably something simple but I have tried loads of different ways and nothing seems to work, I don't even get an error message to work with.
Could someone please help?
Uncomment this line in your php.ini.
extension=php_mssql.dll
To locate your php.ini file, add this code below at the top of your php script
<?php phpinfo(); ?>
By default, Azure environment doesn't install php_mssql.dll extension, it installs php_sqlsrv.dll instead.
You can leverage Console tool in Azure portal and execute php -m to check all the preinstalled modules in Azure PHP runtime:
You can use PDO in PHP to handle sql server database, such as the following connection test:
try {
$conn = new PDO("sqlsrv:server = tcp:garysql.database.windows.net,1433; Database = garymbdata", "gary", "QWEasdzxc123");
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (PDOException $e) {
print("Error connecting to SQL Server.");
die(print_r($e));
}
Additionally, we can configure PHP configuration settings on Azure Web App beside modifying php.ini directly. Also we can install mssql.dll extension on Azure Web App if you insist on.
Please refer to https://learn.microsoft.com/en-us/azure/app-service-web/web-sites-php-configure for more info.
I have a problem with the drivers that I've installed so my MSSQL could run side by side with PHP...
I have the following drivers placed in the directory file of PHP and also edited the .ini file of it:
php_sqlsrv_54_ts.dll
php_pdo_sqlsrv_54_ts.dll
I have the following error message:
PHP Startup: Unable to load dynamic library 'c:/wamp/bin/php/php5.4.12/ext/php_sqlsrv_54_ts.dll' - %1 is not a valid Win32 application.
PHP Startup: Unable to load dynamic library 'c:/wamp/bin/php/php5.4.12/ext/php_pdo_sqlsrv_54_ts.dll' - %1 is not a valid Win32 application.
And I have the following information about my PHP and MSSQL:
FOR PHP:
PHP Version 5.4.12
PHP installed via WAMP SERVER
Architecture x64
Thread safety enebled
FOR MSSQL:
MSSQL 2014 Express Edition
Am using Microsoft SQL Server Management Studio to manage Databases
Those are all the following information... Am I at messed here? please help me, Im still a newbie here in PHP especially in MSSQL.. CHEERS!
This is my code #JayBhatt what must be corrected to run the following code:
<?php
$myServer = "localhost";
$myUser = "sa";
$myPass = "joseph04";
$myDB = "cars";
//connection to the database
$dbhandle = mssql_connect($myServer, $myUser, $myPass)
or die("Couldn't connect to SQL Server on $myServer");
//select a database to work with
$selected = mssql_select_db($myDB, $dbhandle)
or die("Couldn't open database $myDB");
//declare the SQL statement that will query the database
$query = "SELECT brand, model, year ";
$query .= "FROM cars ";
$query .= "WHERE brand='Honda'";
//execute the SQL query and return records
$result = mssql_query($query);
$numRows = mssql_num_rows($result);
echo "<h1>" . $numRows . " Row" . ($numRows == 1 ? "" : "s") . " Returned </h1>";
//display the results
while($row = mssql_fetch_array($result))
{
echo "<li>" . $row["brand"] . $row["model"] . $row["year"] . "</li>";
}
//close the connection
mssql_close($dbhandle);
?>
I had the same problem and after a lot of research I had discovered that, Microsoft haven't released a 64 bit Version of driver for MSSQL.
The driver you have is for a 32 bit environment. And that's why it fails to load. Try switching to a 32 bit environment of WAMP server and it will work.
Hope this helps.
You can get unofficial x64 SQLSRV drivers for PHP . Try to install them. Here is your link. Just replace the dlls with the x64 dlls.
http://robsphp.blogspot.in/2012/06/unofficial-microsoft-sql-server-driver.html
Moreover you should be SQLSRV functions to connect to the database.
and MSSQL functions. Both of them use different drivers.
You should use "sqlsrv_connect" instead of "mssql_connect".
Refer: http://php.net/manual/en/book.sqlsrv.php
i'm new in sql server, i just wanted to connect sql server express edition 2012 with php using iis 7 on windows 7 machine.
here is my code :
$myServer = "ASUS\SQLEXPRESS";
$myUser = "sa";
$myPass = "1991";
$myDB = "test";
echo("aaaa");
//connection to the database
$dbhandle = mssql_connect($myServer, $myUser, $myPass);
echo("bbbb");
//select a database to work with
$selected = mssql_select_db($myDB, $dbhandle)
or die("Couldn't open database $myDB");
//declare the SQL statement that will query the database
$query = "SELECT *FROM mhs";
//execute the SQL query and return records
$result = mssql_query($query);
$numRows = mssql_num_rows($result);
echo "<h1>" . $numRows . " Row" . ($numRows == 1 ? "" : "s") . " Returned </h1>";
//display the results
while($row = mssql_fetch_array($result))
{
echo "<li>" . $row["nim"] . $row["nama"] . "</li>";
}
//close the connection
mssql_close($dbhandle);
my php code running on local iis, i think username, password and servername is correct but when i execution it on local iis, it not show any errors, just 'aaaa' and 'bbbb' not showed.
i think i was wrong in mssql_connect but whats wrong? can anybody help me?
thanks.
I'm not sure, but Newer version of SQL severs doesn't compatible with mssql . You should go with the update SQLSRV (PHP.NET) OR SQLSRV (Microsoft Site) . I faced same problems with MSSQL and in the end moved to SQLSRV. Now it works fine.
change your code to whats written below so you can catch the error.
$dbhandle = mssql_connect($myServer, $myUser, $myPass) or die(mssql_error());
Please follow the steps given below to connect MSSQL with php:
Settings related to your php.ini file:
a) search the variable mssql.secure_connection in your php.ini file and put it to on mode if its off
b) remove comment from the dll extention php_mssql.dll (i.e. remove the ; from the front of the extention )
Settings related to the dll files. Download a file name ntwdblib.dll from the internet. you can download it from here or can search on internet for that. copy the downloaded dll to the apache/bin directory and for IIS copy it to the php extention directory (if path not known can be found in php.ini for variable extension_dir) also you need to have your php_mssql.dll in your php extension directory. if its not present please download it and copy it to the default php extension directory.
restart all your services (i.e. php and apache or iis) and you can use the script given below to connect to your SQL Server.
I'm trying to connect SQL Server 2005 using PHP. I searched in Google but am not getting proper solution, it's showing search results about mssql_connect() is not working properly. By seeing this am not getting anything.
require_once 'login.php';
$db_server = mysql_connect($db_hostname, $db_username, $db_password);
mysql_select_db($db_database) or die("Unable to connect to database: ".mysql_error());
This will connect mysql database. I tried replace mysql_connect() with mssql_connect(). But its not working. login.php has
$db_hostname = 'localhost';
$db_database = 'urlstore';
$db_username = 'root';
$db_password = 'tiger';
How can I connect SQL database using PHP.
You need to install MS SQL PHP extensions and then you can work with your MS SQL Server the way you are used to.
Here is the information about the extension and how to install it.
You could use PDO if your server configuration supports it. PDO is an abstraction layer that allows you to connect to many different database types using the same object.
<?php
try {
$dbh = new PDO ('mssql:host='.$mssql_server.';dbname='.$mssql_db, $mssql_login, $mssql_pwd);
$dbh->exec("INSERT INTO tablename(column1, column2) VALUES ('stuff', 'here')");
$dbh = null;
}
catch (PDOException $e)
{
echo $e->getMessage();
}
?>
This code can help you determine if PDO is enabled:
<?php
foreach(PDO::getAvailableDrivers() as $driver)
{
echo $driver.'<br />';
}
?>
OR you can run this:
<?php phpInfo(); ?>
and look for this:
PDO
PDO support PDO drivers
enabled dblib, mysql, odbc, pgsql, sqlite, sqlite2
If it isn't enabled for MSSQL, you can uncomment the "extension=php_pdo_mssql.dll" line in php.ini.
If it still doesn't work, you might want to try this:
ntwdblib.dll - The most common issue is that you do not have the
ntwdblib.dll file installed in your PHP directory (where php.exe is,
or sometimes placing it in the ext directory works as well). This
library can be found with your Enterprise Manager dll's or in your SQL
servers system32 folder. It's generally best to take the file from the
server where SQL Server is installed
-quoted from http://www.helpspot.com/helpdesk/index.php?pg=kb.page&id=13
I am using the current code in attempt to access a msSQL 2005 db:
<?php
$myServer = "[server]";
$myUser = "[username]";
$myPass = "[password]";
$myDB = "[db]";
//connection to the database
$dbhandle = mssql_connect($myServer, $myUser, $myPass)
or die("Couldn't connect to SQL Server on $myServer");
//select a database to work with
$selected = mssql_select_db($myDB, $dbhandle)
or die("Couldn't open database $myDB");
//declare the SQL statement that will query the database
$query = "SELECT id, name, year ";
$query .= "FROM cars ";
$query .= "WHERE name='BMW'";
//execute the SQL query and return records
$result = mssql_query($query);
$numRows = mssql_num_rows($result);
echo "<h1>" . $numRows . " Row" . ($numRows == 1 ? "" : "s") . " Returned </h1>";
//display the results
while($row = mssql_fetch_array($result))
{
echo "<li>" . $row["id"] . $row["name"] . $row["year"] . "</li>";
}
//close the connection
mssql_close($dbhandle);
?>
It is returning the following:
Warning: mssql_connect() [function.mssql-connect]: Unable to connect to server: XXXXXXX in D:\xxxxx.xxx\xxxx.php on line 16
Couldn't connect to SQL Server on XXXXXXX
What do you think the problem is?
It sounds to me like one of your DLLs is the wrong version. There was an issue of some sort with the move from SQL2000 to SQL2005 that the creators of PHP didn't resolve themselves. There are a variety of posts about it here: the following link
I believe the DLL is ntwdblib.dll and the version needs to be version 2000.80.194.0 at the very least. If you're running Apache or WampServer, there is an identical dll where the Apache DLLs are stored that needs to be overwritten.
Note: I was having this issue a few days ago and finding the correct DLLs and overwriting both allowed it to work.
Also: You may need to setup remote connections. Sql Server 2005 has remote connections disabled by default. You can allow remote connections by running the SQL Surface Area Configuration utility.
Try calling mssql_get_last_message() to get the last error message:
$dbhandle = mssql_connect($myServer, $myUser, $myPass)
or die("Couldn't connect to SQL Server on $myServer. Error: " . mssql_get_last_message());
stop using
mssql_connect
and start using
sqlsrv_connect
That will save you a lot of headaches. Plus, the function *mssql_connect* has been deprecated.
For using sqlsrv_connect, you must download the driver and install it as an extension for PHP to recognize the sqlsrv functions. Download the driver from Microsoft Download Center (search for "sql server php driver"), at the time of this post the download url was: http://www.microsoft.com/en-us/download/details.aspx?id=20098
The right steps for installation are clearly explained by Microsoft itself at http://www.microsoft.com/en-us/download/details.aspx?id=20098
download the driver. 2. put it in the PHP ext folder. 3. update the php.ini 4. restart the server.
After installing the sql server driver, then simply follow the instructions from http://www.php.net/manual/en/function.sqlsrv-connect.php. Below a quick snippet:
<?php
$serverName = "serverName\sqlexpress"; //serverName\instanceName
// Since UID and PWD are not specified in the $connectionInfo array,
// The connection will be attempted using Windows Authentication.
$connectionInfo = array( "Database"=>"dbName");
$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));
}
?>
Vote up!
Invalid credentials, if your not using localhost be sure you add the server's ip to the safe list.
I had some difficulties with this a few months ago, and I found that the only way to make it work was to include the instance name when specifying the server. For example:
$myServer = "SERVER\INSTANCENAME";
Specifying just the server would not work, even with TCP/IP enabled.
First try to do something like phpinfo() on your browser, than see which databases are allowed.
If you don't see anything like mssql then it is not configured. So use the odbc_connect() which will be configured...your connection string will be like this:
$server = '';
$user = '';
$password = '';
$database = '';
$connection = odbc_connect("Driver={SQL Server Native Client `11.0};Server=$server;Database=$database;", $user, $password);`
If you are using mssql 2005 or 2008, then change d 11.0 to 10.0 and for other versions just change to the version of your mssql.
Download the driver in this site http://www.microsoft.com/en-us/download/details.aspx?id=20098, then open your php.ini file and add the DLL that you download.
Late response, but it might help someone.
we are nearly there, it is the problem with your connection parameters,
probably you need to give servername as IP:port
servername:: The MS SQL server. e.g. hostname:port (Linux), or hostname,port (Windows).
Server Name should be "server\instance"
An instance is nothing more than specific port address different than 1433... so just discover the port on mssql server and then try to connect using: ip:port
Example code that works completely fine for me::
ini_set('display_errors', '1');
// $myServer = "winsrv22.somedns.co.uk:22320";//this style works as well
$servername = "123.21.47.23:22320";
$myUser = "UserName";
$myPass = 'xxxxxxxx';
$myDB = "[database]";
//connection to the database
$dbhandle = mssql_connect($servername, $myUser, $myPass)
or die("Couldn'tt connect to SQL Server on $myServer");
if($dbhandle) {
echo "Success, Connected\r\n";
} else {
echo "problem :( \r\n";
}
Hope this helps some one :)
Happy coding!!
if you using windows 10.
step 1. click start button
step 2. type Service and enter (open App Services)
step 3. find - SQL Server (MSSQLSERVER)
step 4. Right click ->select Properties
step 5. show popup. you select 2nd tab (Log On)
step 6. select local system account
step 7. and check allow service to interact with desktop
step 8. finally click ok.
1 time refresh
now connected.
I hope it's very useful