Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- How to get the values in a database table to an array using php code?
- $query = "...";
- $sql = mysql_query($query);
- while($row = mysql_fetch_assoc($sql)) {
- $user_id = $row['user_id'];
- // ...
- }
- $username = mysql_real_escape_string($_POST['username']); //escaping the string, will use it in a query
- $password = $_POST['password'];
- $result = mysql_query("SELECT password FROM users WHERE username = '$username'");
- if (mysql_num_rows($result) == 1) { // Expecting one result
- $userdata = mysql_fetch_array($result);
- if ($password == $userdata['password']) {
- // USER IS AUTHORISED
- } else {
- // USER IS NOT AUTHORISED
- }
- }
- $username="username";
- $password="password";
- //Specify MySQL database to connect to
- $database="database";
- //Create connection and select the appropriate database
- mysql_connect("localhost",$username,$password);
- @mysql_select_db($database) or die( "Unable to select database");
- //Get the username and password from posted form
- $username = mysql_real_escape_string($_POST['username']);
- $password = mysql_real_escape_string($_POST['password']);
- //Build the query to look for users that exist with that username/password combination
- //Here I'm using the users table within my database
- $query = "SELECT * FROM users where username="$username" and password = "$password" ";
- //Retrieve the results of the query, and also aquire the number of records returned
- $result = mysql_query($query);
- $num = mysql_numrows($result);
- //Check to see how many records are returned
- if ($num>0)
- {
- //User login was successful
- echo 'success login';
- } else {
- //Login was unsuccessful
- echo 'user name not found';
- }
- <form method="post">
- <input type="text" name="username" />
- <input type="password" name="password" />
- <input type="submit" name="login" value="login"/>
- </form>
- if(isset($_POST['login']))
- {
- $username=$_POST['username'];
- $password=$_POST['password'];
- $username=mysql_real_escape_string($username);
- $password=mysql_real_escape_string($password);
- $query = "select username,password from tableName
- where username='$username' and password='$password'";
- $sql = mysql_query($query);
- if(mysql_num_rows($sql) == 1)
- {
- // Login success save username in session redirect to homepage
- header("location:homepage.php");
- exit();
- }
- else
- {
- // Display error message
- }
- }
Add Comment
Please, Sign In to add comment