944,028 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 1226
  • PHP RSS
You are currently viewing page 1 of this multi-page discussion thread
Oct 27th, 2009
0

Log In Script Problem

Expand Post »
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

php Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
phouse512 is offline Offline
75 posts
since Jul 2008
Oct 28th, 2009
0
Re: Log In Script Problem
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.
Reputation Points: 11
Solved Threads: 6
Junior Poster in Training
Froger93 is offline Offline
74 posts
since Sep 2009
Oct 28th, 2009
0
Re: Log In Script Problem
think of using -
if (mysqli_num_rows($data) !=0 || mysqli_num_rows($data)!=FALSE)
{
}
Reputation Points: 29
Solved Threads: 76
Practically a Master Poster
network18 is offline Offline
616 posts
since Sep 2009
Oct 28th, 2009
0
Re: Log In Script Problem
why exatly are you using mysqli and not simply mysql?
Reputation Points: 10
Solved Threads: 1
Newbie Poster
roycowebdesign is offline Offline
2 posts
since Oct 2009
Oct 28th, 2009
0
Re: Log In Script Problem
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.
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
phouse512 is offline Offline
75 posts
since Jul 2008
Oct 29th, 2009
0
Re: Log In Script Problem
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.
Reputation Points: 11
Solved Threads: 6
Junior Poster in Training
Froger93 is offline Offline
74 posts
since Sep 2009
Oct 29th, 2009
0
Re: Log In Script Problem
its fine and using @ in front of the mysql_num_rows() will supress any warning you getting.
Reputation Points: 29
Solved Threads: 76
Practically a Master Poster
network18 is offline Offline
616 posts
since Sep 2009
Nov 1st, 2009
0
Re: Log In Script Problem
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:

php Syntax (Toggle Plain Text)
  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>
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
phouse512 is offline Offline
75 posts
since Jul 2008
Nov 1st, 2009
-1
Re: Log In Script Problem
PHP Syntax (Toggle Plain Text)
  1. $data = mysqli_query($dbc, $query);

Should this be:

PHP Syntax (Toggle Plain Text)
  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.
Sponsor
Featured Poster
Reputation Points: 1061
Solved Threads: 951
Disgraced Poster
ardav is offline Offline
6,708 posts
since Oct 2006
Nov 1st, 2009
1
Re: Log In Script Problem
Click to Expand / Collapse  Quote originally posted by ardav ...
PHP Syntax (Toggle Plain Text)
  1. $data = mysqli_query($dbc, $query);

Should this be:

PHP Syntax (Toggle Plain Text)
  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
Reputation Points: 96
Solved Threads: 124
Master Poster
Will Gresham is offline Offline
728 posts
since May 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Search string is too exact help
Next Thread in PHP Forum Timeline: Is the a correct url regex?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC