0

我想从 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 变量中。我做错了什么,你是怎么做到的?

4

1 回答 1

3

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

$result = mysql_query("SELECT ...") or die(msyql_error());
$row = mysql_fetch_assoc($result);
echo $row['userid'];
于 2012-07-09T21:36:19.160 回答