retrieving data from MySQL

Reply

Join Date: Mar 2008
Posts: 22
Reputation: eparse is an unknown quantity at this point 
Solved Threads: 0
eparse eparse is offline Offline
Newbie Poster

retrieving data from MySQL

 
0
  #1
Mar 17th, 2008
hi for all the expert here, i am now facing a minor problem on retrieving data here. MySQL do contain all the variables defined with data inside it. However, i juz need to know how to loop through the array to get all the data in a row?
Below is the php code i used:
  1. <?php
  2. mysql_connect("localhost", "root", "1234") or die(mysql_error());
  3. mysql_select_db("userlist") or die(mysql_error());
  4. //session_start();
  5. $query = "SELECT * FROM user";
  6. $result = mysql_query($query) or die(mysql_error());
  7. $row = mysql_fetch_array($result) or die(mysql_error());
  8. if($_POST['name'] == $row['username'])
  9. {
  10. echo "Thanks for logging in.";
  11. header("http://www.google.com");
  12. }
  13. else
  14. {
  15. echo "Sorry, your username and password does not match.";
  16. //header("http://www.yahoo.com");
  17. }
  18. $_SESSION['name']=$_POST['name'];
  19. $_SESSION['password']=$_POST['password'];
  20. $_SESSION['status']=$_POST['status'];
  21. ?>
especially at the function, i dunnoe why i can see the Thanks for login even no username entered. Can anyone help? By the way, php redirect cannot use more than once? Thanks for help....
Last edited by eparse; Mar 17th, 2008 at 9:23 am.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3,761
Reputation: nav33n is a jewel in the rough nav33n is a jewel in the rough nav33n is a jewel in the rough 
Solved Threads: 332
Moderator
Featured Poster
nav33n's Avatar
nav33n nav33n is offline Offline
Senior Poster

Re: retrieving data from MySQL

 
0
  #2
Mar 17th, 2008
Instead of $row = mysql_fetch_array($result) use, while($row = mysql_fetch_array($result)) { AND
echo "Thanks for logging in.";
header("http://www.google.com");
will give you an error because you can't output anything before header function. Either echo a message or redirect the user.
Ignorance is definitely not bliss!

*PM asking for help will be ignored*
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 22
Reputation: eparse is an unknown quantity at this point 
Solved Threads: 0
eparse eparse is offline Offline
Newbie Poster

Re: retrieving data from MySQL

 
0
  #3
Mar 17th, 2008
thanks for the solution, however i still facing the problem using while loop...
it keeps showing this on the page:
Sorry! Wrong name!!Sorry! Wrong name!!Sorry! Wrong name!!
when i use while loop..
below is the modified code:
  1. <?php
  2. mysql_connect("localhost", "root", "1234") or die(mysql_error());
  3. mysql_select_db("userlist") or die(mysql_error());
  4. $query = "SELECT * FROM user";
  5. $result = mysql_query($query) or die(mysql_error());
  6. while($row = mysql_fetch_array($result)){
  7. if ($_POST['name']==$row['username'] && $_POST['password']==$row['password'])
  8. {
  9. echo "Login successfully!!!";
  10. }
  11. else
  12. {
  13. echo "Sorry! Wrong name!!";
  14. }
  15. }
  16. $name=$_POST['name'];
  17. $password=$_POST['password'];
  18. $status=$_POST['status'];
  19. ?>

and it only manage to get the 1st row of the data...any silly mistakes i'd done?
Last edited by eparse; Mar 17th, 2008 at 11:20 am.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3,761
Reputation: nav33n is a jewel in the rough nav33n is a jewel in the rough nav33n is a jewel in the rough 
Solved Threads: 332
Moderator
Featured Poster
nav33n's Avatar
nav33n nav33n is offline Offline
Senior Poster

Re: retrieving data from MySQL

 
0
  #4
Mar 17th, 2008
When the user types username and password, why not check if that exists in the table, rather than get all records and then check ?
  1. $name=$_POST['username'];
  2. $password=$_POST['password'];
  3. Ie., $query = "select * from user where username='$name' and password='$password'";
Then check if this returns more than 1 row. If it does, then the username and password is valid. Else, invalid username and pass.
Ignorance is definitely not bliss!

*PM asking for help will be ignored*
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 22
Reputation: eparse is an unknown quantity at this point 
Solved Threads: 0
eparse eparse is offline Offline
Newbie Poster

Re: retrieving data from MySQL

 
0
  #5
Mar 17th, 2008
well, using simple script, i can get all the data within the table, the table now consists of 3 rows of data, but i dunnoe why there exist three times of echo ...and the variable do contain data input by the user...
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3,761
Reputation: nav33n is a jewel in the rough nav33n is a jewel in the rough nav33n is a jewel in the rough 
Solved Threads: 332
Moderator
Featured Poster
nav33n's Avatar
nav33n nav33n is offline Offline
Senior Poster

Re: retrieving data from MySQL

 
1
  #6
Mar 17th, 2008
It displays thrice because its looping three times. You can do it this way.
  1. <?php
  2. mysql_connect("localhost", "root", "1234") or die(mysql_error());
  3. mysql_select_db("userlist") or die(mysql_error());
  4. $name=$_POST['name'];
  5. $password=$_POST['password'];
  6. $query = "SELECT * FROM user where username='$name' && password='$password'";
  7. $result = mysql_query($query) or die(mysql_error());
  8. if(mysql_num_rows($result) > 0){
  9. echo "Success!";
  10. }
  11. else
  12. {
  13. echo "Sorry! Wrong name!!";
  14. }
  15. ?>
Ignorance is definitely not bliss!

*PM asking for help will be ignored*
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 22
Reputation: eparse is an unknown quantity at this point 
Solved Threads: 0
eparse eparse is offline Offline
Newbie Poster

Re: retrieving data from MySQL

 
0
  #7
Mar 17th, 2008
thanks man....it solved...
reputation given...
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3,761
Reputation: nav33n is a jewel in the rough nav33n is a jewel in the rough nav33n is a jewel in the rough 
Solved Threads: 332
Moderator
Featured Poster
nav33n's Avatar
nav33n nav33n is offline Offline
Senior Poster

Re: retrieving data from MySQL

 
0
  #8
Mar 17th, 2008
cool !
Ignorance is definitely not bliss!

*PM asking for help will be ignored*
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 22
Reputation: eparse is an unknown quantity at this point 
Solved Threads: 0
eparse eparse is offline Offline
Newbie Poster

Re: retrieving data from MySQL

 
0
  #9
Mar 17th, 2008
by the way...got any php script tht can redirect the viewer after display a message like:

Thank You....You'd logged in sucessfully...

After 5 seconds redirect the page..possible?
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3,761
Reputation: nav33n is a jewel in the rough nav33n is a jewel in the rough nav33n is a jewel in the rough 
Solved Threads: 332
Moderator
Featured Poster
nav33n's Avatar
nav33n nav33n is offline Offline
Senior Poster

Re: retrieving data from MySQL

 
0
  #10
Mar 17th, 2008
Yeah..possible.. check this link ! http://www.tizag.com/javascriptT/javascriptredirect.php
Ignorance is definitely not bliss!

*PM asking for help will be ignored*
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC