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?

Recommended Answers

All 14 Replies

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:

<?php

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 

$username=$_POST['username'];

$password=$_POST['password'];


// 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'";

$result=mysql_query($sql);

// Mysql_num_row is counting table row

$count=mysql_num_rows($result);

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

if($count==1)

{

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

session_register("username");

session_register("password");

header("location:loginsuccess.php");

}

else {

echo "Wrong Username or Password";

}

?>

Line 41 should be as follows:

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

thank you!

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

Use

$_SESSION['username']

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

$_SESSION = $result;

$_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
   <?php
   $_SESSION['username'] = $result['username']; 
    ?>
    </h2>
    <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>

</div>

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

home.php

<?php
session_start();
?>
........
<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 />
        <center>
        <INPUT type=submit value="Log In" />
        <INPUT type=reset value="Cancel" />
        </center>
        </FORM>
        Don`t have an account? Sign Up <a href="http://localhost/nps/register.php">here</a>
        
    </fieldset>

and my logincheck.php

<?php
session_start();
if(!session_is_registered(username)){
header("location:home.php");
}
?>........

 <h2 style="background-color: #FC0; text-align: left; font-family: verdana;">Welcome
   <?php
   $result['username']; 
    ?>
    </h2>

but it still not display the user

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.