12 Replies - 5115 Views - Last Post: 06 September 2012 - 07:10 AM Rate Topic: -----

#1 ersaan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 31-May 12

Warning: mssql_connect() [function.mssql-connect]: Unable to connect t

Posted 03 September 2012 - 09:37 AM

I'm trying to make log in form and i can make it but there is a problem that i made this with variables that i declare and write so in first page Data.html i put mu DATABASE USERNAME PASSWORD and afet that comes up UPDATE.php in which i fill my database but i lost my Credintials of login in this page so i cannot fill nothign ...

i don't how to to fix this i'm trying for 3 days and i can't find the problem .... why

PHP CODE DATA.PHP

<?php
session_start();

function llogedin(){
if (isset($_SESSION['server'])&& !empty ($_SESSION['Server']))
{   return true;}
else
{return false;}
}
$_SESSION['server']=$Server;
//Variablat per mu kyq ne SQLEXPRESS
$Server = $_POST['Server'];
$Username = $_POST['Username'];
$Password = $_POST['Password']; 
//FUKSIONI KU JON TE DEKLARUME VARIABLAT PER LOG QASJE NE MSSQL

function con()
{ global $Server,$Username,$Password,$con ;
		$con = mssql_connect ($Server,$Username,$Password) or die ("Wrond");
}

con();
mssql_select_db('Project1', $con);
$_SESSION['Server']=$Server;

header("Location: update.html");

?>


UPDATE.PHP

<?php 

 //session_start();
require  'C:\AppServ\www\1\data.php';
 //include 'C:\AppServ\www\1\data.php';
 
if (isset($_SESSION['server'])&& !empty ($_SESSION['Server']))
{   echo 'Welcome , '.$_SESSION['server'];}
else
{echo 'Please log in.';}

if(!isset($_SESSION['server']))
{
    die("To access this page, you need to <a href='login.php'>LOGIN</a>"); // Make sure they are logged in!
} 
//procedura n'SQL per futjen e te dhenave si FName LName edhe PHON
  $proc = mssql_init('dbo.insert_in_example',$con);
mssql_bind($proc,'@FName',$FName,SQLVARCHAR);
mssql_bind($proc,'@LName',$LName,SQLVARCHAR);
mssql_bind($proc,'@PHON',$PHON,SQLVARCHAR);

 $result=mssql_execute($proc);

    do {
        while ($row = mssql_fetch_array($result)) {
            echo "$row[0] -- $row[1] -- $row[2] -- $row[3]<BR>";
        }
    } while (mssql_next_result($result));
    mssql_free_result($result);

?>




HTML CODE DATA.HTML

<html> 
<head> 
<title>Login</title> 
</head> 
<body> 
<form method = "post" action ="data.php">
<table border="5"><td>
Server :<br/>
<input type="text" name="Server" size="30" /> <br/>

Username :<br/>
<input type="text" name="Username" size="30" /> <br/>

Password :<br/>
<input type="text" name="Password" size="30" /> <br/>




<input type="submit" value="Log In" />
</td>
</table>

</form> 
</body> 
</html>


update.html

<html> 
<head> 
<title>Update</title> 
</head> 
<body> 
<form method = "post" action ="update.php">
<table border="5"><td>
Name :<br/>
<input type="text" name="FName" size="30" /> <br/>

Last Name :<br/>
<input type="text" name="LName" size="30" /> <br/>

Phone Nr. :<br/>
<input type="text" name="PHON" size="30" /> <br/>

<input type="submit" value="Update" />
</td>
</table>

</form> 
</body> 
</html>




I put all the files in there is anybody that can help me to solve this problem .

Attached File(s)

  • Attached File  data.php (641bytes)
    Number of downloads: 27
  • Attached File  update.html (430bytes)
    Number of downloads: 28
  • Attached File  data.html (437bytes)
    Number of downloads: 23
  • Attached File  update.php (926bytes)
    Number of downloads: 26


Is This A Good Question/Topic? 1
  • +

Replies To: Warning: mssql_connect() [function.mssql-connect]: Unable to connect t

#2 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3730
  • View blog
  • Posts: 6,017
  • Joined: 08-June 10

Re: Warning: mssql_connect() [function.mssql-connect]: Unable to connect t

Posted 03 September 2012 - 10:42 AM

*
POPULAR

A few comments on your PHP code.
<?php

session_start();

// Where do you use this function? Why is it named using
// two "l" characters? (Obvious names make the code
// easier to read and understand.)
function llogedin()
{
    // Keep in mind that session variable names, like all
    // PHP variables and functions, are case sensitive.
    // 'Server' and 'server' are not equal!
    // By the way, the empty() function also checks if the
    // variable is set, so there is no need to call both these
    // functions.
    if (isset($_SESSION['server']) && !empty($_SESSION['Server'])) {
        return true;
    }
    else {
        return false;
    }
}

// At this point, $Server has no value, so all this does
// is clear out whatever value may have been in the
// session variable already.
$_SESSION['server'] = $Server;

// Note that normal, global variables will not persist
// between request. If you want to enter you database
// info on one page and the update info on another, you'll
// have to store this info in the session so you can use
// it on both pages. - However, a MUCH MUCH better approach
// is to store this data in a PHP file instead. Is there
// really any reson to enter it into a form? Are you using
// multiple database servers? Will this info change frequently?
// If not, then just type the info into the PHP file and
// remove the need to enter it into the form.
$Server = $_POST['Server'];
$Username = $_POST['Username'];
$Password = $_POST['Password'];


function con()
{
    // There is no point creating the above variables in
    // the global scope just to have to import them into
    // the function. Define them *in* the function instead.
    global $Server, $Username, $Password, $con;
    
    // The $con variable, even if the connection is successful
    // and assigned to it, will never be used. You don't use
    // it in the function and you don't return it, so it will
    // never be used.
    $con = mssql_connect($Server, $Username, $Password) or die("Wrond");
}

// Why do you create a function only to call it once
// right after it's definition? Why not just create
// the connection in the global scope. It's not like
// this is any safer.
con();

// Like I pointed out above, the $con variable only exists
// inside the function. You can't use it here. Also, why
// do you connect inside the function, but select the 
// database outside it?
mssql_select_db('Project1', $con);

$_SESSION['Server'] = $Server;

// What purpose does this server? By placing this here,
// you cause all pages that include this one to redirect
// to this update page. If you include this into the
// UPDATE.PHP page, this will redirect you to the specified
// HTML page instead.
header("Location: update.html");

// It's best to not close the PHP block unless you have some
// non-PHP content to add. If the page contains only PHP code
// then keep the block open.
?>


<?php

require 'C:\AppServ\www\1\data.php';

// Didn't you define a funtion in the "data.php" page that
// does this check?
if (isset($_SESSION['server']) && !empty($_SESSION['Server'])) {
    echo 'Welcome , ' . $_SESSION['server'];
}
else {
    echo 'Please log in.';
}

// Why are you doing this check again? You just did it above.
if (!isset($_SESSION['server'])) {
    die("To access this page, you need to <a href='login.php'>LOGIN</a>");
}

$proc = mssql_init('dbo.insert_in_example', $con);
mssql_bind($proc, '@FName', $FName, SQLVARCHAR);
mssql_bind($proc, '@LName', $LName, SQLVARCHAR);
mssql_bind($proc, '@PHON', $PHON, SQLVARCHAR);

$result = mssql_execute($proc);

do {
    while ($row = mssql_fetch_array($result)) {
        echo "$row[0] -- $row[1] -- $row[2] -- $row[3]<BR>";
    }
}
while (mssql_next_result($result));
mssql_free_result($result);
?>


Was This Post Helpful? 7
  • +
  • -

#3 ersaan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 31-May 12

Re: Warning: mssql_connect() [function.mssql-connect]: Unable to connect t

Posted 04 September 2012 - 04:50 AM

Thnx a lot ATLI i looked up your comment and it's was very helpful so i changed a lot of things , i'm ok with se session , appers another problem with StoreProcedure , i don't know really why ... here is the code . what i've done
The problem is :

Quote

Warning: mssql_init(): supplied argument is not a valid MS SQL-Link resource in C:\AppServ\www\1\update.php on line 6

Warning: mssql_bind(): supplied argument is not a valid MS SQL-Statement resource in C:\AppServ\www\1\update.php on line 7

Warning: mssql_bind(): supplied argument is not a valid MS SQL-Statement resource in C:\AppServ\www\1\update.php on line 8

Warning: mssql_bind(): supplied argument is not a valid MS SQL-Statement resource in C:\AppServ\www\1\update.php on line 9

Warning: mssql_execute(): supplied argument is not a valid MS SQL-Statement resource in C:\AppServ\www\1\update.php on line 11

Warning: mssql_fetch_array(): supplied argument is not a valid MS SQL-result resource in C:\AppServ\www\1\update.php on line 14

Warning: mssql_next_result(): supplied argument is not a valid MS SQL-result resource in C:\AppServ\www\1\update.php on line 17

Warning: mssql_free_result(): supplied argument is not a valid MS SQL-result resource in C:\AppServ\www\1\update.php on line 18


I don't know really why that's now appears to me ...


DATA.PHP
<?php
session_start();
$_SESSION['Server']=$_POST['Server'];
$_SESSION['Username']=$_POST['Username'];
$_SESSION['Password']=$_POST['Password'];
$_SESSION['Database']=$_POST['Database'];

$con = mssql_connect ($_SESSION['Server'],$_SESSION['Username'],$_SESSION['Password']) or die ("Wrond");
mssql_select_db($_SESSION['Database'], $con);
?>

<html> 
<head> 
<title>Update</title> 
</head> 
<body> 
<form method = "post" action ="update.php">
<table border="5"><td>
Name :<br/>
<input type="text" name="FName" size="30" /> <br/>

Last Name :<br/>
<input type="text" name="LName" size="30" /> <br/>

Phone Nr. :<br/>
<input type="text" name="PHON" size="30" /> <br/>

<input type="submit" value="Update" />
</td>
</table>

</form> 
</body> 
</html>





UPDATE.PHP
<?php 

 session_start();
 //require  'C:\AppServ\www\1\data.php';

 $proc = mssql_init('dbo.insert_in_example',$con);
mssql_bind($proc,'@FName',$FName,SQLVARCHAR);
mssql_bind($proc,'@LName',$LName,SQLVARCHAR);
mssql_bind($proc,'@PHON',$PHON,SQLVARCHAR);

 $result=mssql_execute($proc);

    do {
        while ($row = mssql_fetch_array($result)) {
            echo "$row[0] -- $row[1] -- $row[2] -- $row[3]<BR>";
        }
    } while (mssql_next_result($result));
    mssql_free_result($result);

?>



AGAIN THANKSSS A LOOTTTT FOR HELP :)

Was This Post Helpful? 0
  • +
  • -

#4 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 948
  • View blog
  • Posts: 2,357
  • Joined: 15-February 11

Re: Warning: mssql_connect() [function.mssql-connect]: Unable to connect t

Posted 04 September 2012 - 05:37 AM

Your "MS SQL-Link resource" is not valid meaning that the connection to your MS SQL database was unsuccessful. You should do error checking before attempting to use connection resource. mssql_connect returns FALSE if the connection is unsuccessful. A simple
$con = mssql_connect(....);
if(!$con)
{
    // connection unsuccessful
}


Instead of using POST variables which is an insecure way to pass sensitive unencrypted data to your server place the database credentials in a PHP file.
Was This Post Helpful? 1
  • +
  • -

#5 ersaan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 31-May 12

Re: Warning: mssql_connect() [function.mssql-connect]: Unable to connect t

Posted 04 September 2012 - 05:52 AM

View Postcodeprada, on 04 September 2012 - 05:37 AM, said:

Your "MS SQL-Link resource" is not valid meaning that the connection to your MS SQL database was unsuccessful. You should do error checking before attempting to use connection resource. mssql_connect returns FALSE if the connection is unsuccessful. A simple
$con = mssql_connect(....);
if(!$con)
{
    // connection unsuccessful
}


Instead of using POST variables which is an insecure way to pass sensitive unencrypted data to your server place the database credentials in a PHP file.



i don't know how to fix it or to place database credentials in PHP ... ?
Was This Post Helpful? 0
  • +
  • -

#6 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 948
  • View blog
  • Posts: 2,357
  • Joined: 15-February 11

Re: Warning: mssql_connect() [function.mssql-connect]: Unable to connect t

Posted 04 September 2012 - 06:02 AM

You can use either include, include_once, require, or require_once to add the contents of a file to your current script. You can view the control structures here.

You can use constants to hold the values of your database credentials. e.g.

credentials.php
define('SERVER', '.....');
define('USERNAME', '.....');
define('PASSWORD', '.....');




script.php
require_once 'credentials.php';

$con = mssql_connect(SERVER, USERNAME, PASSWORD);
if(!$con)
{
    // connection unsuccessful
}


Was This Post Helpful? 3
  • +
  • -

#7 ersaan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 31-May 12

Re: Warning: mssql_connect() [function.mssql-connect]: Unable to connect t

Posted 04 September 2012 - 06:11 AM

View Postcodeprada, on 04 September 2012 - 06:02 AM, said:

You can use either include, include_once, require, or require_once to add the contents of a file to your current script. You can view the control structures here.

You can use constants to hold the values of your database credentials. e.g.

credentials.php
define('SERVER', '.....');
define('USERNAME', '.....');
define('PASSWORD', '.....');




script.php
require_once 'credentials.php';

$con = mssql_connect(SERVER, USERNAME, PASSWORD);
if(!$con)
{
    // connection unsuccessful
}



There is a problem with that because in the HTLM form i should define which is my Server,Username,Password,Database with define i should define them exactly how it is like ddefine('SERVER', 'ERSAN-PC');
define('USERNAME', 'ERsaan');
define('PASSWORD', '****');
so this not the way how i want to declare my creditnlials in credentials.php i want to make it dynamic form and fill form with the detail and then if Credintials are ok connect if not type again ....
SOrry for my bad english .. :)...
Was This Post Helpful? 0
  • +
  • -

#8 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 948
  • View blog
  • Posts: 2,357
  • Joined: 15-February 11

Re: Warning: mssql_connect() [function.mssql-connect]: Unable to connect t

Posted 04 September 2012 - 06:25 AM

So do you expect the location of the database or server to change frequently?

If you really want to do it this way then the connection details are a bit different for MS SQL than MySQL. The server string is in the format of COMPUTER NAME\INSTANCE NAME. So basically if you're connecting to the default server instance it will be ERSAN-PC\SQLEXPRESS.

If it still doesn't work then you may want to attempt the connection over TCP/IP. Open the port 1433, which is SQL Server's default port number. Then go to your SQL Server Configuration Manager (right click My Computer > Manage, assuming you're on Windows). Proceed to SQL Server Network Configuration then Protocols for SQLEXPRESS and then enable TCP/IP. Finally restart your server.
Was This Post Helpful? 1
  • +
  • -

#9 ersaan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 31-May 12

Re: Warning: mssql_connect() [function.mssql-connect]: Unable to connect t

Posted 06 September 2012 - 03:42 AM

View Postcodeprada, on 04 September 2012 - 06:25 AM, said:

So do you expect the location of the database or server to change frequently?

If you really want to do it this way then the connection details are a bit different for MS SQL than MySQL. The server string is in the format of COMPUTER NAME\INSTANCE NAME. So basically if you're connecting to the default server instance it will be ERSAN-PC\SQLEXPRESS.

If it still doesn't work then you may want to attempt the connection over TCP/IP. Open the port 1433, which is SQL Server's default port number. Then go to your SQL Server Configuration Manager (right click My Computer > Manage, assuming you're on Windows). Proceed to SQL Server Network Configuration then Protocols for SQLEXPRESS and then enable TCP/IP. Finally restart your server.


I did it Ennable the TCP/IP but again the same problem ... I really don't know what it don't take the procedure I make the echo to ses if i'm logged in here and the it's show that i'm logged in but again i could'nt execute the procedure in PHP ...
DATA.PHP

<?php

session_start();
$_SESSION['Server']=$_POST['Server'];
$_SESSION['Username']=$_POST['Username'];
$_SESSION['Password']=$_POST['Password'];
$_SESSION['Database']=$_POST['Database'];


$con = mssql_connect ($_SESSION['Server'],$_SESSION['Username'],$_SESSION['Password']) or die ("Wrond");
mssql_select_db($_SESSION['Database'], $con);
echo $_SESSION['Server'];
echo $_SESSION['Username'];
echo $_SESSION['Password'];
echo $_SESSION['Database']; 

?>
<html> 
<head> 
<title>Update</title> 
</head> 
<body> 
<form method = "post" action ="update.php">
<table border="5"><td>
Name :<br/>
<input type="text" name="FName" size="30" /> <br/>

Last Name :<br/>
<input type="text" name="LName" size="30" /> <br/>

Phone Nr. :<br/>
<input type="text" name="PHON" size="30" /> <br/>

<input type="submit" value="Update" />
</td>
</table>

</form> 
</body> 
</html>





UPDATE.PHP
<?php 

 session_start();
 echo $_SESSION['Server'];
echo $_SESSION['Username'];
echo $_SESSION['Password'];
echo $_SESSION['Database'];

 $proc = mssql_init('dbo.insert_example',$con);
mssql_bind($proc,'@FName',$FName,SQLVARCHAR);
mssql_bind($proc,'@LName',$LName,SQLVARCHAR);
mssql_bind($proc,'@PHON',$PHON,SQLVARCHAR);

 $result=mssql_execute($proc);

    do {
        while ($row = mssql_fetch_array($result)) {
            echo "$row[0] -- $row[1] -- $row[2] -- $row[3]<BR>";
        }
    } while (mssql_next_result($result));
    mssql_free_result($result);


Quote

$Server COMPUTER NAME\INSTANCE NAME : ERSAN-PC\SQLEXPRESS
$Database [il] Database : Project1 [/1]
Warning: mssql_init(): supplied argument is not a valid MS SQL-Link resource in C:\AppServ\www\1\update.php on line 9

Warning: mssql_bind(): supplied argument is not a valid MS SQL-Statement resource in C:\AppServ\www\1\update.php on line 10

Warning: mssql_bind(): supplied argument is not a valid MS SQL-Statement resource in C:\AppServ\www\1\update.php on line 11

Warning: mssql_bind(): supplied argument is not a valid MS SQL-Statement resource in C:\AppServ\www\1\update.php on line 12

Warning: mssql_execute(): supplied argument is not a valid MS SQL-Statement resource in C:\AppServ\www\1\update.php on line 14

Warning: mssql_fetch_array(): supplied argument is not a valid MS SQL-result resource in C:\AppServ\www\1\update.php on line 17

Warning: mssql_next_result(): supplied argument is not a valid MS SQL-result resource in C:\AppServ\www\1\update.php on line 20

Warning: mssql_free_result(): supplied argument is not a valid MS SQL-result resource in C:\AppServ\www\1\update.php on line 21


What u think where is the problem now ... thanks for your help : ) What do u think that i should do now ???
Was This Post Helpful? 0
  • +
  • -

#10 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3730
  • View blog
  • Posts: 6,017
  • Joined: 08-June 10

Re: Warning: mssql_connect() [function.mssql-connect]: Unable to connect t

Posted 06 September 2012 - 05:36 AM

Where in "update.php" are you connecting to the database?

Each request is separate from the others. You can't go to "data.php" and connect to MSSQL there, and then submit a form to "update.php" and use that same connection to MSSQL in there. The connection must be made on ALL pages that want to use the connection.

If you seriously want to have the user submit the database info, and store that database info in the session to be used to connect to MSSQL each time, then you need to write a script that connects to MSSQL using the session info that is included in all pages that need a MSSQL connection. However, that script must also detect if the info isn't there and redirect to the MSSQL info form if it isn't. - You already seem to be aware of how the isset() and empty() functions work, so that shouldn't be a problem for you.

Basically, you'll want to do something like this:
<?php
// File: dbconnect.php

if (/* DB info is in session */) {
    // Connect to MSSQL
} else {
    // Save whichever URL you are currently on
    // in the session, and redirect to the
    // dbform.php page.
}


<?php
// File: dbform.php

if (/* DB info exists in POST */) {
    // Verify the info and save it in 
    // the session. Then redirect back to
    // the URL you were previously on: the
    // URL you saved in the session.
} else {
    // Print the DB info form
}


<?php
// File: update.php

require "dbconnect.php";

// Do whatever you needed to do with the MSSQL
// connection here.


Was This Post Helpful? 3
  • +
  • -

#11 BrairMoss  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 15
  • View blog
  • Posts: 81
  • Joined: 13-April 10

Re: Warning: mssql_connect() [function.mssql-connect]: Unable to connect t

Posted 06 September 2012 - 05:37 AM

You can try removing the 'dbo.' from line 9 of update.php and see what happens. Another issue is again, in your update.php file. You refer to the connection string $con on line 9 of update.php, however it is or should be unable to access that variable. I personally recommend creating a file called: connection.php or config.php that contains the connection to the database, and then using a require statement on all other php files that need to connect.

Let us know if there are any other issues you encounter.
//BrairMoss
Was This Post Helpful? 1
  • +
  • -

#12 ersaan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 31-May 12

Re: Warning: mssql_connect() [function.mssql-connect]: Unable to connect t

Posted 06 September 2012 - 06:30 AM

View PostBrairMoss, on 06 September 2012 - 05:37 AM, said:

You can try removing the 'dbo.' from line 9 of update.php and see what happens. Another issue is again, in your update.php file. You refer to the connection string $con on line 9 of update.php, however it is or should be unable to access that variable. I personally recommend creating a file called: connection.php or config.php that contains the connection to the database, and then using a require statement on all other php files that need to connect.

Let us know if there are any other issues you encounter.
//BrairMoss


There is the problem if i create dhe connection.php becaus i should make the conecction static . i want to make it dynamic to put my creditnals in html form and with that creditinals to log if it fails to fill again with the correct creditnals . Soo i think there is problem with the SESSION cuz when i put the creditinals i can log on but i think there is problem with session for reason that it's not saving my Creditinals and then i cannot proced the STORE PROCEDURE . i think there is the problem with SESSION . How should i make the session to be avaliable and to save the creditinals or session ... What do u think could this be the problem ... what do u suggest ?...
Was This Post Helpful? 1
  • +
  • -

#13 ersaan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 31-May 12

Re: Warning: mssql_connect() [function.mssql-connect]: Unable to connect t

Posted 06 September 2012 - 07:10 AM

View PostBrairMoss, on 06 September 2012 - 05:37 AM, said:

You can try removing the 'dbo.' from line 9 of update.php and see what happens. Another issue is again, in your update.php file. You refer to the connection string $con on line 9 of update.php, however it is or should be unable to access that variable. I personally recommend creating a file called: connection.php or config.php that contains the connection to the database, and then using a require statement on all other php files that need to connect.

Let us know if there are any other issues you encounter.
//BrairMoss



I Found the problem .

The problem was that i didn't call the function $con in the UPDATE.PHP :) so this problem is solved thank's for the HELP :) ...
i just put the
$con = mssql_connect ($_SESSION['Server'],$_SESSION['Username'],$_SESSION['Password']) or die ("Wrond");
mssql_select_db($_SESSION['Database'], $con);



and my problem is solved . thnxx again
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1