PDA

View Full Version : simple question (very simple)


loren
02-12-2004, 02:43 AM
say i have a function link so


<?
include "config.php";

$connect = mysql_connect($db_host, $db_user, $db_pass) or die("Could not connect:" . mysql_error());
mysql_select_db('db_name', $connect) or die("Could not connect:" . mysql_error());


?>


and in the same code i have a function like so


<?

$connect = mysql_connect($db_host, $db_user, $db_pass) or die("Could not connect:" . mysql_error());
mysql_select_db('db_name', $connect) or die("Could not connect:" . mysql_error());

function One($db_name, $db_host, $db_user, $db_pass) {

$connect = mysql_connect($db_host, $db_user, $db_pass) or die("Could not connect:" . mysql_error());
mysql_select_db('db_name', $connect) or die("Could not connect:" . mysql_error());

}


function Two($db_name, $db_host, $db_user, $db_pass) {

$connect = mysql_connect($db_host, $db_user, $db_pass) or die("Could not connect:" . mysql_error());
mysql_select_db('db_name', $connect) or die("Could not connect:" . mysql_error());

}

?>


Is it really necessacary to put the $connect?

i was looking at a script that did this.... and i thought for sure i could use a global $connect;, and if i can just use global connect in mysql_select_db($db_name, $connect);
or do i just use connect.. can someone please try to clear this up?

Celtboy
02-12-2004, 05:58 AM
I'm confused about something...

Why would you even have to functions that are exactly the same except for name? I mean signature, return, everything?

Just doesn't make sense....no need for it....

further, you could use global $connect, I should think.

Secondly, you don't even technically NEED "$connect = "

while the function mysql_connect() does in fact have a return type, it isn't required that you store that in a variable. you could:


<?php
function One($db_name, $db_host, $db_user, $db_pass) {

mysql_connect($db_host, $db_user, $db_pass) or die("Could not connect:" . mysql_error());
mysql_select_db('db_name') or die("Could not connect:" . mysql_error());
}?>

HTH,
-Celt

Spookster
02-12-2004, 02:22 PM
Loren,

You need to make sure you read our posting guidelines section 2

http://www.codingforums.com/postguide.htm