0

I'm trying to clean up my code and I was wondering if I can place my mysql query that I call several times into an external php function that I include at the top of the file. Everytime I have tried it I have received an error, so I am wondering if it has something to do with my still being relatively new to php. It all works until I place the code in a new file (functions.php)

Here are some snippets:

This is from the page to be displayed

<?php
    retreive_user_data();
    while ($result = mysql_fetch_array($queryobj)) {
        echo $result['about'];
    }
?>

And here is the php file I'm including

<?php 
    function retrieve_user_data() {                                 
        $query = "SELECT * FROM users, editables WHERE users.user = editables.user";
        $queryobj = mysql_query($query);
        return $queryobj;
    }
?>
1
  • It's telling me that queryobj is an unidentified variable Commented Aug 13, 2012 at 4:58

1 Answer 1

2

Change;

retreive_user_data();

To:

$queryobj = retreive_user_data();

Some other notes:

The mysql functions are depreciated, switch to mysqli.

If you use them, use mysql_fetch_assoc() not mysql_fetch_array().

Condense retrieve_user_data() to one line:

function retrieve_user_data() {                                 
  return mysql_query("SELECT * FROM users, editables WHERE users.user = editables.user");
}

Your query style is also depreciated. Change it to:

SELECT * FROM users JOIN editables USING user

PS. Try to find some more modern tutorials to learn from. Look for use of mysqli - that's a good signal it's reasonably new.

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

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.