Insert a PHP constant into a string

痴心易碎 提交于 2021-02-11 14:22:42

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!