我想从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变量中添加任何单词。我做错了什么,您怎么做?
最佳答案
mysql_query
返回结果句柄,而不是您选择的值。您必须先获取一行,然后从该行检索值:
$result = mysql_query("SELECT ...") or die(msyql_error());
$row = mysql_fetch_assoc($result);
echo $row['userid'];