Your first problem is that you have not created a class for you object.
What you have here is a function.
Now, the reason your function doesn't work is because you have not passed it any parameters. I am guessing that db connection info is in passwords.php.
The reason you commented out code works is because it is in the same scope as the informaion from you passwords.php, but once you move you code into a function, the function has a different scope than the main part of the script, so you have to pass your information to the function as parameters.
Try this
<?php
require_once('//include/mysite/passwords.php');
/*
$dbconnect = mysql_connect($dbhost, $dbusername, $dbuserpass);
mysql_select_db('mydatabase');
$getdata = @mysql_query("SELECT * FROM mytable");
$usedata = mysql_fetch_array($getdata);
$namedisplay = $usedata["name"];
echo $namedisplay;
*/
function getData ($dbhost, $dbusername, $dbuserpass) {
//Note that these variables are copies of the ones passed in to this function
//So changing them here only changes them in this function
$dbconnect = mysql_connect($dbhost, $dbusername, $dbuserpass);
mysql_select_db('mydatabase');
$getdata = @mysql_query("SELECT * FROM mytable");
$usedata = mysql_fetch_array($getdata);
return $usedata;
}
$data = getData($dbhost, $dbusername, $dbuserpass);
$namedisplay = $data["name"];
echo $namedisplay;
?>
This code just makes your function work. You should read up on objects and classes to make this into an OO script