MSSQL Connection code syntax error? - 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/

Related

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.

Firebird php Failed to locate host machine

I try to run an ibase_connect to an firebird database.
The database is NOT on my localhost. It run on an network windows server.
I have windows too and have an connection to the shared folder:
Z:\Folder_WITH_DB.
I have XAMPP installed with
ibase extension
pdo firebird extension.
I copied the fbclient.dll into my XAMPP/php folder.
But if i run this code:
$db = 'Z:/Folder_WITH_DB/database.fdb';
$username='SYSDBA';
$password='masterkey';
$dbh = ibase_connect ( $db, $username, $password ) or die ("error in db connect");
gives this error
Warning: ibase_connect(): Unable to complete network request to host "Z". Failed to locate host machine. in xxx/index.php on line xx
firebird.log:
INET/INET_connect: gethostbyname (Z) failed, error code = 11004
I added gds_db 3050/tcp to the service file on my localhost and the server (have restarted both) and it is the same error.
Windows Firewall is deactivated on server. Service for firebird server and firebird guardian is running.
Firebird can only use a database file local to the database server. You can't connect to a Firebird database on a network share*. You need to connect to the Firebird server instead.
So if the database of 'Z:/Folder_WITH_DB/database.fdb' is on server firebirdsrv in folder D:\data\database.fdb, then you need to connect to firebirdsrv/3050:D:\data\database.fdb. I strongly suggest to remove the networkshare.
*: Technically a share can be used, but it is disabled by default because accessing a database from multiple servers through a network share can corrupt a database.

How to connect MySQl using php on IBM Bluemix?

I am install service MySQL to my PHP app on Bluemix and the error is on connection establish on this lines:
$con = mysql_connect("192.155.247.248:3307","uqDqUZ2EKoZ5I","pWXeBZbNtdpOv");
if (!$con){
echo "Failed to connect to MySQL: " .mysql_error();
}
mysql_select_db("d65a2b7e14b594d18a049ac918a4a8603",$con);
Quentin suggested to use mysqli instead of mysql_* as the latter is deprecated, i.e. try this:
$mysqli = new mysqli("192.155.247.248:3307","uqDqUZ2EKoZ5I","pWXeBZbNtdpOv", "MYDB");
$result = $mysqli->query("SELECT * from MYTABLE");
$row = $result->fetch_assoc();
Create a folder
.bp-config/options.json in the parent folder
and add
{
"PHP_EXTENSIONS": ["mysqli"]
}
in the options.json folder
the sqli connect will work fine now
Are you getting:
ERROR 2003 (HY000): Can't connect to MySQL server on '$host' (60).
A developer may have asked a similar question on developerWorks.
The answer seemed to be as follows:
Downloaded the latest PHPMyAdmin(4.1.9)
Created a BlueMix application using - cf push -b https://github.com/dmikusa-pivotal/cf-php-build-pack.git
${myPhpAdminApp} . Note: the PHP build pack is used is PHP 5.4.26, which enables multi-byte support (it is different from the Heroku one in the BlueMix docs). This was necessary because the Heroku pack bundled PHP 5.3.27 which
doesn't enable "multi-byte" character support by default. Multi-byte
support is required to be enabled by PHPMyAdmin apparently.
Added the existing MySQL service to this app. And picked the host, port, user, and password details from the VCAP_SERVICES
environment variable.
Copied config.sample.inc.php in the PHPMyAdmin to config.inc.php and added or modified the following lines in it based on the MySQL
service VCAP_SERVICES details picked in previous step -
$cfg['Servers'][$i]['host'] = 'host-ip-from-vcap_services';
$cfg['Servers'][$i]['port'] = 'port-from-vcap_services';
$cfg['Servers'][$i]['user'] = 'user-from-vcap_services';
$cfg['Servers'][$i]['password'] = 'password-from-vcap_services';
Pushed the updates using the above cf push ... again.

mssql_connect show blank page

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.

MySQL connections from PHP on IIS?

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";
?>

Categories