简体   繁体   English

将多个PHP变量分配给MySQL值

[英]Assign multiple PHP variables to MySQL values

I'm doing a simple CMS for a website, it has been all done like this: 我正在为网站做一个简单的CMS,它的完成过程如下:

(config.php) (config.php)

<?php
$title= "My website title";
$something= "Some text here";
// There are around 300 other vars like that.
// All text from the website comes from this file.
?>

(example.php) (example.php)

<php require ('config.php'); ?>
<html>
    <head>
        <title><?php echo $title ?></title>
    </head>
    <body>
        <p><?php echo $something ?></p>
    </body>
</html>

But now I want to pass it to MySQL. 但是现在我想将其传递给MySQL。 I didn't do it at first because I was really noob at coding, but during the process of getting everything together, I felt ready to do it using a database and a admin panel. 起初我没有这样做,因为我确实不擅长编码,但是在将所有内容组合在一起的过程中,我准备使用数据库和管理面板来进行此操作。

Assuming I already have a MySQLi connection established, how can make config.php variables pull his data from the database, instead of a static string? 假设我已经建立了MySQLi连接,如何使config.php变量从数据库而不是静态字符串中提取其数据?

How can I do that without overload the system with hundreds of queries per page load? 如何在不使系统每页加载数百个查询的情况下使系统过载?

If you want different variables in different pages from same file. 如果要在同一文件的不同页面中使用不同的变量。 Then, get your current page name by, 然后,通过以下方式获取当前页面名称:

$page_name = substr($_SERVER["SCRIPT_NAME"],strrpos($_SERVER["SCRIPT_NAME"],"/")+1);

On the basis of page name pull out your information from database. 根据页面名称从数据库中提取您的信息。

config.php config.php

<?php
  if($page_title == 'example.php'){
    $title= $db_title;               // This variable will come from database...
    $something= $db_something;
  }
  else if($page_title == 'example_2.php'){
    $title= $db_title_2;
    $something= $db_something_2;
  }
  etc...
?>

You can use same config.php file in every pages... 您可以在每个页面中使用相同的config.php文件...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM