Jump to content

Can't connect to MySQL from my login.php, please help!


Recommended Posts

Hello all,

 

I am brand new to PHP and MySQL. I have created my login page and necessary php files to check the password and authenticate. If I execute my script directly with PHP, i.e. C:\php>php.exe check_login.php, then the connection to MySQL db is successful, but when I am using my login page from my web server, then it doesn't work. (I can't even get the log where to trace and see the error being thrown).

 

Here's what I have done:

 

- Created MySQL table "users" and have an entry in it.

- Created login.php as follow:

 

<table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form name="form1" method="post" action="check_login.php">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td colspan="3"><strong>Member Login </strong></td>
</tr>
<tr>
<td width="78">Username</td>
<td width="6">:</td>
<td width="294"><input name="myusername" type="text" id="myusername">
</td>
</tr>
<tr>
<td>Password</td>
<td>:</td>
<td><input name="mypassword" type="text" id="mypassword"></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td><input type="submit" name="Submit" value="Login"></td>
</tr>
</table>
</td>
</form>
</tr>
</table>

 

- created check_login.php

<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password="admin"; // Mysql password
$db_name="wsc"; // Database name
$tbl_name="users"; // Table name

// Connect to server and select database.
mysql_connect($host, $username, $password)or die("cannot connect");

mysql_select_db($db_name)or die("cannot select DB");

// username and password sent from form
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];

// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);

$sql="SELECT * FROM " . $tbl_name . " WHERE username='" . $myusername . "' and password='" . $mypassword . "'";

$result=mysql_query($sql);

$count=mysql_num_rows($result);

if($count==1){
session_register($myusername);
session_register($mypassword);
header("location:login_success.php");
}
else {
echo "Wrong Username or Password";
}
?>

 

- created login_success.php

<?
session_start();
if(!session_is_registered(myusername)){
header("location:main_login.php");
}
?>
<html>
<body>
Login Successful
</body>
</html>

- created [b]logout.php[/b]
[code]
<?
session_start();
session_destroy();
?>

 

When I enter my user name/password, then it gets redirected to check_login.php, but i see a blank screen. If it's successful, i should see "Log in Successful" message, as I have in my login_success.php.

 

 

When I execute this script directly from php as follow: (C:\php\php.exe test.php) then I get success message.

<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password="admin"; // Mysql password
$db_name="wsc"; // Database name
$tbl_name="users"; // Table name

// Connect to server and select database.
mysql_connect($host, $username, $password)or die("cannot connect");

echo "Connected";

mysql_select_db($db_name)or die("cannot select DB");


echo "DB selected";

// username and password sent from form
$myusername="wsc_user";
$mypassword="wsc";

// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);

$sql="SELECT * FROM " . $tbl_name . " WHERE username='" . $myusername . "' and password='" . $mypassword . "'";

echo $sql;

$result=mysql_query($sql);

$count=mysql_num_rows($result);

if($count==1){

echo "sucess";
session_register($myusername);
session_register($mypassword);
//header("location:login_success.php");
}
else {
echo "Wrong Username or Password";
}
?>

 

 

Can someone please help? Really appreciate it.

 

 

Well I only glanced at the code and I already see a couple of things.

It's obvious that you do not have error_reporting on.

so place these two line at the top of the PHP code and fix any errors that you receive.

 

error_reporting(-1);
ini_set('display_errors', 'On');

Thanks Kay for quick reply. I did add these two lines, and here's the error I get

 

Fatal error: Call to undefined function mysql_connect() in E:\Program Files\Apache Group\Apache2\htdocs\test\check_login.php on line 13 

 

and line 13 in my check_login.php is the mysql_connect() statement, as:

 

mysql_connect($host, $username, $password)or die("cannot connect");

 

it appears that mysql_connect() can't be found? do I need to do an include_once() to some files or something?

 

Thanks again for help...

I did follow the other notes, and I made sure all those points that were suggested, are done. But still I am getting this error. Here's what I have:

 

php.ini

- C:\php5\php.ini (is being used, I can see my changes via phpinfo() command.

- extension_dir = "C:\php5\ext" (C:\php5 is my PHP installation directory)

- extension=php_mysql.dll  (removed simicolon)

- libmysql.dll is copied to C:\WINDOWS and C:\php5\ext

- C:\php5 is in my path system variable.

- extension=php_mysql.dll

 

not sure what else am I missing? Can someone please help!!!!!

 

Thanks.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.