Jump to content

[SOLVED] Need help with querry...I think


derekbelcher

Recommended Posts

I have a database that contains usernames, passwords, and first and last names associated with each username.  I want to display the users first and last name when they are logged in.  The code that I am using to pull this information is:

 

<td height="19" colspan="3" valign="top">Welcome <b><?php $sql = mysql_query("SELECT * FROM `officers`");

$fetch = mysql_fetch_assoc($sql);

$firstname = $fetch['firstname'];

$lastname = $fetch['lastname'];

echo $firstname , $lastname; ?></b></td>

 

The table is "officers".  The problem is 1) I only get the first and last name of the first person in the table regardless of the login, and The first and last name have no space between them when displayed.

 

How do I specify that the first and last name should be associated with the username that was used to log in?

 

Thanks.

Link to comment
Share on other sites

O.K.  I know this is going to sound stupid, but i am very new and learning here.  You said to add  WHERE username=postedusername.  What am i supposed to put in the "posted username"  Is there a certain variable from my table, or something.  my column headings are, ID, username, password, firstname, lastname.  I tried WHERE username=username, but that didn't work.

 

Sorry for the "stupidity"..Thanks  ;D

Link to comment
Share on other sites

I'm sure I am getting close here, but it doesn't display anything.  On the login page, the input is myusername.  So I did this:

 

<?php $sql = mysql_query("SELECT * FROM `officers` WHERE username='{$_POST['myusername']}' ");

$fetch = mysql_fetch_assoc($sql);

$firstname = $fetch['firstname'];

$lastname = $fetch['lastname'];

echo $firstname , $lastname; ?>

 

But the display on the logged in page just says Welcome.  No name is shown.  My login.php page sends the info to a checklogin.php page and if the username and password check out then the user sees the right page, if not they get an error page.  I am at a loss.  Any other ideas?

Link to comment
Share on other sites

<?PHP

 

$firstname = $_POST['firstname'] ;

$lastname = $_POST['lastname'] ;

$user = $_POST['myusername'] '

 

$sql = "SELECT * FROM officers WHERE username LIKE myusername" ;

 

$result = mysql_query($sql) ;

 

while($row = mysql_fetch_array($result, MYSQL_ASSOC))

{

 

$dbfname = $row['firstname'] ;

$dblname = $row['lastname'] ;

$dbuser = $row['username'] ;

}

 

echo "$dbfname , $dblname" ;

 

?>

 

right?

Link to comment
Share on other sites

This all makes since, but when I put the code in, I get:

 

Welcome

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/athens/public_html/Officers Area/officer_index.php on line 72

,

 

Was there anything in the code above that I should have changed to be specific with my database?  This all seems to make since, but I am not sure how to trouble shoot errors like the one above.

 

Thanks for your help!!!  :)

Link to comment
Share on other sites

This error mostly means, your query returns an error or empty set.

require_once('connect_script.php');
//or just put mysql_connect and mysql_select_db
$un = trim($_POST['username']);
$pw = trim($_POST['password']);
$query = 'SELECT * FROM users WHERE username=\'' . $un . '\' AND password=\'' . $pw . '\'';
//You will mostly get one result
$rs = mysql_query($query);
$u_obj = mysql_fetch_object($rs);
print 'Hi ' . $u_obj->first_name . ' ' . $u_obj->last_name . ', i am a high tech website that can say hi and your name';

I hope these make since ;)

 

Notice the single quotes in the query. Don't forget t

Link to comment
Share on other sites

The problem in my code appears to be with the:

 

$sql = "SELECT * FROM `officers`"

 

If I enter this line, then I get an output of the last row in the table regardless of what username is used to login.

 

When I try to add the WHERE information I get an error.  I am really not sure what I need to do to fix this.

 

I tried to use: WHERE username = $_POST['myusername'], but that didn't work either.

 

I would be happy to email the code to someone if that would help.

 

Thanks.

Link to comment
Share on other sites

O.K.  I am learning here...I think I understand a little more about what my code is doing:

 

In my SELECT * FROM `TABLE NAME` WHERE username (that is the column heading) = something.  This is where I am getting stuck.  I want username to = all the names in that column.  Then I want some code that checks to see which user is logged in (based on username) and displays the real name associated with that username.

 

In my table, I have a column that has usernames (e.g. dbelcher) and for dbelcher, on the same line, there is a first name listed under the column heading firstname.  Does this make any since?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.