-2

This is my current code in config.php:

mysql_connect("localhost","roof") or die(mysql_error());
mysql_select_db("tarclibrary") or die(mysql_error());

this is my current code in register.php:

require('config.php');

if(isset($_POST["submit"])){

//Perform the verification of the nation
$email1 =$_POST['email1'];
$email2 =$_POST['email2'];
$pass1 =$_POST['pass1'];
$pass2 =$_POST['pass2'];

if($email1 == $email2){
    if($pass1 == $pass2){
        //All good.carry on

        $fname = mysql_real_escape_string($_POST['fname']);
        $lname = mysql_real_escape_string($_POST['lname']);
        $uname = mysql_real_escape_string($_POST['uname']);
        $pass1 = mysql_real_escape_string($pass1);
        $pass2 = mysql_real_escape_string($pass2);
        $email1 = mysql_real_escape_string($email1); 
        $email2 = mysql_real_escape_string($email2);

        $pass1= md5($pass1);

        $sql = mysql_query("SELECT * FROM 'users' WHERE 'uname' = '$uname'");
        if(mysql_num_rows($sql) > 0){
            echo "Sorry, that user already exits";
            exit();
        }

        mysql_select_db('library') or die(mysql_error());

        mysql_query("INSERT INTO users (id, fname, lname, uname, pass, email) VALUES (NULL, '$fname', '$lname', '$uname'. '$pass1', '$email1')") or die (mysql_error());


    }else{
        echo "sorry,your passwords do not match.<br/>";
        exit();
    }
}else{
    echo "sorry your email's do not match<br/><br/>";
}
}else{

$form = <<<EOT

<h1><center>Register</center></h1>
<br>
<form action="register.php" method="post">
<table class="t1" align="center" width="600" height="400">
<tr>
   <th scope="row">First name</th>
   <td><input type="text" name="fname" required type="text"/></td>
</tr>
<tr>
   <th scope="row">Last name</th>
   <td><input type="text" name="lname" required type="text"/></td>
</tr>
<tr>
   <th scope="row">Username</th>
   <td><input type="text" name="uname" required type="text"/></td>
</tr>
<tr>
   <th scope="row">Password</th>
   <td><input type="password" name="pass1" required type="text" /></td>
</tr>
<tr>
   <th scope="row">Confirm Password</th>
   <td><input type="password" name="pass2" required type="text"/></td>
</tr>
  <tr>
   <th scope="row">Email</th>
   <td><input type="text" name="email1" required type="text"/></td>
</tr>
<tr>
   <th scope="row">Confirm Email</th>
   <td><input type="text" name="email2" required type="text"/></td>
  </tr>
</table>
<input id="register" type="submit" value="Register" name="submit"/>
</form>

EOT;

echo $form;

}

when I submit my register the output is:

Access denied for user ''@'localhost' to database 'library'

someone help pls , why is this happened??

Racil Hilan
  • 24,690
  • 13
  • 50
  • 55
Yu Wil Son
  • 119
  • 2
  • 11
  • 8
    Your password or user is wrong. Maybee you mean `root` and not `roof`? – Tobias Nov 19 '15 at 06:45
  • 1
    update > mysql_connect("localhost","root", "") or die(mysql_error()); – CodeLove Nov 19 '15 at 06:46
  • 1
    If the typing mistake is the problem, this question itself is not eligible to ask. – Sanjay Kumar N S Nov 19 '15 at 06:50
  • Possible duplicate of [MySQL ERROR 1045 (28000): Access denied for user 'bill'@'localhost' (using password: YES)](https://stackoverflow.com/questions/10299148/mysql-error-1045-28000-access-denied-for-user-billlocalhost-using-passw) – KhAn SaAb Mar 28 '18 at 14:08

4 Answers4

6

Try to connect with following

mysql_connect("localhost","root", '') or die(mysql_error())

MYSQL is Deprecated So please use mysqli

Mitul
  • 3,431
  • 2
  • 22
  • 35
1

Try to the following using mysqli

$mysqli = new mysqli("localhost", "root", "", "db_name");

/* check connection */
if ($mysqli->connect_errno) {
    printf("Connect failed: %s\n", $mysqli->connect_error);
}
fool-dev
  • 7,671
  • 9
  • 40
  • 54
0

try with this.

mysql_connect("localhost","root", "") or die(mysql_error());
//replace third param with your actual password

mysql_connect is deprecated.. or use

$con = mysqli_connect("localhost","root","","tarclibrary");
// To Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
Sarath
  • 2,318
  • 1
  • 12
  • 24
  • if there is no password just left empty remain quate ?after installed xampp haven't setup any setting yet and the syntax that you suggested is correct it in register.php or config.php ?? – Yu Wil Son Nov 19 '15 at 07:34
  • yes if password is empty then you mention it as "" ,and better you make connection in config – Sarath Nov 19 '15 at 07:36
  • tried with first suggestion output = Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\tarclibrary\register.php on line 92 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '. '08f8e0260c64418510cefb2b06eee5cd', 'bbb')' at line 1 – Yu Wil Son Nov 19 '15 at 07:42
  • replace mysql_connect("localhost","root", "password") or die(mysql_error()); to suggestion 2 output = Access denied for user ''@'localhost' to database 'tarclibrary' – Yu Wil Son Nov 19 '15 at 07:44
  • try with mysql_connect("localhost","root", "") and not mysql_connect("localhost","root", "password") because your password is empty as you said – Sarath Nov 19 '15 at 07:47
  • yes , the code is and output is Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\tarclibrary\register.php on line 92 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '. '08f8e0260c64418510cefb2b06eee5cd', 'bbb')' at line 1 – Yu Wil Son Nov 19 '15 at 08:00
  • and second change mysql_connect("localhost","root", "") or die(mysql_error()); to $con = mysqli_connect("localhost","root","","tarclibrary"); // To Check connection if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } become Access denied for user ''@'localhost' to database 'tarclibrary' – Yu Wil Son Nov 19 '15 at 08:01
0

You will connect to db as follows

     $link=mysql_connect("localhost", "root","password");
     mysql_select_db('tarclibrary', $link);
     $sql = "INSERT into users values('', '$fname', '$lname', '$uname'. '$pass1', '$email1')";
    $res=mysql_query($sql);
Santosh Patel
  • 549
  • 2
  • 13
  • what is the '$company_name' means and the empty quate – Yu Wil Son Nov 19 '15 at 09:52
  • this is my custom value you will insert your value. – Santosh Patel Nov 19 '15 at 09:53
  • add this to register or config.php ?? – Yu Wil Son Nov 19 '15 at 10:08
  • first two lines added in config.php and last two lines add in register – Santosh Patel Nov 19 '15 at 10:17
  • ok , is there i need to remove any current code ?like mysql_connect("localhost","root", '') or die(mysql_error()) or just insert this in – Yu Wil Son Nov 19 '15 at 10:25
  • if you have the database password then put it at the place of passwrd. otherwise put the code as it is. – Santosh Patel Nov 19 '15 at 10:27
  • now my code for is config.php $link=mysql_connect("localhost", "root",""); mysql_select_db('tarclibrary', $link); mysql_select_db("tarclibrary") or die(mysql_error()); – Yu Wil Son Nov 19 '15 at 10:41
  • in register php i remove mysql_query("INSERT INTO users (id, fname, lname, uname, pass, email) VALUES (NULL, '$fname', '$lname', '$uname'. '$pass1', '$email1')") or die (mysql_error()); – Yu Wil Son Nov 19 '15 at 10:42
  • and replace with $sql = "INSERT into users values('id', '$fname', '$lname', '$uname'. '$pass1', '$email1')"; $res=mysql_query($sql); – Yu Wil Son Nov 19 '15 at 10:42
  • when click register go to a blank pages it's succeed ? but in myadminphp ,database taclibrary users table no any browse record created. – Yu Wil Son Nov 19 '15 at 10:44
  • you just put the first two line in config.php and last two line in register as it is. – Santosh Patel Nov 19 '15 at 10:45
  • without remove my code and put the code that you mentioned , after click register result is Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\tarclibrary\register.php on line 92 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '. 'eab71244afb687f16d8c4f5ee9d6ef0e', 'nn')' at line 1 – Yu Wil Son Nov 19 '15 at 10:51
  • firstly remove your code for database connection and insert query. – Santosh Patel Nov 19 '15 at 10:53
  • how to ? what mean ? i'am beginer in php – Yu Wil Son Nov 19 '15 at 10:57
  • mysql_connect("localhost","roof") or die(mysql_error()); mysql_select_db("tarclibrary") or die(mysql_error()); Replace above two line with my first two line. And mysql_query("INSERT INTO users (id, fname, lname, uname, pass, email) VALUES (NULL, '$fname', '$lname', '$uname'. '$pass1', '$email1')") or die (mysql_error()); the above line with my last two line – Santosh Patel Nov 19 '15 at 10:58
  • and remove mysql_select_db('library') or die(mysql_error()); from register page. – Santosh Patel Nov 19 '15 at 11:01
  • ok, done now still have Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\tarclibrary\register.php on line 92 – Yu Wil Son Nov 19 '15 at 11:07
  • problem is in your $sql = mysql_query("SELECT * FROM 'users' WHERE 'uname' = '$uname'"); query. – Santosh Patel Nov 19 '15 at 11:11
  • what problem in here ? which syntax wrong ? – Yu Wil Son Nov 19 '15 at 11:15
  • mysql_num_rows change to mysql_query ?? – Yu Wil Son Nov 19 '15 at 11:16
  • mysql_query() or die(mysql_error()); to find the problem. – Santosh Patel Nov 19 '15 at 11:25
  • code now is $sql = mysql_query("SELECT * FROM 'users' WHERE 'uname' = '$uname'")or die(mysql_error()); if(mysql_num_rows($sql) > 0){ echo "Sorry, that user already exits"; exit(); } – Yu Wil Son Nov 19 '15 at 11:32
  • output becomes You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''users' WHERE 'uname' = 'nn'' at line 1 – Yu Wil Son Nov 19 '15 at 11:32
  • Try This $sql = mysql_query("SELECT * FROM users WHERE uname = '$uname'"); – Santosh Patel Nov 19 '15 at 11:34
  • i add pass = pass1 Now code is $sql = mysql_query("SELECT * FROM users WHERE uname = '$uname' AND pass = '$pass1'") or die(mysql_error()); if(mysql_num_rows($sql) > 0){ echo "Sorry, that user already exits"; exit(); } – Yu Wil Son Nov 19 '15 at 11:37
  • after refresh blank page out – Yu Wil Son Nov 19 '15 at 11:38
  • in myadminphp database tarclibrary and go to users table no query result created – Yu Wil Son Nov 19 '15 at 11:39
  • no error after submit come out a blank page . what does this mean , succeed? – Yu Wil Son Nov 19 '15 at 18:15
  • check your table if data will be inserted then ok. – Santosh Patel Nov 20 '15 at 04:33
  • Downvoted for 2 reasons: 1) This code is terrible (No error checking/handling, building SQL queries by string concatenation, etc) and it will also not work in PHP 7+. 2) Answering low-quality questions will only encourage more low-quality questions – GordonM Mar 28 '18 at 10:16