我需要一些帮助。我试图从同一数据库中的两个表中提取一些信息。
我想从activity_points中提取user_id,并将其与user_id的用户匹配。当它与它们匹配时,我希望它显示该用户的full_name。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<?php
$db_host = "localhost";
$db_username = "******";
$db_pass = "*********";
$db_name = "******";
mysql_connect("$db_host","$db_username","$db_pass") or die(mysql_error());
mysql_select_db("$db_name") or die("no database by that name");
?>
<?php
$staticList = "";
$sql = mysql_query("SELECT * FROM phpfox_user_activity WHERE activity_points ORDER BY activity_points DESC LIMIT 10");
$sql2 = mysql_query("SELECT * FROM phpfox_user");
$peopleCount = mysql_num_rows($sql); // count the output amount
if ($peopleCount > 1) {
while($row = mysql_fetch_array($sql)){
$user_id = $row["user_id"];
$activity_points= $row["activity_points"];
$activity_total= $row["activity_total"];
//$user_id = $user_id = $full_name
$account = mysql_num_rows($sql2); // count the output amount
if ($account > 0) {
while($row = mysql_fetch_array($sql2)){
$user_id1 = $row["user_id"];
$full_name = $row["full_name"];
}
if ($user_id == $user_id1){
$staticList .= '<table width="100%" border="1" cellspacing="0" cellpadding="6">
<tr>
<td width="17%" valign="top">' . $full_name . '</td>
<td width="83%" valign="top">' . $activity_points . '<br /></td>
</tr>
</table>';
}
else{
echo "No Dice";
}
}
}
}
?>
</head>
<body>
<?php echo $staticList; ?>
</body>
</html>
谢谢!
答案 0 :(得分:0)
使用user_id加入两个表,如此
SELECT
phpfox_user.full_name
FROM phpfox_user
INNER JOIN phpfox_user_activity ON phpfox_user.user_id = phpfox_user_activity.user_id
WHERE phpfox_user_activity.activity_points = ??
ORDER BY phpfox_user_activity.activity_points DESC LIMIT 10
您可能还需要在where子句中指定活动点的数量!
修改强>
对于php代码,也许是这样的
$sql = mysql_query("SELECT phpfox_user.full_name, phpfox_user_activity.* FROM phpfox_user INNER JOIN phpfox_user_activity ON phpfox_user.user_id = phpfox_user_activity.user_id WHERE phpfox_user_activity.activity_points = ?? ORDER BY phpfox_user_activity.activity_points DESC LIMIT 10");
$peopleCount = mysql_num_rows($sql); // count the output amount
if ($peopleCount > 1) {
while($row = mysql_fetch_array($sql)){
$full_name = $row["full_name"];
$user_id = $row["user_id"];
$activity_points= $row["activity_points"];
$activity_total= $row["activity_total"];
答案 1 :(得分:0)
WHERE
时,为什么在第一个查询中没有绑定的变量/值? ...WHERE activity_points ORDER...
mysqli_* prepared statement
而非弃用mysql_*
来阻止SQL injections。您的查询如下:
SELECT phpfox_user_activity.activity_points, phpfox_user_activity.activity_total, phpfox_user.full_name
FROM phpfox_user
INNER JOIN phpfox_user_activity ON phpfox_user.user_id = phpfox_user_activity.user_id
WHERE phpfox_user_activity.activity_points = '' /* PUT A VALUE INSIDE THAT CLAUSE */
ORDER BY phpfox_user_activity.activity_points DESC LIMIT 10
如果您希望看到prepared_statement
的一瞥,请选择:
$db_host = "localhost";
$db_username = "******";
$db_pass = "*********";
$db_name = "******";
/* ESTABLISH CONNECTION */
$con = new mysqli($db_host, $db_username, $db_pass, $db_name);
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
if($stmt = $con->prepare("SELECT phpfox_user_activity.activity_points, phpfox_user_activity.activity_total, phpfox_user.full_name FROM phpfox_user INNER JOIN phpfox_user_activity ON phpfox_user.user_id = phpfox_user_activity.user_id WHERE phpfox_user_activity.activity_points = ? ORDER BY phpfox_user_activity.activity_points DESC LIMIT 10")){
$stmt->bind_param("s",$variableToBindOnYourQuery); /* REPLACE NECESSARY VARIABLE TO BE BIND IN YOUR WHERE CLUASE */
$stmt->execute();
$stmt->bind_result($activity_points,$activity_total,$full_name); /* BIND RESULT TO THIS VARIABLES */
while($stmt->fetch()){
?>
<table width="100%" border="1" cellspacing="0" cellpadding="6">
<tr>
<td width="17%" valign="top"><?php echo $full_name; ?></td>
<td width="83%" valign="top"><?php echo $activity_points; ?><br /></td>
</tr>
</table>
<?php
} /* END OF WHILE LOOP */
$stmt->close();
} /* END OF PREPARED STATEMENT */