【发布时间】:2016-05-01 20:27:41
【问题描述】:
我对 PHP 很陌生,所以如果这个答案已经存在,我深表歉意。我有一个看起来像这样的表:
mcID | mcName
-------------
M1 | M1name
M2 | M2name
M3 | M3name
M4 | M4name
我知道如何编写四个单独的 sql select 语句来将它们放入变量中,但这似乎有点过头了。我想知道是否有一种方法可以运行单个 select 语句来设置所有四个变量。我当前设置一个变量的代码如下:
$sql = "SELECT * FROM tblmaincircles WHERE mcID='M1'";
$result = $conn->query($sql);
if($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$M1 = $row["mcName"];
}
}
else {
echo "0 results";
}
对此的任何帮助将不胜感激。我有一种感觉,我错过了一些非常基本的东西。谢谢!
【问题讨论】:
-
如果
SELECT * FROM tblmaincircles呢? -
where不是必需的,如果您不想限制结果,请不要使用where。见dev.mysql.com/doc/refman/5.7/en/select.html;The WHERE clause, if given, indicates the condition or conditions that rows must satisfy to be selected. where_condition is an expression that evaluates to true for each row to be selected. The statement selects all rows if there is no WHERE clause. -
我知道 WHERE 子句将排除所有其他子句。我只是显示我当前的代码。我知道我需要删除此子句才能使其正常工作,但我更喜欢共享一段工作代码,而不是从一开始就被破坏的代码。
标签: php mysql variables select