Log In Script Problem

Thread Solved

Join Date: Jul 2008
Posts: 44
Reputation: phouse512 is an unknown quantity at this point 
Solved Threads: 1
phouse512 phouse512 is offline Offline
Light Poster

Log In Script Problem

 
0
  #1
Oct 27th, 2009
Hello, I've been using a book to learn php, and when I try to make it work for my own use, there seems to be an error when I try to login. First it says that the log in is invalid, and it comes up with this warning message:

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in C:\Program Files\wamp\www\loginsite\login.php on line 25

  1. if (!empty($user_username) && !empty($user_password)) {
  2. // Look up the username and password in the database
  3. $query = "SELECT user_id, username FROM mismatch_user WHERE username = '$user_username' AND password = SHA('$user_password')";
  4. $data = mysqli_query($dbc, $query);
  5.  
  6. //below is line 25 of code
  7. if (mysqli_num_rows($data) == 1) {
  8. // The log-in is OK so set the user ID and username session vars (and cookies), and redirect to the home page
  9. $row = mysqli_fetch_array($data);
  10. $_SESSION['user_id'] = $row['user_id'];

I can post more code if I need to

Thanks
Last edited by phouse512; Oct 27th, 2009 at 9:32 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 38
Reputation: Froger93 is an unknown quantity at this point 
Solved Threads: 4
Froger93 Froger93 is offline Offline
Light Poster
 
0
  #2
Oct 28th, 2009
Okay usually I use supressants on mysqli_num_rows as if there is 0 rows it will return this error.

Try running a query that you know deffinetly exists in your database.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 557
Reputation: network18 is an unknown quantity at this point 
Solved Threads: 64
network18 network18 is offline Offline
Posting Pro
 
0
  #3
Oct 28th, 2009
think of using -
if (mysqli_num_rows($data) !=0 || mysqli_num_rows($data)!=FALSE)
{
}
"The discipline of writing something down is the first step towards making it happen."

follow me on twitter
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 2
Reputation: roycowebdesign is an unknown quantity at this point 
Solved Threads: 1
roycowebdesign roycowebdesign is offline Offline
Newbie Poster
 
0
  #4
Oct 28th, 2009
why exatly are you using mysqli and not simply mysql?
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 44
Reputation: phouse512 is an unknown quantity at this point 
Solved Threads: 1
phouse512 phouse512 is offline Offline
Light Poster
 
0
  #5
Oct 28th, 2009
Froger93: I don't understand what you really mean. Could you explain a little more?

Network18: I tried replacing the line with what you did, but I still got two errors this time:

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in C:\Program Files\wamp\www\loginsite\login.php on line 25

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in C:\Program Files\wamp\www\loginsite\login.php on line 25

But they appear to be the same.

EDIT: I fixed the error because of a stupid mistake, but now, I still can't login, when I inputed the username/password myself using MySQL console.
Last edited by phouse512; Oct 28th, 2009 at 4:24 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 38
Reputation: Froger93 is an unknown quantity at this point 
Solved Threads: 4
Froger93 Froger93 is offline Offline
Light Poster
 
0
  #6
Oct 29th, 2009
Mine and networks solutions do the same thing but make sure you use the @ infront of the num_rows functions incase the query didn't return any rows, this is most likely happening because the query didn't return any results.

As I said before try inputing data into the form that you know really does exist in your database.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 557
Reputation: network18 is an unknown quantity at this point 
Solved Threads: 64
network18 network18 is offline Offline
Posting Pro
 
0
  #7
Oct 29th, 2009
its fine and using @ in front of the mysql_num_rows() will supress any warning you getting.
"The discipline of writing something down is the first step towards making it happen."

follow me on twitter
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 44
Reputation: phouse512 is an unknown quantity at this point 
Solved Threads: 1
phouse512 phouse512 is offline Offline
Light Poster
 
0
  #8
Nov 1st, 2009
I tried what you just said, but it still does not work. To show you, here is the data for the database, but I don't know why it won't work:

mysql> select * FROM login;
+---------+---------------+-------------+--------+
| user_id | username | password | name |
+---------+---------------+-------------+--------+
| 1 | phouse512 | ---------- | Philip |
| 2 | admin | admin | admin|
+---------+---------------+-------------+--------+
2 rows in set (0.00 sec)

Here's the login code:

  1. <?php
  2. require_once('connectvars.php');
  3.  
  4. // Start the session
  5. session_start();
  6.  
  7. // Clear the error message
  8. $error_msg = "";
  9.  
  10. // If the user isn't logged in, try to log them in
  11. if (!isset($_SESSION['user_id'])) {
  12. if (isset($_POST['submit'])) {
  13. // Connect to the database
  14. $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
  15.  
  16. // Grab the user-entered log-in data
  17. $user_username = mysqli_real_escape_string($dbc, trim($_POST['username']));
  18. $user_password = mysqli_real_escape_string($dbc, trim($_POST['password']));
  19.  
  20. if (!empty($user_username) && !empty($user_password)) {
  21. // Look up the username and password in the database
  22. $query = "SELECT user_id, username FROM login WHERE username = '$user_username' AND password = SHA('$user_password')";
  23. $data = mysqli_query($dbc, $query);
  24.  
  25. if (@mysqli_num_rows($data) == 1) {
  26. // The log-in is OK so set the user ID and username session vars (and cookies), and redirect to the home page
  27. $row = mysqli_fetch_array($data);
  28. $_SESSION['user_id'] = $row['user_id'];
  29. $_SESSION['username'] = $row['username'];
  30. setcookie('user_id', $row['user_id'], time() + (60 * 60 * 24 * 30)); // expires in 30 days
  31. setcookie('username', $row['username'], time() + (60 * 60 * 24 * 30)); // expires in 30 days
  32. $home_url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . '/index.php';
  33. header('Location: ' . $home_url);
  34. }
  35. else {
  36. // The username/password are incorrect so set an error message
  37. $error_msg = 'Sorry, you must enter a valid username and password to log in.';
  38. }
  39. }
  40. else {
  41. // The username/password weren't entered so set an error message
  42. $error_msg = 'Sorry, you must enter your username and password to log in.';
  43. }
  44. }
  45. }
  46. ?>
  47.  
  48. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  49. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  50. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  51. <head>
  52. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  53. <title>Mismatch - Log In</title>
  54. <link rel="stylesheet" type="text/css" href="style.css" />
  55. </head>
  56. <body>
  57. <h3>Mismatch - Log In</h3>
  58.  
  59. <?php
  60. // If the session var is empty, show any error message and the log-in form; otherwise confirm the log-in
  61. if (empty($_SESSION['user_id'])) {
  62. echo '<p class="error">' . $error_msg . '</p>';
  63. ?>
  64.  
  65. <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
  66. <fieldset>
  67. <legend>Log In</legend>
  68. <label for="username">Username:</label>
  69. <input type="text" name="username" value="<?php if (!empty($user_username)) echo $user_username; ?>" /><br />
  70. <label for="password">Password:</label>
  71. <input type="password" name="password" />
  72. </fieldset>
  73. <input type="submit" value="Log In" name="submit" />
  74. </form>
  75.  
  76. <?php
  77. }
  78. else {
  79. // Confirm the successful log-in
  80. echo('<p class="login">You are logged in as ' . $_SESSION['username'] . '.</p>');
  81. }
  82. ?>
  83.  
  84. </body>
  85. </html>
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 1,102
Reputation: ardav will become famous soon enough ardav will become famous soon enough 
Solved Threads: 139
ardav's Avatar
ardav ardav is offline Offline
Veteran Poster
 
-1
  #9
Nov 1st, 2009
  1. $data = mysqli_query($dbc, $query);

Should this be:

  1. $data = mysqli_query($query, $dbc);

Having not used mysqli - I can't comment, but I think the first parameter should be the query, then the connection link identifier.
If you don't reply to your own thread or you can't find the solved link - you're off my Christmas list - permanently! Bah humbug!
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 525
Reputation: Will Gresham is on a distinguished road 
Solved Threads: 86
Sponsor
Will Gresham's Avatar
Will Gresham Will Gresham is offline Offline
Posting Pro
 
1
  #10
Nov 1st, 2009
Originally Posted by ardav View Post
  1. $data = mysqli_query($dbc, $query);

Should this be:

  1. $data = mysqli_query($query, $dbc);

Having not used mysqli - I can't comment, but I think the first parameter should be the query, then the connection link identifier.
Nope, for some reason they swapped them for mysqli, although if no connection is specified, it will default to that last connection that was established I believe.
http://www.php.net/manual/en/mysqli.query.php
AJAX is not a programming language, scripting language or any other sort of language.
It is acheived by using JavaScript http functions.
So, AJAX = JavaScript.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the PHP Forum


Views: 581 | Replies: 13
Thread Tools Search this Thread



Tag cloud for PHP
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC