943,603 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 5161
  • PHP RSS
You are currently viewing page 1 of this multi-page discussion thread
Aug 13th, 2008
0

Echo MySQL Row Data

Expand Post »
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");
    }
?>
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
gpdrums is offline Offline
36 posts
since Sep 2007
Aug 13th, 2008
1

Re: Echo MySQL Row Data

If you'd like to do it in a session variable, do this.

PHP Syntax (Toggle Plain Text)
  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.
Reputation Points: 21
Solved Threads: 11
Junior Poster
MVied is offline Offline
111 posts
since Aug 2008
Aug 13th, 2008
0

Re: Echo MySQL Row Data

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.
Reputation Points: 358
Solved Threads: 89
Posting Shark
R0bb0b is offline Offline
986 posts
since Jun 2008
Aug 13th, 2008
0

Re: Echo MySQL Row Data

Click to Expand / Collapse  Quote originally posted by R0bb0b ...
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.

PHP Syntax (Toggle Plain Text)
  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.
Reputation Points: 21
Solved Threads: 11
Junior Poster
MVied is offline Offline
111 posts
since Aug 2008
Aug 13th, 2008
0

Re: Echo MySQL Row Data

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.
Reputation Points: 358
Solved Threads: 89
Posting Shark
R0bb0b is offline Offline
986 posts
since Jun 2008
Aug 13th, 2008
0

Re: Echo MySQL Row Data

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
Reputation Points: 10
Solved Threads: 0
Light Poster
gpdrums is offline Offline
36 posts
since Sep 2007
Aug 14th, 2008
0

Re: Echo MySQL Row Data

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'];
}
?>
Reputation Points: 10
Solved Threads: 0
Light Poster
gpdrums is offline Offline
36 posts
since Sep 2007
Aug 15th, 2008
0

Re: Echo MySQL Row Data

Click to Expand / Collapse  Quote originally posted by gpdrums ...
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:
PHP Syntax (Toggle Plain Text)
  1. if ($_SESSION['company']) {
  2. echo $_SESSION['company'];
  3. }
Reputation Points: 21
Solved Threads: 11
Junior Poster
MVied is offline Offline
111 posts
since Aug 2008
Aug 15th, 2008
0

Re: Echo MySQL Row Data

Click to Expand / Collapse  Quote originally posted by MVied ...
Well if you shouldn't use session_register, maybe you should use this:
PHP Syntax (Toggle Plain Text)
  1. if ($_SESSION['company']) {
  2. echo $_SESSION['company'];
  3. }
I usually go a step further and do this:
php Syntax (Toggle Plain Text)
  1. if (isset($_SESSION['company']) && trim($_SESSION['company']) != "") {
  2. echo $_SESSION['company'];
  3. }
which will also ensure that it is not blank
Reputation Points: 358
Solved Threads: 89
Posting Shark
R0bb0b is offline Offline
986 posts
since Jun 2008
Aug 15th, 2008
0

Re: Echo MySQL Row Data

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?
Reputation Points: 10
Solved Threads: 0
Light Poster
gpdrums is offline Offline
36 posts
since Sep 2007

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:





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


Follow us on Twitter


© 2011 DaniWeb® LLC