PHP SQL 从哪里选择

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11983714/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 02:33:54  来源:igfitidea点击:

PHP SQL Select From Where

phpmysql

提问by Christopher

I am having some difficulty running some SQL code.

我在运行一些 SQL 代码时遇到了一些困难。

What I am trying to do is, find a row that contains the correct username, and then get a value from that correct row.

我想要做的是,找到包含正确用户名的行,然后从该正确行中获取值。

This is my SQL in the php:

这是我在 php 中的 SQL:

mysql_query("SELECT * FROM users WHERE joined='$username' GET name")

As you can see, it looks for a username in users and then once found, it must GET a value from the correct row.

如您所见,它在 users 中查找用户名,然后一旦找到,它必须从正确的行中获取一个值。

How do I do that?

我怎么做?

回答by mimicocotopus

You need some additional PHP code (a call to mysql_fetch_array) to process the result resource returned by MySQL.

您需要一些额外的 PHP 代码(调用 mysql_fetch_array)来处理 MySQL 返回的结果资源。

$result = mysql_query("SELECT name FROM users WHERE joined='$username'");

$row = mysql_fetch_array($result);

echo $row['name'];

回答by Fluffeh

mysql_query("SELECT `name` FROM users WHERE joined='$username' ")

Just select the right column in your 'select clause' like above.

只需像上面一样在“选择子句”中选择正确的列。

Edit: If you are just starting out though, you might want to follow a tutorial like this onewhich should take you through a nice step by step (and more importantly up to date functions) that will get you started.

编辑:如果你是刚刚起步,不过,你可能要遵循这样的教程一个应该带你通过一个漂亮的一步一步(更重要的是最新的功能),这将让你开始。

回答by IEnumerable

mysql_query("SELECT name FROM users WHERE joined='$username'")

回答by asprin

$q = mysql_query("SELECT * FROM users WHERE joined='$username'");

$r = mysql_fetch_array($q);

$name = $r['user_name']; // replace user_name with the column name of your table

回答by elo

mysql_query("SELECT name FROM users WHERE joined='$username' ")

Read documentation : http://dev.mysql.com/doc/refman/5.0/en/select.html

阅读文档:http: //dev.mysql.com/doc/refman/5.0/en/select.html