Jump to content

So I still do not understand classes very well...


Lamez

Recommended Posts

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.