Hi guys i'm new to PHP, I have been doing a simple login system using php, and now I'm face with this error "Could not select database because Access denied for user ''@'localhost' to database 'loginsystem'"

anyone knows how to fix this?

to find out a little info about what is happening, change these bits of code:

Where you connect to the db :
mysql_connect("localhost","user","pass") or die(mysql_error());
Change pass to your password

Where you select the DB : 
mysql_select_db("dbname") or die(mysql_error());
Change dbname to your dbname.

If you still get this error, run this SQL query in PHPMyAdmin, The MySQL Command Line tool, or any other thing that can run MySQL Commands:

GRANT ALL ON *.* TO 'user'@'localhost';

It usually means you have provided invalid username and password into mysql_connect(). This may be because the user does not exist or the mysql server is currently down but when in doubt copy and paste the login details you last used for another site of yours on the same server (eg your master login).

i have a new error says, "Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in checklogin.php on line 47
Wrong Username or Password"

this is a piece of my code:


echo mysql_error();

$host="localhost"; // Host name

$username="root"; // Mysql username

$password=""; // Mysql password

$db_name="test"; // Database name

$tbl_name="members"; // Table name

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

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

echo mysql_error();

//username, and password sent from form 



// To protect MySQL injection (more detail about MySQL injection)

$username = stripslashes($username);

$password = stripslashes($password);

$username = mysql_real_escape_string($username);

$password = mysql_real_escape_string($password);

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


// Mysql_num_row is counting table row


// If result matched $username and $password, table row must be 1 row



// Register $username, $password, and redirect to file "login_success.php"





else {

echo "Wrong Username or Password";



Line 41 should be as follows:

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

another question, what is the code for welcoming the user? for exam if the user is john and login is successful it should display Welcome john!

At the position you want that text to display add the following:

echo htmlentities($_POST['username']);

i tried this one <h2>welcome <?php echo htmlentities($_POST); ?>
but it's not displaying



As pOst variables are destroyed when the code on line 61.

$_SESSION = $result;

and don't forget to put the following at the start of each page with the $_SESSION variable.

<?php session_start(); //nothing before here - line 1

hi i couldn't display it :(
this is my code

<div class="content"><!--start of content -->

    <h2 style="background-color: #FC0; text-align: left; font-family: verdana;">Welcome
   $_SESSION['username'] = $result['username']; 
    <p class="smallp">Hello there! Thanks for visiting our website. We're looking forward to hear from you. We have a guestbook on
    the left navigation bar so you can interact with us and tell and ask us about your visit on our website.
    You are free to suggest and give us
    reasonable feedbacks. We are proud B-Meg, Purina, and Pilmico retailer. Thus, you can be sure to have
    your animals to feed quality products.</p>


Ln 5, you are defining the session again.
To get the value of a session remove everything after, and the equals sign.

this is my first code


<div class="sidebar2">
    <fieldset style="width:132px">
    <legend>Login Form</legend>
      <FORM name="form1" action="checklogin.php" method="post">
        <font size="2.5px">Username: </font>
        <INPUT name="username" type="text" size="16" id="username"/><br />
        <font size="2.5px">Password: </font>
        <INPUT type="password" name="password" size="16" id="password"><br />
        <INPUT type=submit value="Log In" />
        <INPUT type=reset value="Cancel" />
        Don`t have an account? Sign Up <a href="http://localhost/nps/register.php">here</a>

and my logincheck.php


 <h2 style="background-color: #FC0; text-align: left; font-family: verdana;">Welcome

but it still not display the user