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.
Related
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 trying to connect to remote DB2 via PHP. But have some problems. I've already installed IBM Application developer client.
phpinfo() output:
IBM DB2, Cloudscape and Apache Derby support enabled
Module release 1.9.4
Module revision $Revision: 327944 $
Binary data mode (ibm_db2.binmode) DB2_BINARY
Then, I've got a php file which is looking like:
$database = 'MyDB';
$user = 'db2inst1';
$password = 'mypassword';
$hostname = '1.1.1.1';
$port = 50000;
$conn_string = "DRIVER={IBM DB2 ODBC DRIVER};DATABASE=$database;PORT=$port;HOSTNAME=$hostname;".
"PROTOCOL=TCPIP;UID=$user;PWD=$password;";
$conn = db2_connect($conn_string, '', '');
if ($conn) {
echo "connection to $database succeeded";
} else {
echo "connection to $database failed";
echo db2_conn_errormsg();
}
And trying to execute this file, I have "connection to MyDB failed", and NO visible response from db2_conn_errormsg(), which is actually making me baffled
Unfortunately, I haven't got a straight access to the remote server with database. But several months ago, when I was using other client, I succeeded to connect to exactly this database. But that time I didn't need to install IBM ADCL. That is why I can guess that problem is on this side. But even if so, I couldn't fix it.
Sorry if I duplicated some question on stackoverflow, but all answers, which I found, were unfortunately useless to me.
I'm using Apache 2.2 and PHP 5.4.
Hope you can help.
Thanks for any replies!
Are you sure you have connectivity to the server? Correct port, server, firewall rules, username, password, database name?
What is the SQL code you are receiving. Try to get the SQL code from PHP, "connection to xx failed" is your own code so it is useless to help you.
Did you install the application development client? which DB2 version are you using? ADCL is old, it was for DB2 8. Since DB2 9.7, clients have different names, and I think you need IBM Data server client in order to compile the php module. For more information, check this website: http://www-01.ibm.com/support/docview.wss?uid=swg27016878
I think you have to catalog the database server (node) and the database in the local machine with the db2 client. It seems that your PHP code uses ODBC driver, and it has to be configured locally.
Your connection string looks like an ODBC connection where as the db2_connect function in PHP needs :-
DATABASE=database;HOSTNAME=hostname;PORT=port;PROTOCOL=TCPIP;UID=username;PWD=password;
It's on the PHP web page.
I have never been able to get ibm_db2 or pdo_ibm working on a remote. They work if DB2 and Apache are on the same machine (like the iSeries) but not if the host is connecting to a remote DB2.
If you read the Doctrine2 PHP drivers for each you will find they redirect to ODBC if the host is not 'localhost' or '127.0.0.1'.
This code works for me
<?php
if (!$db = odbc_connect ( "AS400", $user, $password) )
echo 'error!';
$result = odbc_exec($db, "select count(*) from $table");
while (odbc_fetch_row($result)) {
var_dump($result);
print_r($result);
echo "\n";
echo odbc_result($result, 1)."\n";
}
odbc_close($db);
AS400 is DSN name defined in ODBC.
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!
Using ubuntu 12.04 64 bit on Lenovo t410.
Using apache2 and Mysql 5.5 and attempting to connect via localhost
I am attempting to establish a connection to a database that I made on localhost. When the line of code is reached to establish a connection, it seems Mysql simply hangs, and there is no error message displayed after. I verified that an echo works immediately prior to the connection attempt. I know that apache2 server is working as I can access the index page and display my html form.
I have tried etc/mysql/my.cnf setting the bind address to localhost.
My line of code looks like:
// Attempts to establish connection to MySql server
$connection = mysql_connect("localhost","username","password");
// Prints error message if the connection to MySql fails
if (!$connection){
die("Connection failed: " . mysql_error());
}
echo "Connection established.";
I tried the connection line with single quotes and with no semi-colon as well.
I am willing to post the contents of any configuration file I have if the error isn't syntax. I haven't done anything fancy to Ubuntu, everything is the default install. I am new to CS and especially databases, PHP, and networking. This is my little experiment that I am stuck on.
Any help is greatly appreciated. Thanks,
Don
Can it be, because there is no error message, that the connection IS established, but you didn't do anything with it?
I mean, what is the rest of your code, is there after your code here something like:
mysql_select_db("database_name",$connection);
After reading your last comment, it appears the mysql extensions are not being loaded. Have a look at your php.ini, uncomment the following line (remove the semicolon at the beginning of the line) and restart your apache:
extension=php_mysql.so
Make sure the extension exists in the php extensions directory.
Due to the fact that you are using MySQL version > 4.1.3 it is strongly recommended that you use the mysqli extension instead. Have a look at this: PHP: MySQL Overview
try to set
$mysql_user = "your_username";
$mysql_pass = "your_password";
$mysql_server = "Servername";
$link = mysql_connect($mysql_server,$mysql_user,$mysql_pass);
if (!$link) {
header('HTTP/1.1 500');
exit();
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