By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,713 Members | 1,243 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,713 IT Pros & Developers. It's quick & easy.

Issue With Simple Php Login Script - Mysql Error

P: 12
Hi,

I have been playing around with a simple php login script and im getting an error message when i attempt to log in with the username and password i set in the sql table. The error message is as follows:

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/greenpos/public_html/test4/checklogin.php on line 28
Wrong Username or Password

The code:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $host="localhost"; // Host name 
  3. $username="greenpos_admin1"; // Mysql username 
  4. $password="carlo"; // Mysql password 
  5. $db_name="greenpos_test"; // Database name 
  6. $tbl_name="greenpos_test.members"; // Table name 
  7.  
  8. // Connect to server and select databse.
  9. mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
  10. mysql_select_db("$db_name")or die("cannot select DB");
  11.  
  12. // username and password sent from signup form 
  13. $myusername=$_POST['myusername']; 
  14. $mypassword=$_POST['mypassword']; 
  15.  
  16. $sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
  17. $result=mysql_query($sql);
  18.  
  19. // Mysql_num_row is counting table row
  20. $count=mysql_num_rows($result);
  21. // If result matched $myusername and $mypassword, table row must be 1 row
  22.  
  23. if($count==1){
  24. // Register $myusername, $mypassword and redirect to file "login_success.php"
  25. session_register("myusername");
  26. session_register("mypassword"); 
  27. header("location:login_success.php");
  28. }
  29. else {
  30. echo "Wrong Username or Password";
  31. }
  32. ?>
--

I had created a database in sql called greenpos_test and created a table called members.
Expand|Select|Wrap|Line Numbers
  1. CREATE TABLE `members` (
  2. `id` INT( 4 ) NOT NULL ,
  3. `myusername` VARCHAR( 65 ) NOT NULL ,
  4. `mypassword` VARCHAR( 65 ) NOT NULL 
  5. ) ENGINE = MYISAM ;
in the table i created 2 fields, myusername and mypassword and each field i put username as jason and password as jason. EG:

i put in these details in the table:

myusername - type (varchar(65) - no function - no null - and value = jason
mypassword - type (varchar(65) - no function - no null - and value = jason

If that makes any sense.

Please can someone tell me why it wont allow me to login in with the my username and password.

Regards,
Jason
[REMOVED]
Jul 24 '07 #1
Share this Question
Share on Google+
20 Replies


dafodil
100+
P: 392
Are you using PHP 4.1.0 or higher?

The use of session_register() is depreciated

This is the preferred way....
Expand|Select|Wrap|Line Numbers
  1. $_SESSION['zim'] = "An invader from another planet.";
Write this:
Expand|Select|Wrap|Line Numbers
  1. session_register("myusername")
like this:
Expand|Select|Wrap|Line Numbers
  1. $_SESSION['myusername']=$myusername;
same on the password


And make sure you put
Expand|Select|Wrap|Line Numbers
  1. session_start();
before you try to access or store Session varables.

It's preferred to put session_start() at the beginning of your php code




Please also put a exit; after you redirect a page to ensure that it will redirect to the page.

Like this:
Expand|Select|Wrap|Line Numbers
  1. header("location:login_success.php");
  2. exit;
Jul 24 '07 #2

P: 12
Hi thanks soooo much for the reply.

i have done what you asked.

below is the code:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. $host="localhost"; // Host name 
  4. $username="greenpos_admin1"; // Mysql username 
  5. $password="carlo"; // Mysql password 
  6. $db_name="greenpos_test"; // Database name 
  7. $tbl_name="members"; // Table name 
  8.  
  9. // Connect to server and select databse.
  10. mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
  11. mysql_select_db("$db_name")or die("cannot select DB");
  12.  
  13. // username and password sent from signup form 
  14. session_start();
  15. $myusername=$_POST['myusername']; 
  16. $mypassword=$_POST['mypassword']; 
  17.  
  18. $sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
  19. $result=mysql_query($sql);
  20.  
  21. // Mysql_num_row is counting table row
  22. $count=mysql_num_rows($result);
  23. // If result matched $myusername and $mypassword, table row must be 1 row
  24.  
  25. if($count==1){
  26. // Register $myusername, $mypassword and redirect to file "login_success.php"
  27. $_SESSION['myusername']=$myusername;
  28. $_SESSION['mypassword']=$mypassword;
  29. header("location:login_success.php");
  30. exit;
  31. }
  32. else {
  33. echo "Wrong Username or Password";
  34. }
  35. ?>
--

THE ERROR MESSAGE APPEARING NOW IS:

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /home/greenpos/public_html/test4/checklogin.php:9) in /home/greenpos/public_html/test4/checklogin.php on line 22

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/greenpos/public_html/test4/checklogin.php:9) in /home/greenpos/public_html/test4/checklogin.php on line 22

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/greenpos/public_html/test4/checklogin.php on line 30
Wrong Username or Password

please if you can please reply!!.

thanks so much.
jason
Jul 24 '07 #3

dafodil
100+
P: 392
Can you try this code:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. $host="localhost"; // Host name 
  4. $username="greenpos_admin1"; // Mysql username 
  5. $password="carlo"; // Mysql password 
  6. $db_name="greenpos_test"; // Database name 
  7. $tbl_name="members"; // Table name 
  8.  
  9. // Connect to server and select databse.
  10. mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
  11. mysql_select_db("$db_name")or die("cannot select DB");
  12.  
  13. // username and password sent from signup form 
  14.  
  15. $myusername=$_POST['myusername']; 
  16. $mypassword=$_POST['mypassword']; 
  17.  
  18. $sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
  19. $result=mysql_query($sql);
  20.  
  21. // Mysql_num_row is counting table row
  22. $count=mysql_num_rows($result);
  23. // If result matched $myusername and $mypassword, table row must be 1 row
  24.  
  25. if($count==1){
  26. // Register $myusername, $mypassword and redirect to file "login_success.php"
  27. session_start();
  28. $_SESSION['myusername']=$myusername;
  29. $_SESSION['mypassword']=$mypassword;
  30. header("location:login_success.php");
  31. exit;
  32. }
  33. else {
  34. header("location: invalid.php");
  35. exit;
  36. }
  37. ?>
  38.  
Please create an invalid.php page...
Jul 24 '07 #4

P: 12
Hi Thanks once again!!!,
you have been great.

i modified the code to your code.

i still get the error message:

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/greenpos/public_html/test4/checklogin.php on line 30
Wrong Username or Password

although i didnt add the invalid.php but i didnt think that mattered.

i think my problem lies in my table members..

this is what i added via a sql command to the members table in the greenpos_test database:
Expand|Select|Wrap|Line Numbers
  1. CREATE TABLE `test_mysql` (
  2. `myusername` varchar(65) NOT NULL default '',
  3. `mypassword` varchar(65) NOT NULL default '',
  4. PRIMARY KEY (`myusername`)
  5. ) TYPE=MyISAM AUTO_INCREMENT=0 ;
Do you think that has anything to do with it?

cheers,
jason.
Jul 24 '07 #5

P: 12
this is what i put in the username and password fields in the members table:

INSERT INTO `members` VALUES (jason', 'jason');

?
Jul 24 '07 #6

dafodil
100+
P: 392
Replace this code:
Expand|Select|Wrap|Line Numbers
  1. // Connect to server and select databse.
  2. mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
  3. mysql_select_db("$db_name")or die("cannot select DB");
  4.  
with this:

Expand|Select|Wrap|Line Numbers
  1. // Connect to server and select databse.
  2. $link=mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
  3. mysql_select_db($db_name, $link)or die("cannot select DB");
  4.  
It should work now...


Please always put :

mysql_close( $link );

after you use the database. It's a good practice
Jul 24 '07 #7

P: 12
Hi,

nope that didnt work either..

error message:


Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/greenpos/public_html/test4/checklogin.php on line 30
Wrong Username or Password
Jul 24 '07 #8

dafodil
100+
P: 392
Did you already set this connections on your db?

$host="localhost"; // Host name
$username="greenpos_admin1"; // Mysql username
$password="carlo"; // Mysql password

on the config.inc
Jul 24 '07 #9

P: 12
just to let you know i am using cpanel on my own server
in the mysql section in cpanel this info comes up:

Users in greenpos_test
greenpos_admin1 (Privileges: ALL PRIVILEGES)
Connection Strings
Perl $dbh = DBI->connect("DBI:mysql:greenpos_test:localhost","gree npos_admin1","<PASSWORD HERE>");
PHP $dbh=mysql_connect ("localhost", "greenpos_admin1", "<PASSWORD HERE>") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("greenpos_test");

so i guess i have added the correct username/password for that database etc?

i dont know, im new to this, i think i have done something wrong somewhere in the setup..

cheers,
thanks for getting back to me so quick.
Jul 24 '07 #10

dafodil
100+
P: 392
just to let you know i am using cpanel on my own server
in the mysql section in cpanel this info comes up:

Users in greenpos_test
greenpos_admin1 (Privileges: ALL PRIVILEGES)
Connection Strings
Perl $dbh = DBI->connect("DBI:mysql:greenpos_test:localhost","gree npos_admin1","<PASSWORD HERE>");
PHP $dbh=mysql_connect ("localhost", "greenpos_admin1", "<PASSWORD HERE>") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("greenpos_test");

so i guess i have added the correct username/password for that database etc?

i dont know, im new to this, i think i have done something wrong somewhere in the setup..

cheers,
thanks for getting back to me so quick.
Set the password to carlo according to your code.
Jul 24 '07 #11

P: 12
i have.. i just cut and copied that from cpanel to show that i have setup the database and made the user and set the password and gave the user all rights.

ill reset the password and see if this helps.

any other ideas?

cheers mate.
Jul 24 '07 #12

P: 12
ok so i reset the user and re-added greenpos_admin1 with password carlo and i get this error now:

Parse error: syntax error, unexpected T_OBJECT_OPERATOR in /home/greenpos/public_html/test4/checklogin.php on line 18


Any ideas?

Thanks for your help.
----

CODE BELOW:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. $host="localhost"; // Host name 
  4. $username="greenpos_admin1"; // Mysql username 
  5. $password="carlo"; // Mysql password 
  6. $db_name="greenpos_test"; // Database name 
  7. $tbl_name="members"; // Table name 
  8.  
  9. // Connect to server and select databse.
  10. $dbh = DBI->connect("DBI:mysql:greenpos_test:localhost","greenpos_admin1","<carlo>"); 
  11. PHP $dbh=mysql_connect ("localhost", "greenpos_admin1", "<carlo>") or die ('I cannot connect to the database because: ' . mysql_error());
  12. mysql_select_db ("greenpos_test");  
  13.  
  14.  
  15. // username and password sent from signup form 
  16.  
  17. $myusername=$_POST['myusername']; 
  18. $mypassword=$_POST['mypassword']; 
  19.  
  20. $sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
  21. $result=mysql_query($sql);
  22.  
  23. // Mysql_num_row is counting table row
  24. $count=mysql_num_rows($result);
  25. // If result matched $myusername and $mypassword, table row must be 1 row
  26.  
  27. if($count==1){
  28. // Register $myusername, $mypassword and redirect to file "login_success.php"
  29. session_start();
  30. $_SESSION['myusername']=$myusername;
  31. $_SESSION['mypassword']=$mypassword;
  32. header("location:login_success.php");
  33. exit;
  34. }
  35. else {
  36. echo "Wrong Username or Password";
  37. exit;
  38. }
  39. ?>
Jul 24 '07 #13

code green
Expert 100+
P: 1,726
Parse error: syntax error, unexpected T_OBJECT_OPERATOR in /home/greenpos/public_html/test4/checklogin.php on line 18
This is exactly what it says it is. A syntax error on line 18. Can't work out where that is but the chevrons round the password look dodgy.
Jul 24 '07 #14

P: 12
This is exactly what it says it is. A syntax error on line 18. Can't work out where that is but the chevrons round the password look dodgy.

i just removed the <> from around the passwords and still the same thing....


any ideas?
Jul 24 '07 #15

P: 12
i just removed the <> from around the passwords and still the same thing....


any ideas?

no sorry: this is the new error message:

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/greenpos/public_html/test4/checklogin.php on line 30
Welcome
Jul 24 '07 #16

code green
Expert 100+
P: 1,726
This query has failed.
Expand|Select|Wrap|Line Numbers
  1. $sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
  2. $result=mysql_query($sql);
Check you have the expected values in $_POST
[PHP]$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword']; [/PHP]
It is advisable to use LIKE when comparing strings in MYSql instead of '='
Jul 24 '07 #17

pbmods
Expert 5K+
P: 5,821
Jason, please use CODE tags when posting source code. See the REPLY GUIDELINES on the right side of the page next time you post.
Jul 24 '07 #18

P: 12
This query has failed.
Expand|Select|Wrap|Line Numbers
  1. $sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
  2. $result=mysql_query($sql);
Check you have the expected values in $_POST
[PHP]$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword']; [/PHP]
It is advisable to use LIKE when comparing strings in MYSql instead of '='
Hi Codegreen,

THanks for your help also!.

you guys rock!

anyways, im very new to this all..

what exactly do you mean with your post?

i just dont understand whats going on, im positive i have followed everything right.

please get back to me, i would really appreciate it.

Cheers,
Jason.
Jul 24 '07 #19

P: 12
Hi gyus,

I think i have confused everyone and myself.

Below is the code im using:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $host="localhost"; // Host name 
  3. $username="greenpos_admin1"; // Mysql username 
  4. $password="carlo"; // Mysql password 
  5. $db_name="greenpos_test"; // Database name 
  6. $tbl_name="members"; // Table name 
  7.  
  8. // Connect to server and select databse.
  9. // Connect to server and select databse.
  10. $link=mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
  11. mysql_select_db($db_name, $link)or die("cannot select DB"); 
  12.  
  13.  
  14. // username and password sent from signup form 
  15.  
  16. $myusername=$_POST['myusername']; 
  17. $mypassword=$_POST['mypassword']; 
  18.  
  19. $sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
  20. $result=mysql_query($sql);
  21.  
  22. // Mysql_num_row is counting table row
  23. $count=mysql_num_rows($result);
  24. // If result matched $myusername and $mypassword, table row must be 1 row
  25.  
  26. if($count==1){
  27. // Register $myusername, $mypassword and redirect to file "login_success.php"
  28. session_start();
  29. $_SESSION['myusername']=$myusername;
  30. $_SESSION['mypassword']=$mypassword;
  31. header("location:login_success.php");
  32. exit;
  33. }
  34. else {
  35. echo "Wrong Username or Password";
  36. exit;
  37. }
  38. ?> 
This is what i did to create the table in mysql:

Expand|Select|Wrap|Line Numbers
  1. CREATE TABLE `members` (
  2. `id` int(4) NOT NULL auto_increment,
  3. `username` varchar(65) NOT NULL default '',
  4. `password` varchar(65) NOT NULL default '',
  5. PRIMARY KEY (`id`)
  6. ) TYPE=MyISAM AUTO_INCREMENT=2 ;
I then:
Dumped the data for table `members`

Expand|Select|Wrap|Line Numbers
  1. INSERT INTO `members` VALUES (1, 'jason', 'jason'');
Now when i use my login screen and type in my username and password

i get this error message:

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/greenpos/public_html/test4/checklogin.php:9) in /home/greenpos/public_html/test4/checklogin.php on line 37

Warning: Cannot modify header information - headers already sent by (output started at /home/greenpos/public_html/test4/checklogin.php:9) in /home/greenpos/public_html/test4/checklogin.php on line 40

line 37:
session_start();

Line 40:
header("location:login_success.php");

Im not sure if im diong everything right...

The code for my main_login.php:

Expand|Select|Wrap|Line Numbers
  1. <table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
  2. <tr>
  3. <form name="form1" method="post" action="checklogin.php">
  4. <td>
  5. <table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
  6. <tr>
  7. <td colspan="3"><strong>Member Login </strong></td>
  8. </tr>
  9. <tr>
  10. <td width="78">Username</td>
  11. <td width="6">:</td>
  12. <td width="294"><input name="myusername" type="text" id="myusername"></td>
  13. </tr>
  14. <tr>
  15. <td>Password</td>
  16. <td>:</td>
  17. <td><input name="mypassword" type="text" id="mypassword"></td>
  18. </tr>
  19. <tr>
  20. <td>&nbsp;</td>
  21. <td>&nbsp;</td>
  22. <td><input type="submit" name="Submit" value="Login"></td>
  23. </tr>
  24. </table>
  25. </td>
  26. </form>
  27. </tr>
  28. </table>
All i can think of is that when i added the table members to my database, i added username and password as the fields and not myusername and mypassword?

Please i would really appreciate your help.

Thanks.
Jason.
Jul 25 '07 #20

dafodil
100+
P: 392
Expand|Select|Wrap|Line Numbers
  1. else {
  2. echo "Wrong Username or Password";
  3. exit;
  4. }
  5.  
I told you to replace that part already. You cannot echo when you use session that's why you keep seeing the error about header already sent....
The alternative solution is to redirect to a page...

Are you sure you can connect to your mysql?
Jul 25 '07 #21

Post your reply

Sign in to post your reply or Sign up for a free account.