Hello
I'm trying to get a function written to make connecting to the database quicker. Currently, I currect using this sequence of commands, which I beliece is very standard:
PHP Code:
$link = mysql_connect ('localhost', 'username', 'password') or die (mysql_error());
$query = "SELECT * FROM payments";
$result = mysql_db_query ('myaccount', $query, $link) or die (mysql_error());
I've just started using functions to make my PHPing quicker, and have been successful so far. However, I would like to make the $link and the $result into functions, because I use them most frequently, and they are the same every time.
I'm running PHP 4.3.9 and mySQL 4.0.21.
If I define the $link function as :
PHP Code:
<?php
function fn_db_connect()
{
mysql_connect ('localhost', 'username', 'password') or die (mysql_error());
}
?>
and then set:
$link = fn_db_connect();
it doesn't work, and says that $link does not exists when it tries to call it in $result.
I have seen thinks like:
PHP Code:
$conn = fn_db_connect();
$result = $conn->query("SELECT * FROM payments");
however, that didn't work. I wondered if that was only something one could do in PHP5.
My question is, does the above command work (and I just screwed it up when I tried)? How can I make $link be a variable of the function fn_db_connect() ?
Many thanks, and any suggestions to get me back on track are very welcome.
Have a nice day
hamish