Proper MSSQL query with PHP on IIS - php

I am new to the IIS and MS SQL world.
I have followed tutorials on how to form my connection to MS SQL and while I do not get any errors, I get a blank screen. Can someone tell me what I am doing wrong?
<?php
$db_server = "Server";
$db_name = "Database";
$connInfo = array( "Database"=>"$db_name");
$conn = sqlsrv_connect($db_server,$connInfo) or die( print_r( sqlsrv_errors(), true));
if( $conn ) {
echo "Connection established.<br />";
}
else {
echo "Connection could not be established.<br />";
die( print_r( sqlsrv_errors(), true));
}
?>
I am using a LAMP server so this is uncharted territory for me.

Related

php sql sever connection failed

$conn = sqlsrv_connect( "192.168.1.6",
array('Database' =>"epromis_test",
"UID"=>"php_test",
"PWD"=>"phptest#123") );
if ($conn)
{
echo "sucess";
}
else
{
echo "failed";
}
Why I failed to connect the database?
thanks in advance
Here is link to very good php documentation http://php.net/manual/en/function.sqlsrv-connect.php. I think you need to escape your # symbol in password. # becomes &commat. link on how to pass special chars https://brajeshwar.github.io/entities/
<?php
$serverName = "serverName\\sqlexpress"; //serverName\instanceName
$connectionInfo = array( "Database"=>"epromis_test", "UID"=>"php_test", "PWD"=>"phptest&commat123");
$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));
}
?>

Sql server, php [duplicate]

So I keep getting this error when I want to query something to the ms sql server..
The connection is made with the database but the queries seem to fail.
The error log contains this:
PHP Fatal error: Call to undefined function mssql_query()
The code on the php:
session_start();
include_once("connect.php");
if (isset($_POST['username'])) {
$username = $_POST['username'];
$password = $_POST['password'];
$sql = "SELECT * FROM test WHERE username='".$username."' AND password='".$password."'";
$res = mssql_query ($sql) or die(mssql_error());
if (mssql_num_rows($res) == 1) {
$row = mssql_fetch_assoc($res);
$_SESSION['uid'] = $row['id'];
$_SESSION['username'] = $row['Username'];
$_SESSION['afdeling'] = $row['Afdeling'];
$_SESSION['mail'] = $row['Mail'];
header("Location: test.php");
exit();
} else {
echo "Invalid login information. Please return to the previous page.";
exit(); } } ?>
Does anybody knows what the problem is?
Thanks in advance!
connect.php code:
<?php
$serverName = "MTN-TEST"; //serverName\instanceName
$connectionInfo = array( "Database"=>"PROCES_TEST", "UID"=>"blaaa", "PWD"=>"blooo");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn ) {
echo "<span style='color:green;'>Connection established.</span><br />";
}else{
echo "<span style='color:red;'>Connection could not be established.</span><br />";
die( print_r( sqlsrv_errors(), true));
}
?>
You don't have the MS SQL Drivers installed.
You can check this with phpinfo();
On Linux you need mssql.so or sybase.so
With debian its apt-get install php5-sybase
For windows take a look here:
http://msdn.microsoft.com/en-US/library/cc793139%28v=SQL.90%29.aspx
Drivers need to be configured for PHP to find the function mssql_...
You could also look at PDO DB classes as they can connect to any DBS, you need the drivers installed tho.
If your connect.php code returns "Connection established.", it's mean you installed MS SQL Drivers correctly.
You have to use sqlsrv_queryfunction instead of mssql_query.
The correct form of this command is:
<?php
$serverName = "serverName";
$options = array( "UID" => "sa", "PWD" => "Password", "Database" => "DBname");
$conn = sqlsrv_connect($serverName, $options);
if( $conn ) {
echo "Connection established.<br />";
$query='select * from test';
$result = sqlsrv_query($conn,$query);
}else{
echo "Connection could not be established.<br />";
die( print_r( sqlsrv_errors(), true));
}
?>
you can learn more here:
"PHP Fatal error: Call to undefined function mssql_select_db() in c:\...appscript.php on line 16"
you can use mssql_get_last_message() for mssql errors

SQL query into option form

I have a PHP code to get info from my Microsoft SQL server 2014, but it isnt working, the page it self works fine since it pops up as it should when i comment out the PHP code, but as soon as the PHP code isnt commented out, its just all white, so im assuming problem with the PHP code. I have to get the results from the query out into a drop down menu.
i use this code:
$servername = "VCCSQL03";
$username = "forecast";
$password = "Telefon2";
$dbname = "Forecast";
$connectionInfo = array("Database"=>$dbname, "UID"=>$username, "PWD"=>$password);
$conn = sqlsrv_connect($serverName, $connectionInfo);
if(!$conn) {
echo "Connection could not be established.<br />";
die( print_r( sqlsrv_errors(), true));
}
// Check connection
$result = sqlsrv_query($conn,"SELECT * FROM dbo.vw_BrandProduct");
if ($result->num_rows > 0) {
// output data of each row
while($row = sqlsrv_fetch_array($result)) {
echo "<option value='".$row['Brand_ProductID']."' name='".$row['Brand_ProductName']."'</option>";
}
} else {
echo "";
}
sqlsrv_close();
First and foremost, you do not have an open and closed select tag, and your option tags was missing a > to close it properly. Try the below revision, assuming connection is established on the page properly then this should work.
$connectionInfo = array( "Database"=>$dbname, "UID"=>$username, "PWD"=>$password);
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if(!$conn) {
//// Check connection
echo "Connection could not be established.<br />";
die( print_r( sqlsrv_errors(), true));
}
$result = sqlsrv_query($conn,"SELECT * FROM dbo.vw_BrandProduct");
if ($result->num_rows > 0) {
// output data of each row
echo "<select name='products'>";
while($row = sqlsrv_fetch_array($result)) {
echo "<option value='".$row['Brand_ProductID']."'>$row['Brand_ProductName']</option>";
}
echo "</select>";
} else {
echo ""; } sqlsrv_close(); ?>

Php - Call to undefined function mssql_query()

I have used the following php code to show the number of rows from sql table but I am getting below error message.
Fatal error: Call to undefined function mssql_query()
in D:\wamp\www\thereport24\gp.php
on line 22
my code:
$conn = odbc_connect('gpcon','','');
if ($conn)
{
$query= "select * from SubscriberServices where SubscriptionGroupID like 'ms_gp_tr24bn_3333'";
$results = mssql_query($query);
$rows = mssql_fetch_array($results);
echo $rows[0];
mssql_close($con);
}
If you use sql server, use this code. This works for me:
<?php
$serverName = "serverName";
$options = array( "UID" => "sa", "PWD" => "Password", "Database" => "DBname");
$conn = sqlsrv_connect($serverName, $options);
if( $conn ) {
echo "Connection established.<br />";
$query="select * from SubscriberServices where SubscriptionGroupID like 'ms_gp_tr24bn_3333'";
$result = sqlsrv_query($conn,$query);
sqlsrv_close($conn);
}else{
echo "Connection could not be established.<br />";
die( print_r( sqlsrv_errors(), true));
}
?>
if it returns "Connection established." it's mean you install MS SQL Drivers correct.

PHP Fatal error: Call to undefined function mssql_query()

So I keep getting this error when I want to query something to the ms sql server..
The connection is made with the database but the queries seem to fail.
The error log contains this:
PHP Fatal error: Call to undefined function mssql_query()
The code on the php:
session_start();
include_once("connect.php");
if (isset($_POST['username'])) {
$username = $_POST['username'];
$password = $_POST['password'];
$sql = "SELECT * FROM test WHERE username='".$username."' AND password='".$password."'";
$res = mssql_query ($sql) or die(mssql_error());
if (mssql_num_rows($res) == 1) {
$row = mssql_fetch_assoc($res);
$_SESSION['uid'] = $row['id'];
$_SESSION['username'] = $row['Username'];
$_SESSION['afdeling'] = $row['Afdeling'];
$_SESSION['mail'] = $row['Mail'];
header("Location: test.php");
exit();
} else {
echo "Invalid login information. Please return to the previous page.";
exit(); } } ?>
Does anybody knows what the problem is?
Thanks in advance!
connect.php code:
<?php
$serverName = "MTN-TEST"; //serverName\instanceName
$connectionInfo = array( "Database"=>"PROCES_TEST", "UID"=>"blaaa", "PWD"=>"blooo");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn ) {
echo "<span style='color:green;'>Connection established.</span><br />";
}else{
echo "<span style='color:red;'>Connection could not be established.</span><br />";
die( print_r( sqlsrv_errors(), true));
}
?>
You don't have the MS SQL Drivers installed.
You can check this with phpinfo();
On Linux you need mssql.so or sybase.so
With debian its apt-get install php5-sybase
For windows take a look here:
http://msdn.microsoft.com/en-US/library/cc793139%28v=SQL.90%29.aspx
Drivers need to be configured for PHP to find the function mssql_...
You could also look at PDO DB classes as they can connect to any DBS, you need the drivers installed tho.
If your connect.php code returns "Connection established.", it's mean you installed MS SQL Drivers correctly.
You have to use sqlsrv_queryfunction instead of mssql_query.
The correct form of this command is:
<?php
$serverName = "serverName";
$options = array( "UID" => "sa", "PWD" => "Password", "Database" => "DBname");
$conn = sqlsrv_connect($serverName, $options);
if( $conn ) {
echo "Connection established.<br />";
$query='select * from test';
$result = sqlsrv_query($conn,$query);
}else{
echo "Connection could not be established.<br />";
die( print_r( sqlsrv_errors(), true));
}
?>
you can learn more here:
"PHP Fatal error: Call to undefined function mssql_select_db() in c:\...appscript.php on line 16"
you can use mssql_get_last_message() for mssql errors

Categories