Adam Brave Posted January 21, 2013 Share Posted January 21, 2013 Hi, I want to make a login and a validation in a database. In order to do it I use the following code where: user -> table with all the user information like birthday, mail, username, first name etcpassword -> column of the table "user" that contains all the passwordsmail -> column of the table "user" that contains all the mails. The two variables, $maill and $passwordd, are use to store the information required at the login moment to the user. $result = mysql_query("SELECT password FROM user WHERE mail=$maill");$row = mysql_fetch_array($result); if ( $row['mail'] == $maill && $row['password'] == $passwordd ){ echo $row['name'] . " " . $row['ultimonome']; } else { echo "login not correct"; } Output: Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\Program Files\EasyPHP-12.1\www\welcome.php on line 29login not correct Can someone help me? Link to comment Share on other sites More sharing options...
justsomeguy Posted January 21, 2013 Share Posted January 21, 2013 That means the query failed. http://w3schools.invisionzone.com/index.php?showtopic=44106 Link to comment Share on other sites More sharing options...
Adam Brave Posted January 21, 2013 Author Share Posted January 21, 2013 From what I see, the information submitted by the user (mail=asd@hotmail.com and password=asd) have something wrongon the password. For example I printed the two variables and get this: <form action="welcome.php" method="post">mail: <input type="text" name="fmail">password: <input type="text" name="fpass"><input type="submit"></form>...$maill = $_POST['fmail'];$passwordd = $_POST['fpass'];echo $maill;echo "<br>";echo $passwordd; Output:asd@hotmail.comasdbool(false) From where come the "bool(false)" ? Link to comment Share on other sites More sharing options...
justsomeguy Posted January 21, 2013 Share Posted January 21, 2013 Unless that text was typed in the field, it doesn't look like it's coming from that code. That sounds like the output from var_dump if you try to var_dump the return from the query. Link to comment Share on other sites More sharing options...
Adam Brave Posted January 24, 2013 Author Share Posted January 24, 2013 I have changed the code to this: $maill = $_POST['fmail'];$passwordd = $_POST['fpass'];$mysqli = new mysqli("localhost",$maill,"");/* check connection */if ($mysqli->connect_errno) { printf("Connect failed: %s\n", $mysqli->connect_error); exit();}$mysqli->select_db("database.accdb");$result = $mysqli->query("SELECT * FROM user WHERE mail=$maill AND password=$passwordd");$row = $result->fetch_row();printf("Default database is %s.\n", $row[0]); And my output is: "Fatal error: Call to a member function fetch_row() on a non-object in C:\Program Files\EasyPHP-12.1\www\welcome.php on line 27" Line 27 : $row = $result->fetch_row(); Now, I'm sure that I have a row in my database with the mail and password stored on the variables $maill and $password and, I'm also sure, that these variables have the corresponding information so why can't I validate that user? Link to comment Share on other sites More sharing options...
boen_robot Posted January 24, 2013 Share Posted January 24, 2013 "database.accdb"?!?!?Are you trying to use MySQLi to access an MS Access (2007+) database? If so, that simply won't work.The fact you're not getting an object means the query failed, so $result is a boolean false. You can check the reason for the failure at the $error property, e.g. $maill = $_POST['fmail'];$passwordd = $_POST['fpass'];$mysqli = new mysqli("localhost",$maill,"");/* check connection */if ($mysqli->connect_errno) { printf("Connect failed: %s\n", $mysqli->connect_error); exit();}$mysqli->select_db("database.accdb");$result = $mysqli->query("SELECT * FROM user WHERE mail=$maill AND password=$passwordd");if (false === $result) { printf('Error %s: %s', $mysqli->errno, $mysqli->error);} else { $row = $result->fetch_row(); printf("Default database is %s.\n", $row[0]);} Link to comment Share on other sites More sharing options...
Adam Brave Posted January 24, 2013 Author Share Posted January 24, 2013 Well I was in fact trying to use an access database LOL that may be the problem.I wil try to change to a sql database then Link to comment Share on other sites More sharing options...
Adam Brave Posted January 24, 2013 Author Share Posted January 24, 2013 (edited) Solved $result = mysql_query(SELECT * FROM userdb WHERE mail LIKE '$maill' AND pass LIKE '$passwordd'); Btw, can someone tell me how do I load a html page with php code? In javascript I would use the instruction location.href but how do I load a page with php? Edited January 24, 2013 by Adam Brave Link to comment Share on other sites More sharing options...
thescientist Posted January 24, 2013 Share Posted January 24, 2013 (edited) location.href = "some-page.php" or just link to the PHP file with an <a> tag and just have the PHP file output the HTML you want. Edited January 24, 2013 by thescientist Link to comment Share on other sites More sharing options...
Adam Brave Posted January 25, 2013 Author Share Posted January 25, 2013 location.href = "some-page.php" or just link to the PHP file with an <a> tag and just have the PHP file output the HTML you want. That works in javascript but not in php. I want a instruction that works inside of the tags <?php ... ?> Link to comment Share on other sites More sharing options...
Ingolme Posted January 25, 2013 Share Posted January 25, 2013 In PHP you can load data from another file using include() but if you want a redirect, you can use a location header: header('Location: http://example.com/');exit; The specifications say URLs in the location header must be absolute and prepended by the http: or https: protocol.If you send a location header, it must be sent before any HTML or printed content. Link to comment Share on other sites More sharing options...
Adam Brave Posted January 25, 2013 Author Share Posted January 25, 2013 In PHP you can load data from another file using include() but if you want a redirect, you can use a location header:header('Location: http://example.com/');exit; The specifications say URLs in the location header must be absolute and prepended by the http: or https: protocol.If you send a location header, it must be sent before any HTML or printed content. Tkz, it worked Link to comment Share on other sites More sharing options...
guruparthi Posted July 7, 2014 Share Posted July 7, 2014 Try this <?php $mail=$_POST['mail']; $pwd=$_POST['password']; $q=mysql_query("select * from user where mail='".$mail."' and password='".$pwd."' ") or die(mysql_error()); $res=mysql_fetch_row($q); if($res) { header('location:home.php'); } else { echo'not valid'; } Reference: http://www.phponwebsites.com/2014/07/php-mysql-login-validation.html Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now