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.
Related
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
Below is my connection string for mssql server its named SKPI-APPS1.
I know mssql is to be removed from php but i just need to finish this simple webpage for my company since all of their webpages are made from the old php version so they cannot just easily switch the new php version.
The code below doesn't display anything when I'm trying it with localhost/login.php
doesn't even display errors.
Can anyone define what the problem is coming from?
<?php
$myServer = "SKPI-APPS1";
$myUser = "sa";
$myPass = "";
$myDB = "AFS";
//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");
//close the connection
mssql_close($dbhandle);
?>
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.
Credit to http://samsami2u.wordpress.com/2008/06/30/how-to-connect-mssql-with-php/
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!
I have inherited a web site that is in PHP and we are mostly a Windows shop. I cannot get the PHP code to connect to MySQL at all.
1 - The IDE is Eclipse. I imported the project and set up a debug configuration and everything works fine there with HTML and other PHP (non DB) code.
2 - I am running IIS 7.5 with PHP enabled
3 - I have tried to connect to two completely separate MySQL instances (different hosts, different DBs, etc) and it will not connect
I have already made sure that sql.safemode is turned to OFF and there is no default server in php.ini (that shouldn't come into play if safe mode is OFF, right?)
I have done nothing special to the PHP code or project and I'm wondering if that's part of the issue - do I need a JAR or reference of any kind to enable the MySQL connections?
The PHP code is as follow - nothing special I think:
$db_name = "foo";
$connection = #mysql_connect("myserver", "myuser", "mypw") or die("Could not connect to database");
mysql_select_db($db_name, $connection) or die("Count not select database");
the above code always results in the die statement of "Could not connect to database"
The # symbol is not necessary for making a DB connection, as far as I know.
Try
$connection = mysql_connect("myserver", "myuser", "mypw") or die("Could not connect to database");
First need to load php.ini file.
How to load php.ini file
Rename any one of the file php.ini-production/php.ini-development to php.ini from C:\PHP(note now the extention will be ini i.e "php.ini").
After renaming to php.ini file restart server .Open a text editor, for example Notepad, as Administrator.
In a new file, type the following text:<?php phpinfo(); ?>
Save the file as C:\inetpub\wwwroot\Phpinfo.php.
Open a browser and enter the following URL:
http://localhost/phpinfo.php
How to Enable mysqli in php.ini
Edit php.ini by uncommenting (removing ';') the following config-lines:
// 1st (uncomment and add config):
include_path = "C:\php\includes"
// <2nd (uncomment):
extension_dir = "ext"
// 3rd (uncomment and edit config):
extension=C:/PHP/ext/php_mysql.dll
extension=C:/PHP/ext/php_mysqli.dll
After editing restart the IIS server and make sure that mysql is running on the system.
Make changes in the code i.e phpinfo.php file to see DB connectivity
<?php
$servername = "localhost";
$username = "root";
$password = "";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
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