Connect sql server 2005 using PHP - php

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

Related

DBMaker connection with PHP ODBC (using PDO)

I'm developing an application with a php framework (Yii) who use pdo connection also for remote ODBC databases.
I'm working on a Debian server with PHP 7.1, with DBMaker drivers installed, ODBC.ini configured so DBMaker works well with this code:
if(!odbc_connect('MYDBDSN','myuser', 'mypass')) {
echo "Connection not found " . odbc_errormsg();
} else {
echo "Database exists";
}
// queries works!
But a connection made by
$conn = new PDO ("odbc:MYDBDSN", 'myuser', 'mypass');
says Unable to connect; PDOException: could not find driver.
Is there any way to connect my DBMaker database with PDO, making my queries framework compatible?
I needed to install and configure pdo_odbc drivers for PHP.
This link can be useful if your servers works under a Docker environment.

PHP Connection statement for SQLserver

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.

Connection between MySQL and PHP

I have a Linux server with:
MySQL 5.1.57
PHP 5.1.6
When I try to connect MySQL- The output is a blank page.
The code is attached.
<?php
$link = mysql_connect("localhost", "usr", "pass");
if (!$link) {
die("Could not connect: " . mysql_error());
}
print "Connected successfully";
Btw do you enable mysql module for php? You can check with this command
php -i | grep mysql
or see in web
<?php phpinfo(); ?>
And check PHP error log.
Most likely you don't have the php_mysql extension installed, so it doesn't understand what you're trying to do. To be sure about that, check your web servers error log. It should tell you why it didn't parse the page.
But as Quentin already mentioned in the comments, the mysql extension has been deprecated for quite a while now. Try installing the MySQLi or PDO extension and use that instead. PDO is more flexible and offers prepared statements support, so I'd suggest that.
So, install the PDO extension on your server (see the documentation for more details). And then connect using something like this instead:
$dbh = new PDO('mysql:host=localhost;dbname=test', 'user', 'pass');
This will connect you to the test database on your localhost. More information can be found in the PHP docs here.

DB2 connection failed 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.

how to connect to sql server 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!

Categories