SQL获取与请求匹配的所有查询

时间:2014-09-25 22:28:01

标签: php sql

嘿所有我想知道如何从表中获取所有查询,它只获得第一个并停止

我的代码:

              $result1 = mysql_query("SELECT `to` FROM transactions WHERE `from` = '$user'");

              $row = mysql_fetch_row($result1);

              $result2 = mysql_query("SELECT `amount` FROM transactions WHERE `from` = '$user'");

               $row2 = mysql_fetch_row($result2);


              echo "<li>TO</li>";
              echo "<li>AMOUNT</li>";
              echo "<li>$row[0]</li>";
              echo "<li>$row2[0]</li>";
              echo "<li>$row[1]</li>";
              echo "<li>$row2[1]</li>";
              echo "<li>$row[2]</li>";
              echo "<li>$row2[2]</li>";
              echo "<li>$row[3]</li>";
              echo "<li>$row2[4]</li>";
              echo "<li>$row[4]</li>";
              echo "<li>$row2[5]</li>";

我的数据库:

I want all of the rows that say "fusion" or the to return, but it only returns first.

1 个答案:

答案 0 :(得分:1)

$row = mysql_fetch_row($result1); will only fetch one row

您需要使用while

$query = mysql_query("SELECT `to`, `amount` FROM transactions WHERE `from` = '$user'");
echo "<li>TO</li>";
echo "<li>AMOUNT</li>";
while ($row = mysql_fetch_assoc($query)) {

     echo "<li>{$row['to']}</li>";
     echo "<li>{$row['amount']}</li>";
}