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");
	}
?>
Member Avatar for diafol

use mysqli instead.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.