| | |
Echo MySQL Row Data
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
•
•
Join Date: Sep 2007
Posts: 36
Reputation:
Solved Threads: 0
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:
Here's my check-login code:
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");
}
?> If you'd like to do it in a session variable, do this.
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.
PHP Syntax (Toggle Plain Text)
<?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."); // If no magic quotes, add slashes if(!get_magic_quotes_gpc()) { $myusername = addslashes($_POST['myusername']); $mypassword = addslashes($_POST['mypassword']); } // Username and password sent from form. $myusername = mysql_real_escape_string($myusername); $mypassword = mysql_real_escape_string($mypassword); $sql = "SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'"; $result = mysql_query($sql); $row = mysql_fetch_assoc($result); // 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"); session_register("mycompany"); // Assign the mycompany variable from user table $_SESSION['mycompany'] = $row['company']; // 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"); } ?>
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.
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.
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.
-- The documentation is inevitable, you may get away with it for a little while but eventually you too will have to do the deed.
•
•
•
•
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.
PHP Syntax (Toggle Plain Text)
<?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."); // If no magic quotes, add slashes if(!get_magic_quotes_gpc()) { $myusername = addslashes($_POST['myusername']); $mypassword = addslashes($_POST['mypassword']); } // Username and password sent from form. $myusername = mysql_real_escape_string($myusername); $mypassword = mysql_real_escape_string($mypassword); $sql = "SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'"; $result = mysql_query($sql); $row = mysql_fetch_assoc($result); // 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) { // Assign session variables from user table $_SESSION['mycompany'] = $row['company']; $_SESSION['myusername'] = $row['username']; // 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"); } ?>
Last edited by MVied; Aug 13th, 2008 at 8:36 pm.
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.
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.
-- The documentation is inevitable, you may get away with it for a little while but eventually you too will have to do the deed.
•
•
Join Date: Sep 2007
Posts: 36
Reputation:
Solved Threads: 0
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
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
•
•
Join Date: Sep 2007
Posts: 36
Reputation:
Solved Threads: 0
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'];
}
?>•
•
•
•
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']; } ?>
PHP Syntax (Toggle Plain Text)
if ($_SESSION['company']) { echo $_SESSION['company']; }
•
•
•
•
Well if you shouldn't use session_register, maybe you should use this:
PHP Syntax (Toggle Plain Text)
if ($_SESSION['company']) { echo $_SESSION['company']; }
php Syntax (Toggle Plain Text)
if (isset($_SESSION['company']) && trim($_SESSION['company']) != "") { echo $_SESSION['company']; }
“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.
-- The documentation is inevitable, you may get away with it for a little while but eventually you too will have to do the deed.
![]() |
Similar Threads
- Clean Previous Next Script for MySQL results (PHP)
- Apache (Linux Servers and Apache)
- help in mysql (PHP)
- Displaying images from mysql database (PHP)
- echo data (PHP)
- Displaying Mysql rows, alternate colors? (PHP)
- Dealing with MySQL Strange Behaviour (PHP)
- mysql_num_rows(): supplied argument is not a valid MySQL result resource (PHP)
- configure PHP to work with Mysql (MySQL)
Other Threads in the PHP Forum
Views: 2948 | Replies: 10
| Thread Tools | Search this Thread |
Tag cloud for PHP
.htaccess access ajax apache api array beginner binary broken cakephp checkbox class cms code countingeverycharactersfromastring cron curl database date directory display download dynamic echo email error file files folder form forms function functions google href htaccess html image include insert integration ip java javascript joomla limit link login loop mail match menu methods mlm mod_rewrite multiple mysql oop pageing parse paypal pdf php printer problem query radio random recursion regex remote script search select server sessions simple sms soap source space spam speed sql structure syntax system table tutorial update updates upload url validation validator variable video web xml youtube





