PHP: mysql_query and this connection explanation

I recently started learning PHP and found out that the general way to connect to the database is:

// create connection to database
$connection = mysql_connect("localhost", "root", "password")
// Select database
$db_select = mysql_select_db("myDB", $connection);
// and finally the query..
$result = mysql_query("SELECT * FROM table", $connection);

Now my question is: why should we use $ connection in the third step ?! since we use the database "myDB", I expect to write the third step as follows:

// and finally the query..
$result = mysql_query("SELECT * FROM table", $db_select);

but it seems that not the way it is done in php. can someone explain this why?

+3
source share
4 answers

, . - , , , ( ). , , .

( , , . MySQL .)

+3

. . , . , , , .

<?php
$forum = mysql_connect(...);
$users = mysql_connect(...);

$me = mysql_query("", $users);
$last_forum_post = mysql_query("", $forum);
?>

, .

+2

mysql_query ( string $query [, resource $link_identifier ] )

mysql_query() → ( , ).

  • query → SQL-.

  • link_identifier → , , mysql_connect(). , , mysql_connect() . , E_WARNING.

+1

mysql_select_db() .
. mysql . USE dbname.

In addition, you can work with two or more databases simultaneously in one connection.

Thus, it makes no sense to use a database resource instead of a connection resource.

+1
source

All Articles