We help IT Professionals succeed at work.

Link failure

255 Views
Last Modified: 2017-03-13
Hi,
The connection to Database should be fine but I get

08S01: [Microsoft][ODBC SQL Server Driver]Communication link failure

on PHP page. Why?
Comment
Watch Question

Imran AliWeb Developer

Commented:
I think you are trying to connect MS SQL Server with wrong credentials.

Let me know what are the parameters for your sql_connect()?

Please post the PHP code to investigate further.

Thanks.
Loganathan NatarajanLAMP Developer
CERTIFIED EXPERT

Commented:
Please check whether you have allowed to establish connection (network issue) to the SQL server database server?  Looks like you don't have permission to connect.
Peter ChanProblem resolver

Author

Commented:
Thanks to all.
Is there any way to put any codes to validate if the connection is fine?
Imran AliWeb Developer

Commented:
Yes, if you have database credentials user below code to validate connection.

<?php
$serverName = "serverName\sqlexpress"; //serverName\instanceName
$connectionInfo = array( "Database"=>"dbName", "UID"=>"userName", "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));
}
?>

Open in new window

Peter ChanProblem resolver

Author

Commented:
Due to original problem, I put echo and sleep lines below. But I cannot capture relevant user name by the two lines.

<?
require_once("login_fns.php");
require_once("..\lib\db_fns.php");

ob_start();
session_start();
echo $username;
sleep(3);
if ($username && $passwd)
// they have just tried logging in
{
    if (login($username, $passwd))
    {
      // if they are in the database register the user id
      $valid_user = $username;
      session_register("valid_user");
	  $conn = df_conn();
	  $sql="select top 1 principal from user_acct where username='$username'";
	  $rst = odbc_exec($conn,$sql);
	  $qry = odbc_fetch_array($rst);
	  $pri=$qry[principal];
      session_register("pri");
	  $sql="select top 1 territory from user_acct where username='$username'";
	  $rst = odbc_exec($conn,$sql);
	  $qry = odbc_fetch_array($rst);
	  $g_area=$qry[territory];
	  session_register("g_area");
	  $g_multiarea = str_replace(",", chr(13), $g_area);
	  session_register("g_multiarea");
    } 

Open in new window

Imran AliWeb Developer

Commented:
This is because you are buffering output. Try commenting line # 5 to print output.
//ob_start();

Open in new window

Peter ChanProblem resolver

Author

Commented:
Sorry, how to further identify which line is leading to original problem?

<?
require_once("login_fns.php");
require_once("..\lib\db_fns.php");

//ob_start();
session_start();
echo $username;
sleep(3);
if ($username && $passwd)
// they have just tried logging in
{
    if (login($username, $passwd))
    {
      // if they are in the database register the user id
      $valid_user = $username;
      session_register("valid_user");
	  $conn = df_conn();
	  $sql="select top 1 principal from user_acct where username='$username'";
	  $rst = odbc_exec($conn,$sql);
	  $qry = odbc_fetch_array($rst);
	  $pri=$qry[principal];
      session_register("pri");
	  $sql="select top 1 territory from user_acct where username='$username'";
	  $rst = odbc_exec($conn,$sql);
	  $qry = odbc_fetch_array($rst);
	  $g_area=$qry[territory];
	  session_register("g_area");
	  $g_multiarea = str_replace(",", chr(13), $g_area);
	  session_register("g_multiarea");
    }  
    else
    {
      // unsuccessful login
      do_html_header("Problem:");
      echo "You could not be logged in. 
            You must be logged in to view this page.";
      do_html_url("login.php", "Login");
      do_html_footer();
      exit;
    }      
}

Open in new window

Imran AliWeb Developer

Commented:
Please post contents for included file.

require_once("..\lib\db_fns.php");

Open in new window

Peter ChanProblem resolver

Author

Commented:
Here is the file
<?
function df_conn()
{
   $result = @odbc_pconnect("sch0", "sahkg", "bcd567"); 
   if (!$result)
   {  echo 'Cannot connect to database.';
      return false;
	}
   return $result;
}

function runStoredProc($sql){ 

  $val = array();
  $con = df_conn();
  $rs = odbc_exec($con, $sql);

    while ( $row = odbc_fetch_array($rs) ){
        array_push($val,$row);
  }
  odbc_free_result ( $rs );
  odbc_close( $con );
  return $val;
} 

?>

Open in new window

Imran AliWeb Developer

Commented:
We need to check if connection to database is successful.

Try updating following function & check the output.
function df_conn()
{
   $result = @odbc_pconnect("sch0", "sahkg", "bcd567"); 
   if (!$result){
    exit("Connection Failed:" . odbc_errormsg() );
   }
   return $result;
}

Open in new window

Peter ChanProblem resolver

Author

Commented:
Sorry, I put these
function df_conn()
{
   $result = @odbc_pconnect("sch0", "sahkg", "bcd567"); 
   if (!$result)
   {  //echo 'Cannot connect to database.';
      //return false;
	exit("Connection Failed:" . odbc_errormsg() );
	}
   return $result;
}

Open in new window

but I do not get the error message, while I'm still with the original problem.
Imran AliWeb Developer

Commented:
Try to connect database using function
odbc_connect

Open in new window

Instead of
odbc_pconnect

Open in new window


If you are still having issues, you should also check if SQL Server(Instance) Services are running or not?
Peter ChanProblem resolver

Author

Commented:
Where do you see
odbc_pconnect

in my codes? I cannot see function is being used, to the codes.

BTW, the DB is running fine there and I have no problem at all to connect to that by SSMS (SS management studio).
Peter ChanProblem resolver

Author

Commented:
Hi Imran,
Good day.
Any other help?
Web Developer
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION
Imran AliWeb Developer

Commented:
I hope its working for you.
Thanks