将 PHP 常量插入字符串 [英] Insert a PHP constant into a string

查看:22
本文介绍了将 PHP 常量插入字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 db.php 的文件,它可以连接到数据库.

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.");
?>

我想要做的是在另一个文件中使用 DB_NAME 定义,以便我只需要在 db.php 中更改它,以防我必须更改数据库名称.

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']."'";

我没有使用 login.users,而是尝试了以下方法,但没有成功.

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

推荐答案

$qp = "UPDATE ".DB_NAME.".users SET palpiteatual...";

注意,如果你没有多个连接和多个数据库,则不需要使用 DB_NAME

Note that, if you don't have multiple connection and multiple database, no need to use DB_NAME

这篇关于将 PHP 常量插入字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆