How to send variable value into class php?

2

I've been perfecting secure database connection techniques, and got to the script below.

<?php class query_sql {
protected static $conect;
private function __construct() {

    $mysql_host = "";
    $mysql_base = "";
    $mysql_user = "";
    $mysql_pass = "";

    try { self::$conect = new PDO("mysql:host=" . $mysql_host . "; dbname=" . $mysql_base, $mysql_user, $mysql_pass);
        self::$conect -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        self::$conect -> exec("set names utf8");
        $conect = null; }

    catch(PDOException $error) {
        echo $error -> getMessage(); } }

public static function conect_sql() {
    if (!self::$conect) {
        new query_sql; }
    return self::$conect; } }

$bind_sql = query_sql::conect_sql(); ?>

My next step is to learn how to submit values from

    $mysql_host = "";
    $mysql_base = "";
    $mysql_user = "";
    $mysql_pass = "";

along with the values below

<?php $list_information = $bind_sql -> prepare("SELECT user_email, user_name FROM usuarios WHERE user_email = :user_email LIMIT 1");
$list_information -> bindValue(":user_email", "@");
$list_information -> execute();
$list_information = $list_information -> fetchAll(); ?>

just list above and compare below (login as an example)

<?php $compare_information = $bind_sql -> prepare("SELECT user_email, user_name FROM usuarios WHERE user_email = :user_email AND user_name = :user_name LIMIT 1");
$statement_array = array(":user_email" => "@", ":user_name" => "*");
foreach($statement_array as $array_key => $array_value) {
    $compare_information -> bindValue($array_key, $array_value); }
$compare_information -> execute();
$compare_information = $compare_information -> fetchAll(); ?>

To receive it so

<?php foreach($list_information as $list_information) {
echo $list_information["user_email"] . " | " . $list_information["user_name"]; } ?>

and so

<?php foreach($compare_information as $compare_information) {
echo $compare_information["user_email"] . " | " . $compare_information["user_name"]; } ?>

I just need some help on what to learn so that I can send the variables into the class so they do not have to be inside, the reason is that I have a database for each information, the destination of this script uses 4 databases so I would define which database to use in each prepare (select, delete, att etc.)

    
asked by anonymous 28.09.2016 / 10:44

1 answer

2

An alternative is to create an (private) helper method that reads a file with the connection parameters and returns them to the constructor.

Let's say the file has the values separated by ; (this is just an example! do not leave user and password exposed)

localhost;loja2015;usuario;senha 

php:

private function getConnectionArgs($file="caminho padrão"){
    return explode(';', file_get_contents($file));
}

Your constructor should add the call to the new method:

private function __construct() {
   $args = self::getConnectionArgs();
   self::$conect = new PDO("mysql:host=" . $args[0] . "; dbname=" . $args[1], $args[2], $args[3]);

You can relax a bit more if you pass the file on the conect_sql() call that will first pass it to the constructor.

    
28.09.2016 / 14:26