有没有办法在Functions.php文件中将PHP语句存储在PHP中?

Let's say I have a piece of code that is getting all records from a particular table. Such as:

mysql_select_db($database_localhost, $localhost);
$query_getTechs = "SELECT * FROM zip_tech ORDER BY tech_name ASC";
$getTechs = mysql_query($query_getTechs, $localhost) or die(mysql_error());
$row_getTechs = mysql_fetch_assoc($getTechs);
$totalRows_getTechs = mysql_num_rows($getTechs);

Is there a way to make this a function where the table name and sort by column are part of the function's arguments? So I could put something such as:

get_records('zip_tech','tech_name');

I end up with a lot of these on some things I'm trying to build as I learn PHP. It seems like having to use blocks of code like that repeatedly makes things more convuluted and less clean looking.

What you're looking for are, well, functions.

http://www.php.net/manual/en/functions.user-defined.php A function for get_records would look something like this:

function get_records($from, $order){
    $query = 'SELECT * FROM '.$from.' ORDER BY '.$order.' ASC';
    //whatever else you wanted
    return $query;
    }

And you'd call it like this:

$query = get_records('zip_tech','tech_name');

You could also create a function without the return value, that preforms what you need. And call it by just invoking the function name and parameters.