问题
I have this file called db.php which makes the connection to the database.
<?php
define("DB_HOST", "localhost");
define("DB_NAME", "login");
define("DB_USER", "admin");
define("DB_PASS", "123");
mysql_connect(DB_HOST, DB_USER, DB_PASS) OR die("Falha na ligação.");
?>
What I wanted to do is to use that DB_NAME define in another file so that I just need to change it in the db.php in case I have to change the database name.
Here's an example of where I want it to be aplied.
$qp = "UPDATE login.users SET palpiteatual = '".$_POST['atextfield']."' WHERE user_name = '".$_SESSION['user_name']."'";
Instead of having login.users, I tried this following ways without succeeding.
$qp = "UPDATE '"DB_NAME"'.users SET palpiteatual = '".$_POST['atextfield']."' WHERE user_name = '".$_SESSION['user_name']."'";
$qp = "UPDATE "+DB_NAME+".users SET palpiteatual = '".$_POST['atextfield']."' WHERE user_name = '".$_SESSION['user_name']."'";
Not sure how is the exact syntax to use it this way. Thanks in advance
回答1:
$qp = "UPDATE ".DB_NAME.".users SET palpiteatual...";
Note that, if you don't have multiple connection and multiple database, no need to use DB_NAME
回答2:
String concatenation in PHP is done with the concatenation operator .
$qp = "UPDATE ".DB_NAME.".users
来源:https://stackoverflow.com/questions/15677348/insert-a-php-constant-into-a-string