Friday, June 16, 2006

[PHP - MYSQL] - Connect to database function

1. Create a file dbconnect.php with the code below (Paste this code and change the values in blue as you need)
<?PHP
function exec_query($sql)
{
$server = "server_name";
$username = "user_name";
$pwd = "pwd";
$db_name = "db_name";
$link = mysql_connect($server,$username,$pwd) or die (mysql_error());
mysql_select_db($db_name) or die (mysql_error());
$result=mysql_query($sql) or die (mysql_error());
mysql_close($link);
return $result;
}
?>


2. The code below shows how to use this across files
<?PHP require("dbconnect.php") ?>
<?PHP
$sql = "Select * from tbl_name";
$result = exec_query($sql);
while($row = mysql_fetch_array($result))
{
echo $row[0];
echo $row[1];

}
?>



Bookmark and Share

0 Comments:

Post a Comment