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.

Touki
  • 7,465
  • 3
  • 41
  • 63
Justin
  • 81
  • 1
  • 2
  • 11
  • 3
    Start by learning [PDO](http://php.net/pdo) as well. – Ja͢ck Mar 13 '13 at 07:53
  • 3
    That is not "declaring the function as variable", it just assigns the return value of the function to a variable. Apparently that's unnecessary since it's never used, as you say. – deceze Mar 13 '13 at 07:55
  • What specifically do you not understand? Can you phrase that? – hakre Mar 13 '13 at 07:56
  • [**Please, don't use `mysql_*` functions in new code**](http://bit.ly/phpmsql). They are no longer maintained [and are officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the [**red box**](http://j.mp/Te9zIL)? Learn about [*prepared statements*](http://j.mp/T9hLWi) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://j.mp/QEx8IB) will help you decide which. If you choose PDO, [here is a good tutorial](http://www.brightmeup.info/article.php?a_id=2). – NullPoiиteя Mar 13 '13 at 08:09

2 Answers2

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);
VolkerK
  • 95,432
  • 20
  • 163
  • 226
  • no need to use fictional onMyErrorHandler(). use trigger_error() instead. it will serve you better and out of box – Your Common Sense Mar 13 '13 at 08: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 '13 at 08: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.

Community
  • 1
  • 1
Ja͢ck
  • 170,779
  • 38
  • 263
  • 309