0

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.

7
  • In a function - $result = mysql_query($query, $connectTwo); What is $connectTwo? Commented Jan 24, 2015 at 18:31
  • In the top of the script notice the include('connectTwo.html');. That file connects to my database and create the $connectTwo variable which I use for the queries. The query variables have the queries inside and then I execute them. Commented Jan 24, 2015 at 18:32
  • 1
    you need to pass the mysql connection string into the function Commented Jan 24, 2015 at 18:32
  • @u_mulder well, $query is defined in the line before as a string (in both cases) and $connectTwo obviously should be a resource. Commented Jan 24, 2015 at 18:33
  • 1
    Thank you - @fortune! It worked! Thank you so much. So I guess that the function couldn't use the global $connectTwo variable and it had to be passed in. thank you so much. Post an answer and I will vote it best! Thank you again. I have been stuck on this for a couple hours. Commented Jan 24, 2015 at 18:35

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.