在另一个表中查找结果并在网页上打印出来

时间:2014-11-30 12:45:04

标签: php mysql

我在网页上打印出具有以下内容的用户和头像  代码但希望还将用户余额添加到输出中  这是在另一个表..两个表共享用户虽然  在余额表中有未注册的用户  在用户信息表

表userinfo

              user                  |        avatar        |

 ------------------------------------------------------------

 29758b15-e021-4b3b-aad7-088b3843a9f6|     Neido Toxx       |

 313b96f2-b9f1-4c8b-a3d0-5997d9a7bc1c|     Marla Abbot      |

 98fde03e-377b-4b51-8ad6-6fb492e75298|     Captian Grid     |

表余额

              user                  |        balance        |

 ------------------------------------------------------------

 29758b15-e021-4b3b-aad7-088b3843a9f6|         1000         |

 313b96f2-b9f1-4c8b-a3d0-5997d9a7bc1c|            0         |

 98fde03e-377b-4b51-8ad6-6fb492e75298|         2500         |

 abcde03e-377b-4b51-8ad6-6fb492e75298|            0         |





$result = mysql_query("SELECT user,avatar FROM userinfo");
if (!$result) {
die("Query to show fields from table failed");
}
$fields_num = mysql_num_fields($result);
echo "<table><tr >";
// printing table headers
for($i=0; $i<$fields_num; $i++)
{
    $field = mysql_fetch_field($result);
    echo " <td>{$field->name}</td>";
}
echo "</tr>\n";
// printing table rows
while($row = mysql_fetch_row($result))
{
    echo "<tr>";
    // $row is array... foreach( .. ) puts every element
    // of $row to $cell variable
    foreach($row as $cell)
        echo "<td>$cell</font></td>";
    echo "</tr>\n";
}
mysql_free_result($result);

1 个答案:

答案 0 :(得分:1)

select balance.user, info.avatar, balance.balance 
from user_balance balance
left join user_info info on info.user = balance.user