Reputation: 83
This is my code:
include('connectTwo.html');
$query = "select * from users";
$result = mysql_query($query, $connectTwo);
while($row = mysql_fetch_array($result))
{
echo $row['username'];
$user_id = $row['user_id'];
$profile = getProfile($user_id);
echo $profile;
}
function getProfile($user_id)
{
$query = "select * from info where user_id='$user_id'";
$result = mysql_query($query, $connectTwo);
$row = mysql_fetch_assoc($result);
$profile = $row['profile'];
return $profile;
}
The function doesn't return anything. I know that the query is correct because I move it outside the function and it runs and gives me the value I'm looking for. Please post a comment if you are confused about what I'm asking.
Upvotes: 0
Views: 94
Reputation: 3382
You can try 2 methods:
Pass the database connection string as an argument to the function getProfile()
Like : getProfile($user_id, $connectTwo);
OR you can use global
keyword to use the connection string inside function getProfile()
function getProfile($user_id) {
global $connectTwo;
But above all the better approach has been pointed out by @zairwolf in the previous answer.
Note: Try to use mysqli_* or pdo * functions instead of mysql_* functions.
Upvotes: 1
Reputation: 14507
Try left join:
$query = "select a.*,b.profile from users a left join info b on b.user_id=a.user_id";
Then you can delete getProfile()
function.
If you still want to use function getProfile($user_id)
, global $connectTwo
in it otherwise you cant use it as it is undefined.
Upvotes: 3