Guest User

Untitled

a guest
Aug 1st, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.45 KB | None | 0 0
  1. How to get the values in a database table to an array using php code?
  2. $query = "...";
  3. $sql = mysql_query($query);
  4. while($row = mysql_fetch_assoc($sql)) {
  5. $user_id = $row['user_id'];
  6. // ...
  7. }
  8.  
  9. $username = mysql_real_escape_string($_POST['username']); //escaping the string, will use it in a query
  10. $password = $_POST['password'];
  11.  
  12. $result = mysql_query("SELECT password FROM users WHERE username = '$username'");
  13.  
  14. if (mysql_num_rows($result) == 1) { // Expecting one result
  15. $userdata = mysql_fetch_array($result);
  16. if ($password == $userdata['password']) {
  17. // USER IS AUTHORISED
  18. } else {
  19. // USER IS NOT AUTHORISED
  20. }
  21. }
  22.  
  23. $username="username";
  24. $password="password";
  25. //Specify MySQL database to connect to
  26. $database="database";
  27. //Create connection and select the appropriate database
  28. mysql_connect("localhost",$username,$password);
  29. @mysql_select_db($database) or die( "Unable to select database");
  30.  
  31. //Get the username and password from posted form
  32. $username = mysql_real_escape_string($_POST['username']);
  33. $password = mysql_real_escape_string($_POST['password']);
  34.  
  35. //Build the query to look for users that exist with that username/password combination
  36. //Here I'm using the users table within my database
  37. $query = "SELECT * FROM users where username="$username" and password = "$password" ";
  38. //Retrieve the results of the query, and also aquire the number of records returned
  39. $result = mysql_query($query);
  40. $num = mysql_numrows($result);
  41.  
  42. //Check to see how many records are returned
  43. if ($num>0)
  44. {
  45. //User login was successful
  46.  
  47. echo 'success login';
  48. } else {
  49. //Login was unsuccessful
  50. echo 'user name not found';
  51. }
  52.  
  53. <form method="post">
  54. <input type="text" name="username" />
  55. <input type="password" name="password" />
  56. <input type="submit" name="login" value="login"/>
  57. </form>
  58.  
  59. if(isset($_POST['login']))
  60. {
  61. $username=$_POST['username'];
  62. $password=$_POST['password'];
  63.  
  64. $username=mysql_real_escape_string($username);
  65. $password=mysql_real_escape_string($password);
  66. $query = "select username,password from tableName
  67. where username='$username' and password='$password'";
  68. $sql = mysql_query($query);
  69.  
  70. if(mysql_num_rows($sql) == 1)
  71. {
  72. // Login success save username in session redirect to homepage
  73. header("location:homepage.php");
  74. exit();
  75. }
  76. else
  77. {
  78. // Display error message
  79. }
  80. }
Add Comment
Please, Sign In to add comment