/    Sign up×
Community /Pin to ProfileBookmark

few database q’s

In the code below how come in the function is has $err, $id but in the place it loads the function it has mysql_connect”, $sql_id. I thought they have to be the same???

Also what does the @ do before hte mysql and finaly how come the code is connecting like this. the $sql show errors is a function to say whether errors are shown or not and that is usually set to 1 to say show the errors so does it mean the same as

[code=php]
if {$sql_show_errors = 1) { ShowErrorMsg(“mysql_connect”, $sql_id) }
[/code]

but what does the $sql_id bit mean as there is no variable by that in the document? what is it doing

[code=php]
if (!($sql_id = @mysql_connect($SQL_HOST, $SQL_USER, $SQL_PWD))) {

if ($SQL_SHOW_ERRORS <> 0) ShowErrorMsg(“mysql_connect”, $sql_id);

}

// the ShowErrorMsg Function

function ShowErrorMsg($err, $id) {

switch ($err) {

case “mysql_connect”:
$msg[1] = “Couldn’t connect to MySQL server”;
$msg[3] = “Check if the name of the MySQL server, user name & password specified in CompuCLICK.php are correct!”;
break;
if (@mysql_error($id) <> “”) {

$msg[2] = @mysql_error($id);
} else {

$msg[2] = “Unknown”;
}

die(“<hr><font color=”#ff0000″><b>” . $msg[1] . “</b><br>Reason: ” . $msg[2] . “<p></p>” . $msg[3] . “</font><hr><br>”);
}
[/code]

Also what is better

[code=php]
//using vairbales
$SQL_USERNAME = “k0r54”;

//or a define
define(“SQL_USERNAME”, “k0r54”);
[/code]

what is the difference.

Sorry about all the Q

Thanks
k0r54

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@ShrineDesignsMar 02.2005 — $sql_id is right here[code=php]if (!($sql_id = @mysql_connect($SQL_HOST, $SQL_USER, $SQL_PWD))) {[/code]you can assign variables within statements in php

the @ symbol suppresses most errors
Copy linkTweet thisAlerts:
@NogDogMar 02.2005 — When you define a function, the variable names you use in the parameter list are the names that will be used within the function. When you later call that function from elsewhere in your code, the values of the parameters you use in the call will be assigned to the function's parameters in the same order. So for this function...
[code=php]function my_func($param1, $param2)
{
echo "You called this function with parameter values '$param1' and '$param2'.";
}
[/code]

...you could call it with any combination of two variables and/or literal values...
[code=php]my_func("Value 1", "Value 2");
$v1 = "Value 1";
$v2 = "Value 2";
my_func($v1, $v2); # same result as first call
[/code]

The "@" is used to suppress error reporting. Generally I don't like this and prefer to make sure I do proper error-checking and handling.

$sql_id is going to be set to the return value from the mysql_connect() function. Notice that it is followed by an equal sign ("="), not a comparison operator ("=="). The return value will also be used within the if() check, in this case checking to see if it is FALSE (due to the "!" operator).

[code=php]
# assigns a value to a variable, which can later be changed:
$SQL_USERNAME = "k0r54";

# defines a constant and assigns a value to it. It can not later be reassigned a new value.
define("SQL_USERNAME", "k0r54");
[/code]
Copy linkTweet thisAlerts:
@k0r54authorMar 02.2005 — Ok so if i want to declare the server username is would be best to do it with define, is that it.

Also with the @ what do you mean can get rid of errors? so for instance if i was copying something it is didn't meet the DATABASES validations (i.e char length) then it would continue?? where if the @ wasn't there it would fail?

Thanks for the great answers

Thanks

k0r54
Copy linkTweet thisAlerts:
@NogDogMar 02.2005 — For more info on '@', see http://us3.php.net/manual/en/language.operators.errorcontrol.php .

As far as when to define() a constant versus using a variable, some of it just comes down to personal taste. Just remember that if you use define(), its value cannot be modified later if you change your mind without changing it to a variable. Also note that contant names do not begin with a '$', they must begin with a letter or underscore. Typically people will use all uppercase letters to make it stand out from the rest of the variable names as a constant name. For more info on constants, see http://us3.php.net/manual/en/language.constants.php .
×

Success!

Help @k0r54 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...