473,466 Members | 1,370 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Issue With Simple Php Login Script - Mysql Error

12 New Member
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
20 2357
dafodil
392 Contributor
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
cmbcorp
12 New Member
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
392 Contributor
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
cmbcorp
12 New Member
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
cmbcorp
12 New Member
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
392 Contributor
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
cmbcorp
12 New Member
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
392 Contributor
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
cmbcorp
12 New Member
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
392 Contributor
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
cmbcorp
12 New Member
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
cmbcorp
12 New Member
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
1,726 Recognized Expert Top Contributor
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
cmbcorp
12 New Member
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
cmbcorp
12 New Member
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
1,726 Recognized Expert Top Contributor
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
5,821 Recognized Expert Expert
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
cmbcorp
12 New Member
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
cmbcorp
12 New Member
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
392 Contributor
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

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

Similar topics

8
by: Steve Fitzgerald | last post by:
The below login script does work. The form does not seem to be submitting. I keep getting the username and password fields. The only errors I get are notices that email and password and undefined...
2
by: bagsmode | last post by:
Hi, I'm trying to set a session cookie and then redirect, however I get the error: Status: 302 Moved Location: /index.cgi I thought I recall getting an error like this when I first tried...
0
by: Richard Jones | last post by:
I'm proud to release this, the 1.1.1 release of Roundup. Fixed in this release: - failure with browsers not sending "Accept-Language" header (sf bugs 1429646 and 1435335) - translate class...
3
by: James Thomas | last post by:
I have an issue with the login process on my site since the upgrade to ASP .NET 2.0 (from 1.1). I have the standard textbox and button for logging and my own custom login process. I'd like to...
3
by: focussys | last post by:
hi i am a student and am doing this for one of my assignments i am trying to create a login using perl and mysql database.i was succesful in doing that now i want to use cookies for...
1
by: www.web20developers.com | last post by:
http://www.web20developers.com http://www.web20developers.com/index.php?option=com_content&task=view... Ajallerix : AJAX, simple, fast Web image gallery demo ; at Novell AJAX -...
9
by: warezguy05 | last post by:
Hello I'm experiencing a problem; I've written a small script where volunteers can be booked for work-activities at a festival. The festival has 5 different departments so i've created a...
9
by: Ben | last post by:
Hello, I'll bet this has been asked a million times but I can't seem to find a thread that gives the clear example I need. This PC has MySQL and IIS configured and running. The MySQL database is...
3
by: mejpark | last post by:
Good afternoon PHPers, This morning I downloaded "PHP Users" from sourceforge to implement a user registration system. I followed the instructions in INSTALL.txt, and successfully configured...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.