Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

So basically i have set up a login system and when you log in it displays "Welcome " and then ur username, i want to set up where it does a display name instead, so i have it all set up with the register system and stuff but i've ran into a small problem.

I wanted it to assign the display name in the database to a varialbe

First I tried this

$data = mysql_query("SELECT userid FROM ao_user WHERE username = '{$this->_username}' AND password = '{$this->_password}' AND display = '{$this->_display}'");

I don't know if i set that up correctly or not. Whenever i go to log in when this is set up it says invalid username/password, even though i know its right. So that makes me think that its because thats where it checks the database for the correct username and password and if i add that in since when the user logs in they dont have to type in a display name it does nothing. I could be wrong

But i decided to move on so i tried doing this

$displaynamedata = mysql_query("SELECT userid FROM ao_user WHERE display = " . $this->_display);

The login executes but then it displays nothing for the display name.

Sorry that im terrible at explaining but hopefully someone can understand what im doing!

Thanks in advanced

EDIT: I just realized this as well but i could be wrong again, i think im executing the query backwards? Im not sure and im really confused.

EDIT: Ok so i got a bit of a problem

I set up a different script and it worked, this is how the script looked

<?php
$username = "Nynex71";
mysql_connect("localhost", "root", "test") or die(mysql_error());
  mysql_select_db("test") or die(mysql_error());
$result = mysql_query("SELECT display FROM ao_user WHERE username = '{$username}'") or die(msyql_error());
$row = mysql_fetch_assoc($result);
echo $row['display'];
?>

Then i tried merging it in with my system annndd nothing display after welcome

This is the code that was merged in

public function getDisplay()
  {
    mysql_connect("localhost", "root", "test") or die(mysql_error());
    mysql_select_db("test") or die(mysql_error());

    $result = mysql_query("SELECT display FROM ao_user WHERE username = '{$this->_username}'");
    $row = mysql_fetch_assoc($result);
    $this->_display = $row['display'];
    $_SESSION['display'] = $this->_display;
  }

I dont understand what could go wrong through that?

share|improve this question
 
The below answer is correct. However my piece of advise is if you are just starting out learning PHP & MySQL. Use MySQLi or PDO_MySQL As mysql_* are depreciated, and therefore you might as well forget that they exist! –  Matt Clements Jul 9 '12 at 21:42
add comment

1 Answer

mysql_query returns a result handle, not the value you selected. you have to first fetch a row, then retrieve the value from that row:

$result = mysql_query("SELECT ...") or die(msyql_error());
$row = mysql_fetch_assoc($result);
echo $row['userid'];
share|improve this answer
 
Thank you so much –  user1509916 Jul 9 '12 at 21:44
 
to get all answer rows (and not only the first one) put the "$row = mysql_fetch_assoc($result)" part into a loop like "while($row = mysql_fetch_assoc($result)) { echo $row['userid']; }" –  Asto Jul 9 '12 at 21:44
 
uhhh i ran into another problem ill post in in my first question –  user1509916 Jul 9 '12 at 21:50
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.