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)
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());
return $result;

2. The code below shows how to use this across files
<?PHP require("dbconnect.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


Post a Comment

Links to this post:

Create a Link