Lamez Posted August 24, 2010 Share Posted August 24, 2010 So I am trying to run my execute function from my database class from my email class. I have SMTP system to handle emails on the website. I am working on a function in the email class called addAccount. It is suppose to add a row in the database under the SMTP table. When I run the function, I get no parsing errors, so I add the or die to my query from the execute function, still nothing at all. So here is some code: -The addAccount function from Email.php: function addAccount($name, $email, $username, $password, $protocol, $port, $server){ //Error checking & cleaning vars. will be done in the application, not the backend. if(!empty($name) && !empty($email) && !empty($username) && !empty($password) && !empty($proctocol) && !empty($port) && !empty($server)){ $name = ucwords(strtolower($name)); $email = strtolower($email); $this->db->execute("INSERT INTO ".TBL_SMTP." (name, email, username, password, protocol, port, server) VALUES ('$name', $email', '$username', '$password' '$protocol', '$port', '$server')", true); return true; }else return false; } -The entire email class from Email.php <?php require_once("pear/Mail.php"); class Email{ var $from_name, $from_email, $to_name, $to_email, $subject, $body, $host, $port, $username, $password; private $db = NULL; function __construct(Database $db){ $this->db = $db; } function dbEmail($id, $name, $email, $subject, $body){ $q = $this->db->select(TBL_SMTP, "*", "id='".$id."'"); if($q->numRows() > 0){ $f = $q->fetchRow(); $this->from_name = $f['name']; $this->from_email = $f['email']; $this->username = $f['username']; $this->password = $f['password']; $this->host = $f['protocol']."://".$f['server']; $this->port = $f['port']; $this->to_name = $name; $this->to_email = $email; $this->subject = $subject; $this->body = $body; return sendEmail(); }else return false; } function sendEmail(){ $from = $this->from_name." <".$this->from_email.">"; $to = $this->to_name." <".$this->to_email.">"; $headers = array ('From' => $this->from, 'To' => $this->to, 'Subject' => $this->subject); $smtp = Mail::factory('smtp', array ( 'host' => $this->host, 'port' => $this->port, 'auth' => true, 'username' => $this->username, 'password' => $this->password)); $mail = $smtp->send($to, $headers, $this->body); if(PEAR::isError($mail)){ //echo($mail->getMessage()); //For debugging purposes only return false; }else return true; } function addAccount($name, $email, $username, $password, $protocol, $port, $server){ //Error checking & cleaning vars. will be done in the application, not the backend. if(!empty($name) && !empty($email) && !empty($username) && !empty($password) && !empty($proctocol) && !empty($port) && !empty($server)){ $name = ucwords(strtolower($name)); $email = strtolower($email); $this->db->execute("INSERT INTO ".TBL_SMTP." (name, email, username, password, protocol, port, server) VALUES ('$name', $email', '$username', '$password' '$protocol', '$port', '$server')", true); return true; }else return false; } } $email = new Email($db); ?> The entire database class from Database.php <?php class Database{ var $mysqli, $result, $q, $affectedRows; function __construct($host, $user, $pass, $db){ $this->mysqli = new MySQLi($host, $user, $pass, $db); } function execute($query, $error = false, $mode = MYSQLI_STORE_RESULT){ $this->q = $query; if(!$error) $result = $this->mysqli->query($query, $mode); else $result = $this->mysqli->query($query, $mode) or die($this->mysqli->error); if(is_object($result) && $result instanceof MySQLi_Result){//if result is a object and is part of the mysqli class? $this->result = $result; $this->affectedRows = $this->result->num_rows; }else $this->affectedRows = $this->mysqli->affected_rows; return $this; } function fetchRow($mode = MYSQLI_ASSOC){ return $this->result->fetch_assoc($mode); } function fetchAll($mode = MYSQLI_ASSOC){ $row = $this->result->fetch_all($mode); return !empty($row) ? $row : array();//if not empty return row, else return an array? } function numRows(){ return $this->affectedRows; } function delete($table, $where){ return $this->execute("DELETE FROM ".$table." WHERE ".$where); } function deleteAll($table){ return $this->execute("TRUNCATE ".$table); } function update($table, $set, $where){ return $this->execute("UPDATE ".$table." SET ".$set." WHERE ".$where); } function select($table, $select = "*", $where = NULL){ if(is_null($where)) $where = ""; return $this->execute("SELECT ".$select." FROM ".$table." ".$where); } } $db = new Database(DB_HOST, DB_USER, DB_PASS, DB_DB); ?> Chances are it is my email class since it was 100% written by me. The database class I had some help from a member here. What do you experts think? Quote Link to comment https://forums.phpfreaks.com/topic/211582-so-i-still-do-not-understand-classes-very-well/ Share on other sites More sharing options...
trq Posted August 24, 2010 Share Posted August 24, 2010 Have you got error reporting on? You seem to be missing a { after your else to me. Quote Link to comment https://forums.phpfreaks.com/topic/211582-so-i-still-do-not-understand-classes-very-well/#findComment-1103012 Share on other sites More sharing options...
Lamez Posted August 24, 2010 Author Share Posted August 24, 2010 According to phpinfo, I do. What else are you talking about? Quote Link to comment https://forums.phpfreaks.com/topic/211582-so-i-still-do-not-understand-classes-very-well/#findComment-1103015 Share on other sites More sharing options...
trq Posted August 24, 2010 Share Posted August 24, 2010 You have if statements that are missing braces. Quote Link to comment https://forums.phpfreaks.com/topic/211582-so-i-still-do-not-understand-classes-very-well/#findComment-1103016 Share on other sites More sharing options...
Alex Posted August 24, 2010 Share Posted August 24, 2010 All the else statements that don't have braces that I see should work fine because you're only executing one statement on that condition, so I think your problem is somewhere else. Personally, even if I only need to execute a single statement on a certain condition I opt to leave the braces there. I find that it makes things look a lot neater. Quote Link to comment https://forums.phpfreaks.com/topic/211582-so-i-still-do-not-understand-classes-very-well/#findComment-1103017 Share on other sites More sharing options...
Lamez Posted August 24, 2010 Author Share Posted August 24, 2010 In most free form languages you can have if statements, without braces, that will execute the next statement given that the expression is true. Same goes for the else, executes next statement, given that the conditions are met. Quote Link to comment https://forums.phpfreaks.com/topic/211582-so-i-still-do-not-understand-classes-very-well/#findComment-1103018 Share on other sites More sharing options...
trq Posted August 24, 2010 Share Posted August 24, 2010 Yeah ok, so they work. Terrible way to code though. Consistency is king. if ($a) { echo "true"; } else echo "false"; Terrible. Especially seeing as your using return if the first expression is true, you don't even need an else. Quote Link to comment https://forums.phpfreaks.com/topic/211582-so-i-still-do-not-understand-classes-very-well/#findComment-1103032 Share on other sites More sharing options...
Lamez Posted August 24, 2010 Author Share Posted August 24, 2010 I am being consistent actually, if the 'if' body is only going to be one statement then I leave out the braces, otherwise... Any who, I was wondering if you guys knew why my execute function is not giving me anything in return. It is in the database class. Quote Link to comment https://forums.phpfreaks.com/topic/211582-so-i-still-do-not-understand-classes-very-well/#findComment-1103090 Share on other sites More sharing options...
CroNiX Posted August 24, 2010 Share Posted August 24, 2010 Most languages allow this. Its only if you want more than one statement to be executes as the result of the if/else do you need to include them in braces. That being said, it is poor coding practice, looks ugly, and is harder to follow for lack of consistency. Best practice is to use braces around all if/else/etc conditionals, even singles. Quote Link to comment https://forums.phpfreaks.com/topic/211582-so-i-still-do-not-understand-classes-very-well/#findComment-1103385 Share on other sites More sharing options...
nethnet Posted August 25, 2010 Share Posted August 25, 2010 I'm with thorpe on this one. Leaving out the braces not only looks tacky (luckily this has no application to anything practical), but it also provides for a headache in debugging, as we can see. I had to run though each of those scripts a couple times just to make sure I was counting all of the braces correctly. But alas, let's not get distracted from the problem... It's possible that the lack of any output is an indication of a parse error, even though you say error reporting is turned on. Copy/paste your codes into a program like jEdit if you aren't already just to check for possible parse errors (most of these softwares have it built in). And if it looks good, then run the script with some control values. Supply inputs that would produce a known output, and follow the path of that schematic through your programming logic, placing echos along the way writing simple things such as "Looks like methodA() is returning false after all.." If none of these echo statements are printed back, then you know for sure there is a fatal error. Quote Link to comment https://forums.phpfreaks.com/topic/211582-so-i-still-do-not-understand-classes-very-well/#findComment-1103389 Share on other sites More sharing options...
Lamez Posted August 25, 2010 Author Share Posted August 25, 2010 lol type-o in my addAccount function. Thanks for the help and criticism anyways Quote Link to comment https://forums.phpfreaks.com/topic/211582-so-i-still-do-not-understand-classes-very-well/#findComment-1103675 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.