Link to home
Start Free TrialLog in
Avatar of Peter Chan
Peter ChanFlag for Hong Kong

asked on

Link failure

Hi,
The connection to Database should be fine but I get

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

on PHP page. Why?
Avatar of Imran Ali
Imran Ali
Flag of Pakistan image

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.
Avatar of Loganathan Natarajan
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.
Avatar of Peter Chan

ASKER

Thanks to all.
Is there any way to put any codes to validate if the connection is fine?
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

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

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

Open in new window

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

Please post contents for included file.

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

Open in new window

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

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

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.
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?
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).
Hi Imran,
Good day.
Any other help?
ASKER CERTIFIED SOLUTION
Avatar of Imran Ali
Imran Ali
Flag of Pakistan image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I hope its working for you.
Thanks