简体   繁体   中英

PHP OOP variables from database

I am currently working on a CMS and am learning how to use OOP.

My question is: How do I take variables set in a mysql database and use them on my website?

Lets say I have 2 columns, one called var_name and var_content .

How would I use it so that $var->sitetitle; echos out whatever it matches up to in my database?

I don't know what this is called, if anyone could lead me in the right direction, I appreciate it!

Right now, I have this:

require_once("classes/database.class.php");
$database = new database();
$database->set_value('sitevariables', $database_prefix . "sitevariables");
$database->set_value('host', $database_host);
$database->set_value('pass', $database_pass);
$database->set_value('user', $database_user);
$database->set_value('table', $database_table);

I wanted it so I could use $database->host to get my mysqli server. It's basically the same thing as I want to do, except it takes from the database to get and set the values

you can define a function to get variables, once your database connection is set up (I sadly not think it is possible to query the database for credentials to connect to that vary database. kind of spiraling thing there.)

this takes into account that var() is a member function of your database() class. Since I don't have your database class, I don't think the way I'll query the database is like yours, but you will be able to fix that.

function var($var_name) {
  $stmt = $this->query('SELECT var_content FROM variables WHERE var_name=?');
  $stmt->execute(array($var_name));
  return $stmt->fetch(PDO::FETCH_COLUMN);
}

$sitetitle = $database->var('sitetitle');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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