php sql sever connection failed - php

$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));
}
?>

Related

MS SQL statements work in DBeaver but not in PHP (with configuration and setup correct)

I have the following code to verify that my PHP-MS SQL connection and configuration are setup properly. I have about 100 getter functions that are all set up the same to retrieve info from this database:
function ms_getClientLN($repEnvKey) {
$repCustKey = '';
$serverName = "1.1.1.1\\EDGESQL"; //serverName\instanceName
$connectionInfo = array( "Database"=>"EDGESQL", "UID"=>"userid", "PWD"=>"password");
$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 = "SELECT repCustKey FROM repair WHERE repEnvKey LIKE '$repEnvKey';";
//echo $sql . "<br>";
$stmt = sqlsrv_query( $conn, $sql );
if( $stmt === false) {
die( print_r( sqlsrv_errors(), true) );
} else {
#echo "0 results";
}
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {
$repCustKey = $row['repCustKey'];
}
sqlsrv_close( $conn );
//----------===== Get ClientLast from customer table =====----------
$serverName = "1.1.1.1\\EDGESQL"; //serverName\instanceName
$connectionInfo = array( "Database"=>"EDGESQL", "UID"=>"userid", "PWD"=>"password");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
$clast = '';
if( $conn ) {
//echo "Connection established.<br />";
}else{
echo "Connection could not be established.<br />";
die( print_r( sqlsrv_errors(), true));
}
$sql = "SELECT cuLastName FROM customers WHERE cuKey LIKE '$repCustKey';";
//echo $sql . "<br>";
$stmt = sqlsrv_query( $conn, $sql );
if( $stmt === false) {
die( print_r( sqlsrv_errors(), true) );
} else {
#echo "0 results";
}
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {
$clast = $row['cuLastName'];
}
sqlsrv_close( $conn );
return $clast;
}
I then have the following function formatted exactly like the other working hundred that produces no results:
function ms_getRepairHistory($rhRepKey) {
$type = '';
$results[] = array();
$serverName = "1.1.1.1\\EDGESQL"; //serverName\instanceName
$connectionInfo = array( "Database"=>"EDGESQL", "UID"=>"userid", "PWD"=>"password");
$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 = "SELECT rhWhen, rhWhereNext, rhNotes FROM RepairHistory WHERE rhRepKey LIKE '$rhRepKey';";
echo $sql . "<br>";
$stmt = sqlsrv_query( $conn, $sql );
if( $stmt === false) {
die( print_r( sqlsrv_errors(), true) );
} else {
echo "0 results<br>";
}
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {
$results[] = $row;
}
sqlsrv_close( $conn );
return $row;
}
I'm even echoing the SQL command and can copy and paste it into DBeaver:
echo $sql . "<br>";
outputs:
SELECT rhWhen, rhWhereNext, rhNotes
FROM RepairHistory
WHERE rhRepKey LIKE '001-167896-001';
Which works in DBeaver with the
Databse.dbo.
prefix attached:

How to connect sql server to php

Hi I had a question about connecting php to sql server I install odbc driver and confige php.ini file. But when execute I get error.
I change the computer but still get same error.
<?php
$serverName = "YASSERAHMMED\YASOFT";
$connectionInfo = array( "Database"=>"ELECTERC_DB", "UID"=>"sa", "PWD"=>"google");
$con = sqlsrv_connect( $serverName, $connectionInfo );
if( $con === false ) {
//echo sqlsrv_errors();
die( print_r( sqlsrv_errors(), true));
}
$sql = "SELECT * FROM ACCOUNT_CUSTMERS";
$stmt = sqlsrv_query( $con, $sql );
if( $stmt === false) {
die( print_r( sqlsrv_errors(), true) );
}
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {
echo $row['date'].", ".$row['id']."<br />";
}
sqlsrv_free_stmt( $stmt);
?>
the result is :
error 5701 and 5703
I finally use PDO string connection to resolve the problem..
thanks every one...
$serverName = "YASSERAHMMED\YASOFT";
try
{
$conn = new PDO( "sqlsrv:server=$serverName ; Database=ELECTERC_DB", "", "");
$conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
if($conn){
// echo'ggggg';
} else{
echo'undone';
}
}
catch(Exception $e)
{
die( print_r( $e->getMessage() ) );
}

Proper MSSQL query with PHP on IIS

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.

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(); ?>

Not getting anything back from sqlsrv_num_rows

I'm trying pull back data from MS SQL via a php page. I have got a valid connection, and am trying a simple SELECT * FROM MyTable but sqlsrv_num_rows is just blank no matter what I do!!!
Here is my code:
function connect() {
$serverName = DB_HOST; //serverName\instanceName
$connectionInfo = array( "Database"=>DB_NAME, "UID"=>DB_USER, "PWD"=>DB_PASSWORD);
$this->connection = sqlsrv_connect( $serverName, $connectionInfo);
sqlsrv_connect( $serverName, $connectionInfo);
if( $this->connection ) {
echo "<br>Connection established.<br />";
}else{
echo "<br>Connection could not be established.<br />";
die( print_r( sqlsrv_errors(), true));
}
}
The echo for Connection established is working so all AOK there!
Now when I do a simple query:
function query($sql) {
if ($this->debug) {
echo $sql . "<br />";
}
$this->sql = $sql;
$this->recordset = sqlsrv_query($this->connection, $sql);
if (!$this->recordset) {
die('<br><br>Invalid query :<br><br><bold>' . $this->sql . '</bold><br><br>' . sqlsrv_errors());
}
echo "<br>rows = " . sqlsrv_num_rows($this->recordset);
I get absolutely nothing from the above echo? Any reason why? Or can you suggest a new echo I can try to debug this?
All my code in my DB class is converted from mysql so there may be a few bits wrong that is doing the damage!
I've even tried a super simple version, all the code together and it's still blank/false:
$server = DB_HOST;
$conn = sqlsrv_connect( $server, array( "Database"=>DB_NAME, "UID"=>DB_USER, "PWD"=>DB_PASSWORD) );
$stmt = sqlsrv_query( $conn, "SELECT * FROM MyTable");
$row_count = sqlsrv_num_rows($stmt);
echo "<br>row count = " . $row_count;
if ($row_count === false)
echo "\nerror\n";
else if ($row_count >=0)
echo "\n$row_count\n";
die;
Try this.....
Replace below statement
sqlsrv_query( $conn, "SELECT * FROM MyTable");
as
sqlsrv_query( $conn, "SELECT * FROM MyTable", array(), array("Scrollable"=>"buffered"));

Categories