1

i started learning php and my problem with understanding that in many cases functions are defined as variables. can anyone explain me what is the actual purpose behind it and example:

$db_name= "testdb";
$table_name= "my_music";
// below we create the connection to the database.
$connection= @mysql_connect("localhost", "myusername", "mypassword") or die(mysql_error());
// below we are gonna define what database to connect.

$db= @mysql_select_db($db_name, $connection) or die(mysql_error());

//below creates the sql statement so it can insert the data into the table.

$sql= "
INSERT INTO $table_name
(format, title, artist_fn, artist_ln, rec_label, my_notes, date_acq) 
VALUES 
('$format', '$title', '$artist_fn', '$artist_ln', '$rec_label', '$my_notes', '$date_acq')";

//below is the function that holds the result of the query function
$result= @mysql_query($sql, $connection) or die(mysql_error());
 // end of the php for inserting data into database.
?>

As you see here $db is not used in any following codes and this code is perfectly connecting to the database and does the work. what would be the alternative way to help me understand it better. i have been searching it over the internet but could not find any helpful answer Thanks in advance.

4

2 Answers 2

3

Preamble: The mysql extension is marked as deprecated. For new script and esp. when learning that php/database stuff please choose another mysql api, see http://docs.php.net/mysqlinfo.api.choosing


The return value of mysql_connect() assigned to $db is not a function but the mysql connection resource. You can and should pass it to the subsequent calls to mysql_select_db(..., $db), mysql_real_escape_string(..., $db), mysql_query(..., $db), mysql_error($db) (except directly after mysql_connect) and so on.
The last connection established by mysql_connect is stored "within" the extension. If you leave the link parameter out when calling e.g. mysql_query() the extension will use this last connection. But it also "allows" the extension to establish a new default connection if there is no valid link stored, see mysql.default_* at http://docs.php.net/mysql.configuration.
So e.g. if you have a script without proper error handling and without passing the link resource to the subsequent function calls like

<?php
mysql_connect('server1', ..., ...);
mysql_select_db('somedatabase');
mysql_query('INSERT INTO ....');

it might actually "work" but put the data into a different MySQL server than you intended it to do.
Therefore always install proper error handling (whatever "proper" means in your context) and always pass the link resource to the mysql_* functions.

<?php
checkParameters() or onParameterError();
$db = mysql_connect('server1', ..., ...) or onMyErrorHandler(null);
mysql_select_db('somedatabase', $db)  or onMyErrorHandler($db);
$param = mysql_real_escape_string($_POST['value'], $db);
$result = mysql_query('INSERT INTO ....', $db)  or onMyErrorHandler($db);
2
  • no need to use fictional onMyErrorHandler(). use trigger_error() instead. it will serve you better and out of box Mar 13, 2013 at 8:07
  • Thanks for the input: I'll add that the actual error handling is another topic (one over one could write books ...oh, wait, people did write books on that subject ;-) ) and is not covered here. I justed didn't want to put the usual or-die-die-DIE tutorial shortcut there.
    – VolkerK
    Mar 13, 2013 at 8:15
3
$db= @mysql_select_db($db_name, $connection) or die(mysql_error());

In here, the result of mysql_select_db() (a boolean) is assigned to $db here. As you rightfully pointed out, it's not used further down the code and the statement could therefore be written as:

@mysql_select_db($db_name, $connection) or die(mysql_error());

Note that the use of an error control operator is not recommended, learn about error logging and how you can avoid those or die() constructs.

Also, the old mysql_ functions shouldn't be used either; it would be better to learn either PDO or mysqli.

Not the answer you're looking for? Browse other questions tagged or ask your own question.