MySQL将查询数据转换为PHP变量

时间:2012-07-09 21:32:39

标签: php mysql

我想从MySQL数据库中获取一个值并将其放入PHP变量中。

我试过了:

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

代码表示用户名/密码无效。

以下是用户登录代码:

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

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

程序不会将任何单词放入PHP变量中。我做错了什么,你是怎么做到的?

1 个答案:

答案 0 :(得分:3)

mysql_query返回结果句柄,而不是您选择的值。你必须先获取一行,然后从该行中检索值:

$result = mysql_query("SELECT ...") or die(msyql_error());
$row = mysql_fetch_assoc($result);
echo $row['userid'];