Member Avatar for vijiraghs

I am writing a simple program where i have a HTML page that gets the username and password from the user and sends it to a PHP page. The PHP page connects to an MS access database and retrieves the stored password, compares it with the password given by the user and redirects the user appropriately. The following is the code.

<?php

$username = $_GET["fname"];
$userpass = $_GET["lname"];

$conn = odbc_connect('db1','','');
echo "hello     ";
if (!$conn)
  {exit("Connection Failed: " . $conn);}
 echo "hello2";
$sql="SELECT * FROM login where `username`= $username";
$rs=odbc_exec($conn,$sql);
if (!$rs)
  {exit("Error in SQL");}

odbc_fetch_row($rs);
$dbpass = odbc_result($rs,"password");
if(strcmp($dbpass,$userpass))
    header( 'Location: mainpage.html' );
else
    header( 'Location: index.html' );
odbc_close($conn);

?>

The following is my error.

Warning: odbc_connect() [function.odbc-connect]: SQL error: Failed to fetch error message, SQL state HY000 in SQLConnect in C:\wamp\www\MyFiles\hello.php on line 6

what have I done wrongly??

Please help.

Member Avatar for vijiraghs

the first parameter that I have mentioned is the DSN. I have kept the DSN and the database name the same. Is something else the problem??

I gave 2 echo statements to check where things are going wrong. The first echo statement is getting executed while the second one is not. I dont know why i am not able to establish the connection.

Actually, I know how to get the job done using servlets. But I dont know how to deploy servlets in WAMP. Nor am I able to run tomcat and WAMP through different ports. Also, i dont know anything in PHP. Hence, this confusion.

Please help. I want this code to work :(

The second one is only echoed if the connection fails. The same with fetch row. Show your new code without the comments (use code tags). It looks like a logic error.

Member Avatar for vijiraghs
<?php

$username = $_GET["fname"];
$userpass = $_GET["lname"];

$conn = odbc_connect('db1','','');
echo "hello	 	";
if (!$conn)
  {exit("Connection Failed: " . $conn);}
 echo "hello2";
$sql="SELECT * FROM login where username=";
$rs=odbc_exec($conn,$sql);
if (!$rs)
  {exit("Error in SQL");}
 
odbc_fetch_row($rs);
$dbpass = odbc_result($rs,"password");
if(strcmp($dbpass,$userpass))
	header( 'Location: mainpage.html' );
else
	header( 'Location: index.html' );
odbc_close($conn);

?>

And how does your DSN look ? Because I still see 'db1', and that can't be correct.

Should look like this:

$connection = odbc_connect("Driver={Microsoft Access Driver (*.mdb)};Dbq=$mdbFilename", $user, $password);
Member Avatar for vijiraghs

i changed that. That line is working. But my

$sql="SELECT * FROM login where `username`= $username";

statement is showing error. How should I modify this statement??

Member Avatar for vijiraghs

This is what I want to do.


I want to check if the username and password entered by the user match. My database name is "db1". My table name is "login". It has two fields namely "username" and "password".

Can you please modify the code so that it works properly??

Are you sure MS Access supports those backticks, thought that was MySQL specific.

Member Avatar for vijiraghs

I dont know. I started learning PHP yesterday and I really have no clue how to debug the code.

Try this:

$sql = "SELECT * FROM login where username = '$username'";

It's not PHP, that's SQL dialect specific, in this case MS Access.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.