Newbie question - pull first name from mysql database

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

mthierfelder
Forum Newbie
Posts: 12
Joined: Mon Apr 10, 2006 3:18 pm

Newbie question - pull first name from mysql database

Post by mthierfelder »

I need to design some code that will figure out if a member is logged in or not, then if they are, display a message like Hello "customer name" on the index page.

Here's what I have so far:

Code: Select all

if(empty($_SESSION[AgentID]))
{
$CustName .= "";
}
else
{
$aok = ($_SESSION[FirstName]);
$CustName .= "Hello $aok[FirstName]\n";
}
It doesn't do anything so far, and I'm pretty stumped.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Newbie question - pull first name from mysql database

Post by RobertGonzalez »

mthierfelder wrote:I need to design some code that will figure out if a member is logged in or not, then if they are, display a message like Hello "customer name" on the index page.

Here's what I have so far:

Code: Select all

<?php
if(empty($_SESSION[AgentID]))
{
$CustName .= "";
}
else
{
$aok = ($_SESSION[FirstName]);
$CustName .= "Hello $aok[FirstName]\n";
}
?>
It doesn't do anything so far, and I'm pretty stumped.
Your code is a little wonky. First off, are you calling session_start()? Second, are you setting $_SESSION['AgentID'] to anything (PS Notice the single quotes around AgentID). Next, try cleaning your code a little bit...

Code: Select all

<?php
if( empty($_SESSION['AgentID']) )
{
    $CustName .= "";
}
else
{
    $CustName .= "Hello " . $_SESSION['AgentID'] . "!\n";
}
?>
mthierfelder
Forum Newbie
Posts: 12
Joined: Mon Apr 10, 2006 3:18 pm

Post by mthierfelder »

Ok, well first off I'm not sure, and secondly I'm not sure....I don't know what I'm doing!

All I want to do is for mysql to get the table called 'yellow_agents' where the cell 'FirstName' is that of the person who is logged in, and display it. That's IT! I admit, I know very little about authoring PHP logic...although I can understand it just fine.

Please help!
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

If you are not sure about whether you called session_start() or whether you are setting a value to a variable, then you may want to consider starting a little slower. Using MySQL to authenticate users in conjunction with Sessions is a little involved. You need to be able to start somewhere. This community can help you but it won't do the work for you.

Do you have something that you are starting with (a current code base) or are you tackling this from scratch? How long have you been developing in PHP/MySQL and what other work have you done in it? We might have to take a few baby steps to get you in the swing of using the more powerful features of the two.
mthierfelder
Forum Newbie
Posts: 12
Joined: Mon Apr 10, 2006 3:18 pm

Post by mthierfelder »

I am working with a finished script, I'm just trying to augment it a little bit here and there. Mostly, I'm a web designer but have been moving towards more development in the past few years. My PHP/MySQL experience has been with OsCommerce, which I have been successful with making all kinds of changes requested by clients.

I have always just changed things or moved them slightly though, I have never tried to author anything on my own. That is to say, I understand the syntax pretty well if you put a piece of code in front of me but if you told me to sit down and author that same piece of code I'd be lost.

Yes, baby steps sounds good...
mthierfelder
Forum Newbie
Posts: 12
Joined: Mon Apr 10, 2006 3:18 pm

Post by mthierfelder »

I have made a little progress, but still not getting what I need. Here is my updated code:

Code: Select all

<?
		if ($_SESSION['AgentID'] == ''){ //empty 
print "Welcome, guest"; 
} else { 
print "Welcome, " . $_SESSION['AgentID']['FirstName']; 
}  
?>
This almost works, but for whatever reason it refuses to pull the data from the 'FirstName' cell in my database...all it gives me is "Welcome, 3" instead which is the 'AgentID', which is the primary key of that table.

What am I doing wrong?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

The code you posted above assumes that $_SESSION['AgentID'] is an array. To get the data from the database you are going to have to look at your query and how the PHP code interacts with it. Then you are going to have to figure out the field name used in the query for FirstName (sometimes it is the database column name, sometimes it is an alias). Once you have that, you need to set the value of $_SESSION['AgentID'] to a corresponding value of the resultset from the database query. This is just the setting of the session variable.

There needs to be more interaction than what you have shown to do what you want to do. If you can, provide more details or more code. Maybe there is something in the code that we can help you with in terms of setting and getting your session values.
mthierfelder
Forum Newbie
Posts: 12
Joined: Mon Apr 10, 2006 3:18 pm

Post by mthierfelder »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I understand what you're saying.  I've done some digging and come up with a more elaborate script, and now I'm 9/10ths of the way ( I THINK)

Here she is:
[syntax="php"]//WELCOME GREETING
	  
$data = mysql_query("SELECT * FROM yellow_agents")
or die(mysql_error());
$info = mysql_fetch_array( $data );
/*while($info = mysql_fetch_array($data))*/

		if ($_SESSION['AgentID'] == ''){ //empty 
print "Welcome, guest"; 
} elseif ($_SESSION['AgentID'] != ''){ 
print "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\" style=\"border: 1px dashed #cccccc; margin: 10px; padding: 10px;\">\n
<tr>\n
<td valign=\"top\">Welcome Back, " . $info['FirstName'] . "<br><br>\n";
print "Account Status: " . $info['AccountStatus'] . "</td>\n</tr>\n</table>\n";
}  
Unfortunately, this code only returns the name of the very first person listed in the "yellow_agents" table...Leann.

You can see this script in action here: http://www.nwbuildnet.com/stores/builders/
Here's a login: username: test password: test


feyd | Please use[/syntax]

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

without a loop, only one record can be returned.
mthierfelder
Forum Newbie
Posts: 12
Joined: Mon Apr 10, 2006 3:18 pm

Post by mthierfelder »

can you give me some example code to work with?
Unless its not otherwise obvious...I don't know what I'm doing.

Thanks in advance.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

mthierfelder wrote: Here she is:

Code: Select all

<?php
//WELCOME GREETING
	  
$data = mysql_query("SELECT * FROM yellow_agents") 
or die(mysql_error());
$info = mysql_fetch_array( $data );
/*while($info = mysql_fetch_array($data))*/

		if ($_SESSION['AgentID'] == ''){ //empty 
print "Welcome, guest"; 
} elseif ($_SESSION['AgentID'] != ''){ 
print "<table border="0" cellpadding="0" cellspacing="0" width="100%" style="border: 1px dashed #cccccc; margin: 10px; padding: 10px;">\n
<tr>\n
<td valign="top">Welcome Back, " . $info['FirstName'] . "<br><br>\n";
print "Account Status: " . $info['AccountStatus'] . "</td>\n</tr>\n</table>\n";
}?>
Unfortunately, this code only returns the name of the very first person listed in the "yellow_agents" table...Leann.
I think this has to do with the way you are trying to get information out of your result array. But, first things first, try to use the *PHP* button instead of the *CODE* button when posting PHP (you could also use the [ syntax="php ] BBCode tag as well).

This will clean up what you are doing...

Code: Select all

<?php
// This query is pulling every record from yellow_agents table
// Is this what you want?
$data = mysql_query("SELECT * FROM yellow_agents") or die(mysql_error());
$info = mysql_fetch_array($data);
while($info = mysql_fetch_array($data))
{
	if (empty($_SESSION['AgentID'])) {  //empty 
		echo 'Welcome, guest!';
	} else {
		echo '<table border="0" cellpadding="0" cellspacing="0" width="100%" style="border: 1px dashed #cccccc; margin: 10px; padding: 10px;">';
		echo '<tr>';
		echo '<td valign="top">Welcome Back, ' . $info['FirstName'] . '<br><br>';
		echo 'Account Status: ' . $info['AccountStatus'] . '</td></tr></table>';
	}
}?>
This assumes that there is a field in the yellow_agents table with the name of AccountStatus and that you are selecting every row of data in the yellow_agents table. Another thing to consider is that you are comparing against the SESSION value for AgentID but you are using the database value if it is not empty. This would be a great place to use the session value.
mthierfelder
Forum Newbie
Posts: 12
Joined: Mon Apr 10, 2006 3:18 pm

Post by mthierfelder »

Great!

Yes, I just read the

Code: Select all

sticky so I'll try that one next time.

I just want to return the value for the currently logged in member, or current session...not every single record in that table.  But not sure how to do that.

Any help appreciated.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

You may want to go back to basics. What you are after is a login/authentication routine using information stored in a database. That is not really terribly involved, but you are going to know SQL and how to use PHP to get at what you want. You might want to search here for "login scripts" or "user authentication" to see what comes up.

From your previous reply this thread is going to turn into a long tutorial on database interaction, sessions and user authentication. That might be a bit outside the scope of a single forum thread.
mthierfelder
Forum Newbie
Posts: 12
Joined: Mon Apr 10, 2006 3:18 pm

Post by mthierfelder »

You'll have to forgive my ignorance, but it seems like I'm almost there...

That is to say:

Previous posts with code examples --> Very helpful

Recent post with suggestion to go learn a mountain of PHP logic --> Not very helpful
mthierfelder
Forum Newbie
Posts: 12
Joined: Mon Apr 10, 2006 3:18 pm

Post by mthierfelder »

I thought that maybe if I provided a bit more of my code so you can see the context that it exists in you might be able to help.

And I actually DO have an authentication script in service, that is to say there is a registration process that envolves a username/password to get in.

Code: Select all

<?
require_once("conn.php");
require_once("includes.php");
require_once("templates/HeaderTemplate.php");


//WELCOME GREETING
	  
$data = mysql_query("SELECT * FROM yellow_agents") or die(mysql_error());
$info = mysql_fetch_array($data);
/*while($info = mysql_fetch_array($data))*/
{
        if (empty($_SESSION['AgentID'])) {  //empty
              echo 'Welcome, guest!';
        } else {
                echo '<table border="0" cellpadding="0" cellspacing="0" width="100%" style="border: 1px dashed #cccccc; margin: 10px; padding: 10px;">';
                echo '<tr>';
                echo '<td valign="top">Welcome Back, ' . $info['FirstName'] . '<br><br>';
                echo 'Account Status: ' . $info['AccountStatus'] . '</td></tr></table>';
        }
}
?>
I can provide the include files if that would help.
Post Reply