PHP - MySQL 选择,从,哪里
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15712954/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP - MySQL select, from, where
提问by Ryan
I am having some difficulties selecting a field in a row. Perhaps someone can point me into the correct direction to read some proper documentation, I am pretty new at this.
我在连续选择一个字段时遇到了一些困难。也许有人可以指出我阅读一些正确文档的正确方向,我对此很陌生。
I have a table called users where I have a row with a username set to ryanjay. I am trying to select the password from this row.
我有一个名为 users 的表,其中有一行的用户名设置为 ryanjay。我正在尝试从此行中选择密码。
This is what I have right now, and I am getting an error:
这就是我现在所拥有的,并且出现错误:
Unknown column 'password' in 'field list'
“字段列表”中的未知列“密码”
Here is my code
这是我的代码
$loggin_user = $_COOKIE['username'];
$query = "SELECT password FROM users WHERE username = $loggin_user";
$result = mysql_query($query) or die($query."<br/><br/>".mysql_error());
$password = mysql_result($result, 0);
echo $password;
For the sake of this question, password is 'password'. When echoing $password, I should see 'password'. But nothing.
对于这个问题,密码是“密码”。当回显 $password 时,我应该看到“密码”。但没什么。
I am able to write to the database and everything. I am also including a db.php which has my database info for connecting.
我能够写入数据库和所有内容。我还包括一个 db.php,其中包含用于连接的数据库信息。
Any help would be great.
任何帮助都会很棒。
回答by fyrye
try
尝试
$loggin_user = mysql_real_escape_string($_COOKIE['username']);
$query = "SELECT `password` FROM `users` WHERE `username` = '$loggin_user'";
$result = mysql_query($query) or die($query."<br/><br/>".mysql_error());
$password = mysql_result($result, 0);
echo $password;
回答by Captain Payalytic
It is telling you that there is no column called "password" in the table users.
它告诉您表用户中没有名为“密码”的列。
回答by Stefan Brendle
You need single quotes ' for string columns. Otherwise SQL thinks, it's a table name. Try this:
字符串列需要单引号 ' 。否则 SQL 认为,它是一个表名。尝试这个:
$query = "SELECT `password` FROM `users` WHERE `username` = '".$loggin_user."' ";
Don't forget to escape your variables to prevent SQL injections. Also it's more pretty to use backticks in MySQL for table/column names to avoid problems with reserved names.
不要忘记对变量进行转义以防止 SQL 注入。此外,在 MySQL 中对表/列名称使用反引号更漂亮,以避免保留名称出现问题。