1
$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> ";
} 

I am getting:

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

Is there a way to directly pull the data? I tried doing something like $row['users.NAME'] as well but still failed.

Jason
  • 811
  • 1
  • 12
  • 26

2 Answers2

1

MYSQL_ASSOC Return assocative array not numeric. So use

$row['NAME'];
Saty
  • 22,443
  • 7
  • 33
  • 51
0

Try this,

    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> ";
    } 
Hytool
  • 1,358
  • 1
  • 7
  • 22