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?
LVL 12
HuaMin ChenProblem resolverAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Imran AliWeb DeveloperCommented:
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.
1
Loganathan NatarajanLAMP DeveloperCommented:
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.
0
HuaMin ChenProblem resolverAuthor Commented:
Thanks to all.
Is there any way to put any codes to validate if the connection is fine?
0
Protecting & Securing Your Critical Data

Considering 93 percent of companies file for bankruptcy within 12 months of a disaster that blocked access to their data for 10 days or more, planning for the worst is just smart business. Learn how Acronis Backup integrates security at every stage

Imran AliWeb DeveloperCommented:
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

1
HuaMin ChenProblem resolverAuthor 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

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

Open in new window

0
HuaMin ChenProblem resolverAuthor 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

0
Imran AliWeb DeveloperCommented:
Please post contents for included file.

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

Open in new window

0
HuaMin ChenProblem resolverAuthor 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

0
Imran AliWeb DeveloperCommented:
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

0
HuaMin ChenProblem resolverAuthor 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.
0
Imran AliWeb DeveloperCommented:
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?
0
HuaMin ChenProblem resolverAuthor 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).
0
HuaMin ChenProblem resolverAuthor Commented:
Hi Imran,
Good day.
Any other help?
0
Imran AliWeb DeveloperCommented:
Hi HuaMinChen,

You can find
odbc_pconnect

Open in new window

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

Open in new window

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

Open in new window


Try changing
odbc_pconnect

Open in new window

to
odbc_connect

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Imran AliWeb DeveloperCommented:
I hope its working for you.
Thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
SQL

From novice to tech pro — start learning today.