0

Hi Everyone,

Just a basic Conn function i would like to share with the community.

Any constructive criticism and tweaks are very welcome, im trying to optimize my entire code library, starting with the basics.

<?php 
funtion dbconn(){
	
	$db = "database_name" //<-- name of database being connected to 
	$server = "000.000.0.0"  //<-- your server address
	$user = "your_name"  //<-- username with sufficient privelages
	$pass = "your_pass"  //<-- password for $user
	
	$con = mysql_connect($server,$user,$pass) or die ("Could not connect to server, Check your Username and Password");
	mysql_select_db($db,$con) or die ("Could not connect to Database");
	$rs = mysql_query ($sql, $con);
	return $rs
}
/*
To Use this function simply:

$sql = "your sql here";
$rs = dbconn($sql);
$row = mysql_fetch_array($rs); or mysql_fetch_assoc($rs);   <-- which ever you prefer
$rows = mysql_num_rows($rs);

eg.

$sql = "SELECT A, B, C FROM Alpha WHERE ID > 0";
$rs = dbconn($sql);
$row = mysql_fetch_array($rs);
$rows = mysql_num_rows($rs);
for ($i=0;$i<$rows;$i++){
		
echo mysql_result($rs,$i,"A") . " and " . mysql_result($rs,$i,"B") . " and " . mysql_result($rs,$i,"C");
	}
?>
2
Contributors
1
Reply
3
Views
6 Years
Discussion Span
Last Post by diafol
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.