Peter Chan
asked on
Link failure
Hi,
The connection to Database should be fine but I get
on PHP page. Why?
The connection to Database should be fine but I get
08S01: [Microsoft][ODBC SQL Server Driver]Communication link failure
on PHP page. Why?
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.
ASKER
Thanks to all.
Is there any way to put any codes to validate if the connection is fine?
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));
}
?>
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");
}
This is because you are buffering output. Try commenting line # 5 to print output.
//ob_start();
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;
}
}
Please post contents for included file.
require_once("..\lib\db_fns.php");
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;
}
?>
We need to check if connection to database is successful.
Try updating following function & check the output.
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;
}
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;
}
but I do not get the error message, while I'm still with the original problem.
Try to connect database using function
If you are still having issues, you should also check if SQL Server(Instance) Services are running or not?
odbc_connect
Instead ofodbc_pconnect
If you are still having issues, you should also check if SQL Server(Instance) Services are running or not?
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).
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).
ASKER
Hi Imran,
Good day.
Any other help?
Good day.
Any other help?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I hope its working for you.
Thanks
Thanks
Let me know what are the parameters for your sql_connect()?
Please post the PHP code to investigate further.
Thanks.