php mysql获取,PHP从MySQL数据库中获取数据

So I'm trying to fetch data in a many-to-many relationship.

So far I have this, which finds the user:

$user = $_SESSION['user'];

$userID = mysql_query("SELECT * FROM users WHERE user='$user'") or die(mysql_error());

And I know that to echo this information I have to put it in an array like so:

while ($r = mysql_fetch_array($userID)) {

echo $r["0"];

}

This works fine, but when I try to find this variable in another table, I'm not sure what to use as the variable:

$projects = mysql_query("SELECT projects_ID FROM projects_users WHERE users_ID='???'") or die(mysql_error());

I've tried replacing ??? with $userID and $r, but to no avail. I know the code works because it's fine when I put a user ID in manually - where have I gone wrong?

解决方案

You can get your projects with one query:

$user = mysql_real_escape_string($_SESSION['user']);

$query = mysql_query("SELECT pu.projects_ID FROM users u

INNER JOIN projects_users pu ON (pu.users_ID = u.users_id)

WHERE u.user='$user'") or die(mysql_error());

$result = mysql_query($query) or die(mysql_error());

while ($row = mysql_fetch_assoc($result)) {

echo $row['projects_ID'];

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值