Doubt with php database connection with mysql

Asked

Viewed 419 times

3

I’m learning about php and mysql, I see several tutorials on the internet and one question that came up is, I see that create several variables and then put these variables in the command, example:

$servidor = "localhost";
$usuario = "root";
$pass = "";
$dbname = "vendas";
$conn = mysqli_connect($servidor, $usuario, $pass, $dbname);

Why don’t you just put it in the driver’s seat?? example:

$conn = mysqli_connect('localhost','root','','vendas');

Which goal to use the variables in this case ?

  • 1

    Basically both are personal choices. In practice, neither way is "better" or "worse" than the other, and they work the same way. If you have a connection only at the beginning of the code, do not disturb anything put straight into the function. If you will use this data after other logical parts of the code, separating the variables at the beginning of the code is easier to organize. Now, as in the first example, where the connection function is right next to the variables, it doesn’t make much sense. In short: give anyway, use whatever is convenient or desirable for your specific case.

  • 1

    I understood now, as you commented "as in the first example, where the connection function is right after the variables, it doesn’t make much sense", also for me it didn’t make any sense. But if you are going to use this data elsewhere is better put in variables, thanks for the explanation.

  • I ended up posting as an answer to be able to complement.

2 answers

5


Basically both are personal choices. In practice, neither way is "better" or "worse" than the other, and they work the same way.

If you have a connection only at the beginning of the code, do not disturb anything put straight into the function. If you will use this data after other logical parts of the code, leaving the data in variables at the beginning of the code makes it easier to maintain.

Now, as in the first example, where the connection function is right next to the variables, it doesn’t make much sense. In short: give anyway, use whatever is convenient or desirable for your specific case.

It is not part of the question, but I think it is good to comment that more important than separating the variables, for reuse purposes, is to have the connection in a separate file, for example dbconnect.php that makes the connection for you, and on all pages that use DB you use require_once( 'dbconnect.php' );, so will only have a place to adjust in case of a server change or configuration in general.


Note: in principle the question appears to be one that generates answers "based on opinion", which is usually reason for closure, but as it is clarification to know if there is any reason, and not "which is better", I thought it appropriate to answer

0

It’s better for organization, and the like, for when someone other than you can understand more easily. And when you transfer to an online server, you don’t have to look for where you connected to the bank, you just change the value of the variables. They are good practices of organization and reuse of the code. I hope I’ve helped

  • My dear, quote "e tal" can mean a lot that needs to be explained or can mean absolutely nothing. In case, "e tal" means what ?

  • 1

    Thanks for the comment, I edited and wrote in a way of better understanding.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.