0

I'm developing a setup class to manage some parameters stored in the database and I am trying to make a class effective and shorter so, I did this:

First, I add a db.php file where the database is configured and connected, after that I added the parameters as private attributes. To process them in a better way all are included into an Array, so I build the query in the variable 'consulta' processing the information and retrieve one by one the values from the db

<?php
  require 'db.php';

  class setup {
private $lenguaje;
private $charset;
private $sitio_titulo;
private $sitio_descripcion;
private $kewords;
private $autor;
private $path_css_frontend;
private $path_css_backend;
private $path_modernizr;
private $path_jquery;
private $logo_url;
private $copyright;
private $dbconn;
private $site_version;

//edit - code separated only for visibility, part of same class

    public function __construct() {
    $this->dbconn = new database ();
}
private function fillData() {
    $valores = array (
            lenguaje,
            charset,
            sitio_titulo,
            sitio_descripcion,
            kewords,
            autor,
            path_css_frontend,
            path_css_backend,
            path_modernizr,
            path_jquery,
            logo_url,
            copyright,
            dbconn,
            site_version
    );
    $this->getData($valores);
}

//edit - code separated only for visibility, part of same class

public function getData($columnName) {

    while($columnName){

        $consulta = 'SELECT $columnName from config LIMIT 1';

        $this->dbconn->query ( $consulta );

        $this->dbconn->execute ();

        $r = $this->dbconn->fetch (); //

        '$this->'.$columnName = $r;

    }

   }

    ?>

did I something wrong?

1
  • 1
    Why don't you at least run your code and fix numerous syntax errors? Commented Sep 9, 2013 at 3:11

3 Answers 3

2

Sure.
You just managed to make it long and ineffective.
Here is an improved version:

<?php
require 'db.php';
$dbconn = new database();

class setup
{
    public function __construct($dbconn)
    {
        $this->dbconn = $dbconn;
        $this->fillData();
    }

    private function fillData()
    {
        $data = $this->getData();
        foreach ($data as $key => $value)
        {
            $this->$key = $value;
        }
    }
    private function getData()
    {
        $sql = 'SELECT * FROM config';
        return $this->dbconn->query($sql)->fetchAll(PDO::FETCH_ASSOC);
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

I'm surprised you're letting the database class slide. Seems to support the same method set as PDO and I know how much you love PDO wrappers
Honestly, I am no wonder it didn't. If you have as much errors in your other files as have in this one.
0

First quote the values of your array or they will be considered as constants.

$valores = array (
            'lenguaje',
            'charset',
            'sitio_titulo',
            'sitio_descripcion',
            'kewords',
            'autor',
            'path_css_frontend',
            'path_css_backend',
            'path_modernizr',
            'path_jquery',
            'logo_url',
            'copyright',
            'dbconn',
            'site_version'
    );

Next, the way you are using while loop is wrong. Combine the array values and send one query.

public function getData($columnName) {

    $columnName = implode(",", $columnName);
    $consulta = 'SELECT $columnName from config LIMIT 1';

    // Query Now

}

1 Comment

You don't typically quote MySQL identifiers (column names)
-1

Finally I've changed the architecture of the table and I made more easier the class to get the params

<?php
    require 'db.php';

    class setup {

      public function __construct() {

    $this->dbconn = new database ();

      }

      public function getParameter($p) {

    $sql = 'SELECT param_value from parameters where param_name = :par';

    $this->dbconn->query($sql);

    $this->dbconn->bind(':par', $p);

    $r = $this->dbconn->getSingleRecord();

    return $r['param_value'];
}

?>

The following functions are involved in the solution from the file db.php

public function query($query) {

    if ($this->isConnected == False) {

        $this->Connect ();

    } else {

        $this->q = $this->pdo->prepare ( $query );

    }
}


public function bind($param,$value,$type = NULL){

    if (is_null($type)) {

        switch (true) {
            case is_int($value):
                $type = PDO::PARAM_INT;
                break;
            case is_bool($value):
                $type = PDO::PARAM_BOOL;
                break;
            case is_null($value):
                $type = PDO::PARAM_NULL;
                break;
            default:
                $type = PDO::PARAM_STR;
        }
    }

    $this->q->bindValue($param,$value,$type);

}

  public function getSingleRecord(){

    $this->execute();

    return $this->q->fetch(PDO::FETCH_ASSOC);

}

1 Comment

This is terribly designed. Your bind method depends on the successful execution of the query method yet it does no sanity checks. I really don't see what you're adding to the suite of PDO classes other than useless complexity

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.