Echo MySQL Row Data

Thread Solved

Join Date: Sep 2007
Posts: 36
Reputation: gpdrums is an unknown quantity at this point 
Solved Threads: 0
gpdrums gpdrums is offline Offline
Light Poster

Echo MySQL Row Data

 
0
  #1
Aug 13th, 2008
Hi All:

I have an extensive website in which I would like to echo the name of the company various pages after the user logs in. I think I should be using the $_SESSION global, but I'm having difficulty implementing it, placing it in my code, and knowing what php code to place in the pages where I want the company name to appear. From what I understand, I need to fetch the company name and assign it to the session while the user name and password are being authenticated.

For purposes of this question, the MySQL table is laid out as follows:
  • id
  • username
  • password
  • usergroup (not being used yet)
  • company
  • last_login

Here's my check-login code:

<?php

  $host = "---";
  $username = "---";
  $password = "---";
  $db = "---";
  $tbl_name = "---";

// Connect to server and select database.
mysql_connect($host, $username, $password) or die("Unable to connect to database.");
mysql_select_db($db) or die("Unable to connect to database.");


// Username and password sent from form.
$myusername = mysql_real_escape_string($_POST['myusername']);
$mypassword = mysql_real_escape_string($_POST['mypassword']);


$sql = "SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result = mysql_query($sql);

// Mysql_num_row is counting table row
$count = mysql_num_rows($result);

// If result matched $myusername and $mypassword, table row must be 1 row
  if($count==1) {

// Register $myusername, $mypassword and redirect to file "login_success.php"
  session_register("myusername");
  session_register("mypassword");

// Update the last_login field of the MySQL database
$lastlogin = mysql_query("UPDATE $tbl_name SET last_login=now() WHERE username='$myusername'");

// Redirect good login attempt
  header("location:login_success.php");
  }

// Redirect bad login attempt
  else {
    header("location:login_retry.php");
    }
?>
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 84
Reputation: MVied is an unknown quantity at this point 
Solved Threads: 5
MVied's Avatar
MVied MVied is offline Offline
Junior Poster in Training

Re: Echo MySQL Row Data

 
1
  #2
Aug 13th, 2008
If you'd like to do it in a session variable, do this.

  1. <?php
  2.  
  3. $host = "---";
  4. $username = "---";
  5. $password = "---";
  6. $db = "---";
  7. $tbl_name = "---";
  8.  
  9. // Connect to server and select database.
  10. mysql_connect($host, $username, $password) or die("Unable to connect to database.");
  11. mysql_select_db($db) or die("Unable to connect to database.");
  12.  
  13. // If no magic quotes, add slashes
  14. if(!get_magic_quotes_gpc()) {
  15. $myusername = addslashes($_POST['myusername']);
  16. $mypassword = addslashes($_POST['mypassword']);
  17. }
  18.  
  19. // Username and password sent from form.
  20. $myusername = mysql_real_escape_string($myusername);
  21. $mypassword = mysql_real_escape_string($mypassword);
  22.  
  23.  
  24. $sql = "SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
  25. $result = mysql_query($sql);
  26. $row = mysql_fetch_assoc($result);
  27.  
  28. // Mysql_num_row is counting table row
  29. $count = mysql_num_rows($result);
  30.  
  31. // If result matched $myusername and $mypassword, table row must be 1 row
  32. if($count==1) {
  33.  
  34. // Register $myusername, $mypassword and redirect to file "login_success.php"
  35. session_register("myusername");
  36. session_register("mypassword");
  37. session_register("mycompany");
  38.  
  39. // Assign the mycompany variable from user table
  40. $_SESSION['mycompany'] = $row['company'];
  41.  
  42. // Update the last_login field of the MySQL database
  43. $lastlogin = mysql_query("UPDATE $tbl_name SET last_login=now() WHERE username='$myusername'");
  44.  
  45. // Redirect good login attempt
  46. header("location:login_success.php");
  47. }
  48.  
  49. // Redirect bad login attempt
  50. else {
  51. header("location:login_retry.php");
  52. }
  53. ?>

I'm not completely certain you need to register the username and password with the session, but I may be wrong. It's be a while since I wrote my own session handler.

Oh, and if magic_quotes_gpc is enabled, you should use stripslashes() on the submitted form data and visa versa.
Last edited by MVied; Aug 13th, 2008 at 6:43 pm.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 849
Reputation: R0bb0b is on a distinguished road 
Solved Threads: 67
R0bb0b's Avatar
R0bb0b R0bb0b is offline Offline
Practically a Posting Shark

Re: Echo MySQL Row Data

 
0
  #3
Aug 13th, 2008
Also, you shouldn't be using session_register() as this is registering a global variable. You will find that in most hosts, register globals is turned off and in php 6.0 it is completely removed. You shouldn't be using register globals anyway. You only need to use $_SESSION[].

So, unless you really feel like revisiting everything you've done with sessions, I recommend not doing this.
Last edited by R0bb0b; Aug 13th, 2008 at 7:24 pm.
“Be who you are and say what you feel because those who mind don't matter and those who matter don't mind.” - Dr. Seuss

-- The documentation is inevitable, you may get away with it for a little while but eventually you too will have to do the deed.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 84
Reputation: MVied is an unknown quantity at this point 
Solved Threads: 5
MVied's Avatar
MVied MVied is offline Offline
Junior Poster in Training

Re: Echo MySQL Row Data

 
0
  #4
Aug 13th, 2008
Originally Posted by R0bb0b View Post
Also, you shouldn't be using session_register() as this is registering a global variable. You will find that in most hosts, register globals is turned off and in php 6.0 it is completely removed. You shouldn't be using register globals anyway. You only need to use $_SESSION[].

So, unless you really feel like revisiting everything you've done with sessions, I recommend not doing this.
Ah see, news to me R0b. I haven't messed with session in forever. So maybe this is what he should use.

  1. <?php
  2.  
  3. $host = "---";
  4. $username = "---";
  5. $password = "---";
  6. $db = "---";
  7. $tbl_name = "---";
  8.  
  9. // Connect to server and select database.
  10. mysql_connect($host, $username, $password) or die("Unable to connect to database.");
  11. mysql_select_db($db) or die("Unable to connect to database.");
  12.  
  13. // If no magic quotes, add slashes
  14. if(!get_magic_quotes_gpc()) {
  15. $myusername = addslashes($_POST['myusername']);
  16. $mypassword = addslashes($_POST['mypassword']);
  17. }
  18.  
  19. // Username and password sent from form.
  20. $myusername = mysql_real_escape_string($myusername);
  21. $mypassword = mysql_real_escape_string($mypassword);
  22.  
  23.  
  24. $sql = "SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
  25. $result = mysql_query($sql);
  26. $row = mysql_fetch_assoc($result);
  27.  
  28. // Mysql_num_row is counting table row
  29. $count = mysql_num_rows($result);
  30.  
  31. // If result matched $myusername and $mypassword, table row must be 1 row
  32. if($count==1) {
  33.  
  34. // Assign session variables from user table
  35. $_SESSION['mycompany'] = $row['company'];
  36. $_SESSION['myusername'] = $row['username'];
  37.  
  38. // Update the last_login field of the MySQL database
  39. $lastlogin = mysql_query("UPDATE $tbl_name SET last_login=now() WHERE username='$myusername'");
  40.  
  41. // Redirect good login attempt
  42. header("location:login_success.php");
  43. }
  44.  
  45. // Redirect bad login attempt
  46. else {
  47. header("location:login_retry.php");
  48. }
  49. ?>
Last edited by MVied; Aug 13th, 2008 at 8:36 pm.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 849
Reputation: R0bb0b is on a distinguished road 
Solved Threads: 67
R0bb0b's Avatar
R0bb0b R0bb0b is offline Offline
Practically a Posting Shark

Re: Echo MySQL Row Data

 
0
  #5
Aug 13th, 2008
Not a problem, I've just heard horror stories about php 4.2 when developers had to go back through the scripts that they wrote in the past because register globals was turned off. The less of that we have in the future the better off we'll be.

Yes, that would be the best way to go.
“Be who you are and say what you feel because those who mind don't matter and those who matter don't mind.” - Dr. Seuss

-- The documentation is inevitable, you may get away with it for a little while but eventually you too will have to do the deed.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 36
Reputation: gpdrums is an unknown quantity at this point 
Solved Threads: 0
gpdrums gpdrums is offline Offline
Light Poster

Re: Echo MySQL Row Data

 
0
  #6
Aug 13th, 2008
Thank you for the replies. I will have to continue this Friday or next Monday due to other web obligations.

With regard to the php version, we are self-hosting. I can reconfigure Apache if need be, but what I'm doing works--and if it aint broke, don't fix it! Right?

One more thing: I have to try out the code you both provided to see how it goes, but if I want to echo the company name on any page while the session is active, what do I need to do?

Thank you again for helping me here. I'll return the favor as often as I can.

GP
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 36
Reputation: gpdrums is an unknown quantity at this point 
Solved Threads: 0
gpdrums gpdrums is offline Offline
Light Poster

Re: Echo MySQL Row Data

 
0
  #7
Aug 14th, 2008
Update: This worked. Thank you for the help. FYI, here's what I did to echo the company name on the appropriate pages:

<?php
if (!session_register(company));
{
echo $_SESSION['company'];
}
?>
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 84
Reputation: MVied is an unknown quantity at this point 
Solved Threads: 5
MVied's Avatar
MVied MVied is offline Offline
Junior Poster in Training

Re: Echo MySQL Row Data

 
0
  #8
Aug 15th, 2008
Originally Posted by gpdrums View Post
Update: This worked. Thank you for the help. FYI, here's what I did to echo the company name on the appropriate pages:

<?php
if (!session_register(company));
{
echo $_SESSION['company'];
}
?>
Well if you shouldn't use session_register, maybe you should use this:
  1. if ($_SESSION['company']) {
  2. echo $_SESSION['company'];
  3. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 849
Reputation: R0bb0b is on a distinguished road 
Solved Threads: 67
R0bb0b's Avatar
R0bb0b R0bb0b is offline Offline
Practically a Posting Shark

Re: Echo MySQL Row Data

 
0
  #9
Aug 15th, 2008
Originally Posted by MVied View Post
Well if you shouldn't use session_register, maybe you should use this:
  1. if ($_SESSION['company']) {
  2. echo $_SESSION['company'];
  3. }
I usually go a step further and do this:
  1. if (isset($_SESSION['company']) && trim($_SESSION['company']) != "") {
  2. echo $_SESSION['company'];
  3. }
which will also ensure that it is not blank
“Be who you are and say what you feel because those who mind don't matter and those who matter don't mind.” - Dr. Seuss

-- The documentation is inevitable, you may get away with it for a little while but eventually you too will have to do the deed.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 36
Reputation: gpdrums is an unknown quantity at this point 
Solved Threads: 0
gpdrums gpdrums is offline Offline
Light Poster

Re: Echo MySQL Row Data

 
0
  #10
Aug 15th, 2008
As I continued to play with the code, I added "Welcome, " to the echo() statement. I noticed that if the person was not logged in, "Welcome," printed without the company name. What type of else statement will keep this from happening?
Reply With Quote Quick reply to this message  
Reply

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




Views: 2948 | Replies: 10
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC