Avatar of Peter Chan
Peter Chan
Flag 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?
SQLPHPWeb DevelopmentWeb ServersProgramming Languages-Other

Avatar of undefined
Last Comment
Imran Ali

8/22/2022 - Mon
Imran Ali

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 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.
Peter Chan

ASKER
Thanks to all.
Is there any way to put any codes to validate if the connection is fine?
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Imran Ali

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 Chan

ASKER
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 Ali

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

Open in new window

Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Peter Chan

ASKER
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 Ali

Please post contents for included file.

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

Open in new window

Peter Chan

ASKER
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

I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Imran Ali

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 Chan

ASKER
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 Ali

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?
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Peter Chan

ASKER
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 Chan

ASKER
Hi Imran,
Good day.
Any other help?
ASKER CERTIFIED SOLUTION
Imran Ali

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Imran Ali

I hope its working for you.
Thanks
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck