PHP OOP binding problem inside the method - php

I try so hard to create this method with prepare, bind, and execute inside the class. But I guess I don't have enough knowledge about it whatever I do I couldn't make it work. I'm looking google for hours. I know that below code is wrong about binding but Can someone show me the correct way to do binding inside this method?
class User {
protected static $db_table = "users";
protected static $db_table_fields = array('username', 'password', 'first_name', 'last_name');
public $id;
public $username;
public $password;
public $first_name;
public $last_name;
protected function properties() {
$properties = array();
foreach (static::$db_table_fields as $db_field) {
if (property_exists($this, $db_field)) {
$properties[$db_field] = $this->$db_field;
}
}
return $properties;
}
protected function clean_properties() {
global $database;
$clean_properties = array();
foreach ($this->properties() as $key => $value) {
$clean_properties[$key] = $value;
}
return $clean_properties;
}
public function create($params= []){
global $database;
$properties = $this->clean_properties();
$fields = ":" . implode("',:'", static::$db_table_fields);
$sql= "INSERT INTO " .static::$db_table . "(" . implode(",", array_keys($properties)) . ")
VALUES('". $fields ."')";
$stmt = $database->prepare($sql);
foreach ($fields as $field => &$params) {
$stmt->bindValue($field, $params);
}
if ($stmt->execute()) {
$this->id = $database->InsertId();
return true;
} else {
return false;
}
}

So I made my code like this for binding. I don't know if it is the correct way. I know I keep changing stuff. I guess it is the best way to learn to try different stuff.
//$param = array('x'=>$x, 'y'=>$y);
public function bind($param = [], $type=NULL){
foreach($param as $key=>&$value){
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->stmt->bindValue(':'.$key,$value);
}
return $this->stmt;
}

Related

Load database value with function

I didn't see any topic like this so I posted a new one.
First of all, I'm sorry about my bad English.
I want an public function who can storage in a global variable a value from database, and with that value the php script select the website templates. Can you help me?
private $page_title = null;
private $body_title = null;
private $body_content = null;
private $template = null;
private $settings = array();
private $file_template = 'content';
private $path = 'templates/';
private $parse_page = false;
global $template2;
public function GetHASH()
{
return $this->hash;
}
function Tema()
{
global $db,$user_class;
if($user_class->authorized)
{
$row = $db->sql("SELECT * FROM `ucp_users` WHERE `user_hash` = '".$this->GetHASH()."'");
$array = $db->get_array($row);
$template2 = $array['template'];
}
else
{
$template2 = '1';
}
return $template2;
}
function __construct() {
switch($template2) {
default:
{
$template_name = 'dsa/';
$template2 = 1;
}
break;
case 0: $template_name = 'lsrp/';
break;
case 1: $template_name = 'dsa/';
break;
}
$this->path = $this->path.''.$template_name;
}
Thank you.
When you instantiate your class, the constructor is called, at this time your tema method has not been invoked yet since it cannot be called without an instance. One solution would be to call your tema method in your constructor like this :
function __construct() {
$template2 = this->tema();
switch($template2) {
default:
{
$template_name = 'dsa/';
$template2 = 1;
}
break;
case 0: $template_name = 'lsrp/';
break;
case 1: $template_name = 'dsa/';
break;
}
$this->path = $this->path.''.$template_name;
}

Proper way to access database in PHP classes

My limited PHP knowledge is rather old. When I used to create websites I always had a config.php file which contained some defines and a $_DB global variable. Then in every function_xyz.php file I included this config file.
Now I want to finally move on and use classes. But I can't figure out a proper way to have access to mysql in functions of my classes without inclusion of the so called config.php file on top of each file.
Imagine I have a class called User.php:
class User {
private $firstName;
private $familyName;
private $emailAddress;
public function __construct($username, $password) {
//check if user with name and pass exist in DB
// stuff....
//If user exist, populate member variables
$this->emailAddress = ...
}
public function getEmail(){
return $this->emailAddress;
}
}
I know it is not the best example or practice...but how can I have a global MySQL access in all my classes without being required to have the config file included.
What is the best practice nowadays?
Make a global Instance:
//db.php include once
class DB {
#bind connenction in it
}
#make instance
$db = new DB($config);#use for User Instances
and then:
class User {
private $db;
private $firstName;
private $familyName;
private $emailAddress;
public function __construct($db) {
$this->db=$db;
}
public function validate($username, $password,$db) {
//check if user with name and pass exist in DB
//If user exist, populate member variables
$this->emailAddress = ...
}
public function getEmail(){
return $this->emailAddress;
}
}
$user = new User($db);
Is one way.
But you telling to less about how you want to use the classes.
I would go with PHPAcademy's login/register tutorial. He have DB class that handles almost anything you need. Here is sample of his code, slightly modified by me, but all credits to Alex Garrett.
<?php
class DB {
private static $_instance = null;
private $_pdo,
$_query,
$_error = false,
$_results,
$_count = 0;
private function __construct() {
try {
$this->_pdo = new PDO('mysql:host=' . Config::get('mysql/host') . ';dbname=' . Config::get('mysql/db'), Config::get('mysql/username'), Config::get('mysql/password'));
} catch(PDOException $e) {
die($e->getMessage());
}
}
public static function getInstance() {
if(!isset(self::$_instance)) {
self::$_instance = new DB();
}
return self::$_instance;
}
public function query($sql, $params = array()) {
$this->_error = false;
if($this->_query = $this->_pdo->prepare($sql)) {
$x = 1;
if(count($params)) {
foreach($params as $param) {
if (is_int($param)) {
$this->_query->bindValue($x, $param, PDO::PARAM_INT);
} else {
$this->_query->bindValue($x, $param);
}
$x++;
}
}
if($this->_query->execute()) {
$this->_results = $this->_query->fetchAll(PDO::FETCH_OBJ);
$this->_count = $this->_query->rowCount();
}
else {
$this->_error = true;
print_r($this->_query->errorInfo());
}
}
return $this;
}
public function action($action, $table, $where = array()) {
if(count($where) === 3){
$operators = array('=', '>', '<', '>=', '<=', '!=');
$field = $where[0];
$operator = $where[1];
$value = $where[2];
if(in_array($operator, $operators)) {
$sql = "{$action} FROM {$table} WHERE {$field} {$operator} ?";
if(!$this->query($sql, array($value))->error()) {
return $this;
}
}
} else if (count($where) === 0) {
$sql = "{$action} FROM {$table}";
if(!$this->query($sql)->error()) {
return $this;
}
}
return false;
}
public function get($table, $where) {
return $this->action('SELECT *', $table, $where);
}
public function delete($table, $where) {
return $this->action('DELETE', $table, $where);
}
public function getAll($table) {
return $this->action('SELECT *', $table);
}
public function first() {
return $this->results()[0];
}
public function last() {
$i = count($this->results()) - 1;
return $this->results()[$i];
}
public function insert($table, $fields = array()) {
if(count($fields)) {
$keys = array_keys($fields);
$values = '';
$x = 1;
foreach ($fields as $field) {
$values .= '?';
if($x < count($fields)) {
$values .= ', ';
}
$x++;
}
$sql = "INSERT INTO {$table} (`" . implode('` , `', $keys) . "`) VALUES({$values})";
if (!$this->query($sql, $fields)->error()) {
return true;
}
}
return false;
}
public function update($table, $where, $parametar, $fields) {
$set = '';
$x = 1;
foreach ($fields as $name => $value) {
$set .= "{$name} = ?";
if ($x < count($fields)) {
$set .= ', ';
}
$x++;
}
if (is_int($parametar)) {
$sql = "UPDATE {$table} SET {$set} WHERE {$where} = {$parametar}";
} else {
$sql = "UPDATE {$table} SET {$set} WHERE {$where} = '{$parametar}'";
}
if (!$this->query($sql, $fields)->error()) {
return true;
}
return false;
}
public function results() {
return $this->_results;
}
public function error() {
return $this->_error;
}
public function count() {
return $this->_count;
}
}
Then you can query database like DB::getInstance()->getAll('tableName')->results();. Change DB credentials in __construct, or watch his videos (which I recomend).

Setting PHP variable to class member in a Pass-By-Reference Fails

I am passing a variable by reference to a class method with the intention that it will be set to a member of that same class (based on a selector to the function). Although this doesn't appear to violate the rules of PHP's deprecated call-time pass-by-reference, I must be missing something, because it doesn't work.
I've dumbed down my code to illustrate the issue:
<?php
class Database
{
static $USER_TABLE = 1;
static $INVOICE_TABLE = 2;
static $PRODUCT_TABLE = 3;
var $users;
var $invoices;
var $products;
function __construct()
{
$this->users []= array ('id'=>0, 'first_name'=>'John', 'last_name'=>'Doe');
$this->products []= array('id'=>0, 'user_id'=>'0', 'printer'=>'HP','computer'=>'Toshiba');
$this->invoices []= array('id'=>0, 'user_id'=>'0', 'total_items'=>2, 'total_cost'=>700);
}
function getTable($table_name, &$table)
{
switch ($table_name)
{
case Database::$USER_TABLE: $table = $this->users; break;
case Database::$INVOICE_TABLE: $table = $this->invoices; break;
case Database::$PRODUCT_TABLE: $table = $this->products; break;
}
}
function addEntry($table_name, $info_array)
{
$this->getTable($table_name, $table); // THIS FAILS! WHY?
//$table = &$this->users; // THIS WORKS
if ($table !== null)
{
$id = 0;
if (count($table))
$id = ((int)$table[count($table)-1]['id'])+1;
$entry['id'] = $id;
foreach ($info_array as $k => $v)
{
$entry [$k]= $v;
}
$table []= $entry;
}
}
}
$db = new Database;
$db->addEntry(Database::$USER_TABLE, array('first_name'=>'Jane', 'last_name'=>'Plain'));
var_dump($db);
?>
The alternative is just to take the switch case out of getTable(...) and paste that to the top of all of my functions that would have called it, but that type of code duplication is not desirable.
Any insight is appreciated!
It'd be a lot easier just to have your getTable actually return a table:
function getTable($table_name)
{
switch ($table_name)
{
case Database::$USER_TABLE: return $this->users; break;
case Database::$INVOICE_TABLE: return $this->invoices; break;
case Database::$PRODUCT_TABLE: return $this->products; break;
}
}
And then just call the following:
$table = $this->getTable($table_name);

PDO Binding Sequence/MySQL Incompatibility?

I'm using MySQL(i)-community-5.3 if not mistaken. After finally getting the hang of PDO, I can now conclude that the infected sectore is that which processes binding, all other functions are fine (that I know of). Below I will present a function which works and also doesn't work (the sequence without anything to do with binding works flawlessly.
Function you() is as followes:
public function you($row) {
if(isset($_COOKIE["SK"])) {
$usr = $this->baseXencode($_SESSION["username"]);
$q = "SELECT $row FROM SN_users WHERE username=:USR";
$this->netCore->q($q);
$this->netCore->bind(":USR", $usr);
$result = $this->netCore->single();
$result = $result[$row];
} else {
$q = "SELECT $row FROM SN_users WHERE username='".$this->baseXencode("Anonymous")."' AND password='".$this->baseXencode("Anonymous")."'";
$result = $this->netCore->q($q);
$result = $this->netCore->single();
$result = $result[$row];
}
return $result;
}
}
(As you can see, when pre-allocating the username/password combo for Anonymous users, the function executes perfectly whereas binding within the if() section does not, returning the value 1.
Below is my binding function bind() (if you may require any other code, I will edit this post further~^^):
EDIT:
Below is the netCore class:
class netCore {
private $boot;
private $dbHost = "";
private $dbNAME = "";
private $dbPASS = "";
private $dbUSR = "";
private $err;
private $state;
public function __construct() {
$bootloadr = "mysql:host=".$this->dbHost.";dbname=".$this->dbNAME.";charset=UTF8";
$opt = array(PDO::ATTR_PERSISTENT => false, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION);
try {
$this->boot = new PDO($bootloadr, $this->dbUSR, $this->dbPASS, $opt);
} catch(PDOException $e) {
$this->err = "<b>Lebensborn® netCore™ Error:</b> An exception has been raised during Network-to-Database procedures.<br /><b>Message:</b> ".$e->getMessage().".";
}
}
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->state->bindParam($param, $value, $type);
}
public function debug() {
return $this->state->debugDumpParams();
}
public function exe() {
return $this->state->execute();
}
public function count() {
$this->exe();
return $this->state->fetchColumn();
}
public function q($q) {
$this->state = $this->boot->prepare($q);
}
public function set() {
$this->exe();
return $this->state->fetchAll(PDO::FETCH_ASSOC);
}
public function single() {
$this->exe();
return $this->state->fetch(PDO::FETCH_ASSOC);
}
public function transBegin() {
return $this->boot->beginTransaction();
}
public function transCancel() {
return $this->boot->rollBack();
}
public function transEnd() {
return $this->boot->commit();
}
}

How can i correctly assign a value to a private static variable in PHP class?

I have a class that translate language array base. So the problem is that the language does not change base on a cookie value.
this function should set the language value but it does not. it seems that no matter what i do i always get "ar" as a self::$currlang value. how can i correct this issue?
public function _set(){
if( $_COOKIE['defaultLang'] != '' ) {
self::$currlang = $_COOKIE['defaultLang'];
} else {
//this is the default language
self::$currlang = 'ar';
}
}
here is my code
thanks for your help :)
<?php
include('../langs/english.php');
include('../langs/arabic.php');
class Translator{
private static $strs = array();
private static $currlang = "";
public function _set(){
if( $_COOKIE['defaultLang'] != '' ) {
self::$currlang = $_COOKIE['defaultLang'];
} else {
//this is the default language
self::$currlang = 'ar';
}
}
public static function loadTranslation($lang, $strs){
if (empty(self::$strs[$lang]))
self::$strs[$lang] = array();
self::$strs[$lang] = array_merge(self::$strs[$lang], $strs);
}
public static function setDefaultLang($lang){
self::$currlang = $lang;
}
public static function getDefaultLang(){
return self::$currlang;
}
public static function translate($key, $lang=""){
if ($lang == ""){
$lang = self::$currlang;
}
$str = self::$strs[$lang][$key];
if (empty($str)){
//$str = "$lang.$key";
$str = 'Language "'. $lang . '", '. $key . ' is not defined.';
}
return $str;
}
public static function freeUnused(){
foreach(self::$strs as $lang => $data){
if ($lang != self::$currlang){
$lstr = self::$strs[$lang]['langname'];
self::$strs[$lang] = array();
self::$strs[$lang]['langname'] = $lstr;
}
}
}
public static function getLangList(){
$list = array();
foreach(self::$strs as $lang => $data){
$h['name'] = $lang;
$h['desc'] = self::$strs[$lang]['langname'];
$h['current'] = $lang == self::$currlang;
$list[] = $h;
}
return $list;
}
public static function &getAllStrings($lang){
return self::$strs[$lang];
}
}
?>
The _set() magic method works with the -> operator, which works with instantiated objects. You can't use static member variables with instantiated objects, it's one or the other.

Categories