Jump to content

Solar

Members
  • Posts

    145
  • Joined

  • Last visited

Profile Information

  • Gender
    Not Telling

Solar's Achievements

Member

Member (2/5)

0

Reputation

  1. Simply perfect! To know that this is fine to create a new object and then call the get function to retrieve the pdo relieves a lot of stress! Thanks a billion for this quick reply!
  2. Hey, I'm stumped. Through out this post, I will bold my questions. Is global $pdo; frowned upon? is it safe? is there a better way to access it? As of right now, I'm using the easy way of creating a database php file and including it in specific php pages to access the $pdo variable but I would like a different approach. I would like to use a database class. Let's say I have my class/class_database.php -- converted from a non-class. <?php class Database{ private $pdo; function __construct(){ try{ $pdo = new PDO('mysql:host=HOST;dbname=DBNAME', "USER", "PASSWORD"); $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); }catch(PDOException $e){ echo 'Connection failed: '.$e->getMessage(); } } } ?> I would like to create a separate class called Statements: class Statements{ function select($sql,$arr){ $stmt = $this->pdo->prepare($sql); if(empty($arr) == false){ foreach($arr as $k => $v){ $stmt->bindValue($k, $v); } } $stmt->execute(); return $stmt->fetchAll(PDO::FETCH_ASSOC); } } The problem I'm having is that I would have to use extends in order to access the private pdo: class Statements extends Database Is extending a Database class safe or recommended? If not, how can I use a construct inside of Statements to obtain the Database Class PDO variable? Because every time Statements is called, it will need to use the pdo from the Database Class. class Statements{ private $pdo; function __construct(Database $newPDO){ $this->pdo = $newPDO; } //functions below... } How exactly do I pass the pdo to the __construct function and make the call? I would easily assume that creating a get function inside of the Database Class to get the private $pdo variable is risky business. After doing a bunch of research online, it seems like programmers fight over global pdo variables and extends of the Database because it logically doesn't make sense. Unfortunately, I have yet to find a reasonable answer. I appreciate your time!
  3. As if it's that simple! Thanks a ton! Edit: I guess either way, I still have to specify the variables. I'm good with this! $account = new Account(); $account->getUserInfo($email,$rank); class Account{ function getUserInfo(&$email,&$rank){ global $con; $stmt = $con->prepare('SELECT email, rank FROM accounts WHERE id = ?'); $stmt->bind_param('i', $_SESSION['id']); $stmt->execute(); $stmt->bind_result($email, $rank); $stmt->fetch(); $stmt->close(); } }
  4. Let's say I have a simple statement. $stmt = $con->prepare('SELECT email,rank FROM accounts WHERE id = ?'); $stmt->bind_param('i', $_SESSION['id']); $stmt->execute(); $stmt->bind_result($email,$rank); $stmt->fetch(); $stmt->close(); I can then echo the e-mail and rank variables very easily in PHP <?=$email?> <?=$rank?> What is the best way to echo the results with a bind_result while it's inside of a function? function getUserInfo(){ global $con; $stmt = $con->prepare('SELECT email,rank FROM accounts WHERE id = ?'); $stmt->bind_param('i', $_SESSION['id']); $stmt->execute(); $stmt->bind_result($email,$rank); $stmt->fetch(); $stmt->close(); } I could easily stick email and rank into an array and return it but that's duplicating code. Is there a way to fetch the bind_result and return all variables inside of it? I greatly appreciate it 🙂
  5. Why are you re-initializing the following $params array? public function query($sql, $params = array()) { Should be just this, no? public function query($sql, $params) {
  6. If you are running apache to you need to edit the ini's in the folder. Then restart your server as well. apache/bin/php.ini
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.