$sql = 'SELECT users.NAME, company.name
FROM users
LEFT JOIN (user_company_access, company) ON (users.USER_ID=user_company_access.company_id AND user_company_access.company_id=company.company_id)
WHERE users.USER_ID="1" AND company.company_id=users.USER_ID';
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
echo "USER ID :"+$row[0]+" <br> ";
}
我得到了:
Notice: Undefined offset: 0 in C:\xampp\htdocs\index.php on line 23
0
Notice: Undefined offset: 0 in C:\xampp\htdocs\index.php on line 23
0Fetched data successfully
有没有办法直接提取数据?我尝试过类似$ row [&#39; users.NAME&#39;]的内容,但仍然失败了。
答案 0 :(得分:1)
MYSQL_ASSOC返回关联数组而不是数字。所以使用
$row['NAME'];
答案 1 :(得分:0)
试试这个,
SELECT u.NAME AS username , c.name AS companyname
FROM users AS u LEFT JOIN user_company_access AS uc ON u.USER_ID=uc.company_id
LEFT JOIN company AS c ON uc.company_id=c.company_id
WHERE users.USER_ID="1" AND c.company_id=u.USER_ID
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
echo "USER ID :".$row[0]." <br> ";
//OR
// echo "USER ID :".$row['username']." <br> ";
}