0

I want to get a value from a MySQL database and put it into a PHP variable.

I tried this:

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

The code says invalid username/password.

Here is the user login code:

<?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'];
?>

and

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;
  }

The program does not put any words into the PHP variable. What am I doing wrong and how do you do this?

1
  • 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! Commented Jul 9, 2012 at 21:42

1 Answer 1

3

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'];
Sign up to request clarification or add additional context in comments.

1 Comment

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']; }"

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.