Solved

PDO PHP Trying to create a Class

Posted on 2014-02-13
9
462 Views
Last Modified: 2014-02-22
I'm trying to create an OOP file(s) and I;m not sure what I'm doing wrong:

The first file is to grab a total from a DB table:
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
$pdo = new PDO("mysql:host=...;dbname=...", "l...", "...");
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

class moneyMade	{
		function totalAmounts($pdo){
			$total_amounts = 	"SELECT SUM(amount) AS amount
								FROM amounts";
			$pdosb = $pdo->prepare($total_amounts, array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL));
			try {
//  GET RESULTS
				while($row = $pdosb->fetch()):
//  RETURN THE RESULTS FROM THE FUNCTION
				$amount = $row['SUM(amount)'];
				return $amount;
				endwhile;	
			}
			catch(PDOException $e) {
   			echo 'ERROR: ' . $e->getMessage();
			}
			
		}//  END TOTAL AMOUNTS

}  //  END CLASS
?>

Open in new window

Then the other file where I want to run it:
<?php
include('classes/functions.php');
?>
<!--  START AMOUNTS MADE  -->
			<fieldset>
				<b>AMOUNTS::</b><br />
				<?php
					$amount = new moneyMade();
					$amount ->totalAmounts($pdo);
				?>
				
				<form>
					<table>
						<tr>
							<td>TOTAL</td>
						</tr>
						<tr>
							<td><?php $amount->amount; ?></td>
						</tr>
						
					</table>
				</form>
			</fieldset>
<!--  END AMOUNTS MADE  -->

Open in new window

I get the ERROR:
 Undefined property: moneyMade::$amount  in the above file.
0
Comment
Question by:rgranlund
9 Comments
 
LVL 42

Expert Comment

by:Chris Stanyon
ID: 39857837
You are trying to access a property of your class called amount:

<?php $amount->amount; ?>

It doesn't exist. Your class has one method - totalAmount() which returns the amount, so you will need to assign the return value of that method call to a variable and use that (or just output it directly).

One of these 2 blocks will work:

<?php
$amount = new moneyMade();
$myAmount = $amount->totalAmounts($pdo);
?>

<?php echo $myAmount; ?>

Open in new window

or

<?php
$amount = new moneyMade();
echo $amount->totalAmounts($pdo)
?>

Open in new window

Also, if you query only returns 1 record, you don't need a loop. You've also aliased the SUM in your query as 'amount' so that's what the column is called

$row = $pdosb->fetch()
return $row['amount'];

Open in new window

0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39858499
It looks like the Class MoneyMade script prepares the query but does not actually run the query.

To help with things like this, add error_reporting(E_ALL) to the top of all of your PHP scripts.  Then when you accidentally rely on an undefined variable you have a better chance of catching the issue before it wastes your time. Without the raised level of error reporting, PHP will issue a Notice but will suppress the Notice and you'll never be told what was wrong!

You will also want to make the $amount variable into a class property.  Properties are assigned with the $this-> keyword.  Run this little script to see what is visible in the Thing class.  The $abc contains local data, usable only inside the class constructor.  The $this->abc is a class property, accessible outside the $x object, and therefore visible to var_dump().

<?php

Class Thing
{
    public function __construct()
    {
        $abc = 'ABC';
        $this->abc = '123';
    }
}

$x = new Thing;
var_dump($x);

Open in new window

Forgetting to use $this-> has got to rank as the most frequently made coding error in PHP object oriented programming!
0
 
LVL 9

Expert Comment

by:rinfo
ID: 39858636

$total_amounts =       "SELECT SUM(amount) AS amount       FROM amounts";

In the above context is this correct.

$amount = $row['SUM(amount)'];


i think this should be $amount = $row['amount'];
I would have preferred
$total_amount_qry =       "SELECT SUM(amount) AS totamts       FROM amounts";
$sum_amount = $row['totamts'];

I suggest following codes
<?php
	
	error_reporting(E_ALL);
	ini_set('display_errors', '1');

					
	$pdo = new PDO("mysql:host=...;dbname=...", "l...", "...");
	$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);


	class moneyMade	{

		function totalAmounts($pdo)
		{
			$sum_amounts_qry = 	"SELECT SUM(amount) AS totamts FROM amounts";
			$sum_amounts = 0;
			$pdosb = $pdo->prepare($total_amounts, array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL));
			try {
					//  GET RESULTS
				    $pdosb->execute();
					while($row = $pdosb->fetch()):
					//  RETURN THE RESULTS FROM THE FUNCTION
					$sum_amount = $row['totamts'];
					return $sum_amount;
					endwhile;	
			}
			catch(PDOException $e) {
   			echo 'ERROR: ' . $e->getMessage();
			}
			
		}//  END TOTAL AMOUNTS

}  //  END CLASS
?>

<?php	
	include('classes/functions.php');
?>
<!--  START AMOUNTS MADE  -->
			<fieldset>
				<b>AMOUNTS::</b><br />
				<?php
					$amount = new moneyMade();
					$amount ->totalAmounts($pdo);
				?>
				
				<form>
					<table>
						<tr>
							<td>TOTAL</td>
						</tr>
						<tr>
							<td><?php $amount->sum_amounts; ?></td>
						</tr>
						
					</table>
				</form>
			</fieldset>
<!--  END AMOUNTS MADE  -->

Open in new window

0
 
LVL 42

Expert Comment

by:Chris Stanyon
ID: 39858662
@Ray - can't believe I missed the non-execution!!

@rinfo - I've already covered that

@rgranlund - as Ray said, you also need to make sure you execute your query:

function totalAmounts($pdo){
   $total_amounts = "SELECT SUM(amount) AS amount FROM amounts";
   $pdosb = $pdo->prepare($total_amounts, array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL));
   try {
      $pdosb->execute();
      $row = $pdosb->fetch();
      return $row['amount'];
   } catch(PDOException $e) {
      echo 'ERROR: ' . $e->getMessage();
   }
}

Open in new window

0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 9

Expert Comment

by:rinfo
ID: 39858673
@Chris Stanyon my emphasis was on wrong use of column name .
And what i submitted was a more appropriate way to achieve what author is trying to achieve.
0
 
LVL 42

Expert Comment

by:Chris Stanyon
ID: 39858687
@rinfo - read the last part of my opening comment!
0
 
LVL 9

Expert Comment

by:rinfo
ID: 39858807
@Chris Stanyon i stand corrected and  perhaps deleted.
0
 
LVL 42

Expert Comment

by:Chris Stanyon
ID: 39858816
No worries - at least we're all on the same page :)
0
 
LVL 33

Accepted Solution

by:
Slick812 earned 500 total points
ID: 39860248
it looks like your CLASS could be written better as it should contain the OBJECT for the PDO operator, below is my version -
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);

class moneyMade	{
  public $cPDO = false; // CLASS property for PDO object 
  
// Make your CLASS produce the PDO object when it starts in __construct()
  public function __construct() {
  $this->cPDO = new PDO("mysql:host=...;dbname=...", "l...", "...");
  $this->cPDO->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  }
  
  public function totalAmounts(){
  $sql = 	"SELECT SUM(amount) AS amount FROM amounts";

// If you use a simple PDO Query you have less code
  try {
    $statem = $this->cPDO->query($sql);
    $row = $statem->fetch(PDO::FETCH_ASSOC);
    }  //  END TRY
    catch(PDOException $e) {
    echo 'ERROR: ' . $e->getMessage();
    }

  return $row['amount']; // RETURN the number of SUM additions
  }//  END TOTAL AMOUNTS

}  //  END CLASS

?>

<!--  START AMOUNTS MADE  -->
  <fieldset><b>AMOUNTS::</b><br />
<?php
// It helps me if I name the variable same as CLASS name
$moneyMade = new moneyMade();

// It helps me if I name the variable the same as what the variable has in it
$total = $moneyMade->totalAmounts();
?>
  <form><table>
    <tr><td>TOTAL</td></tr>
    <tr><td><?php echo $total; ?></td>
    </tr>
  </table></form>
  </fieldset>
<!--  END AMOUNTS MADE  -->

Open in new window

, I tried this on my server and it works there, please ask questions if you need any information about this.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

I annotated my article on ransomware somewhat extensively, but I keep adding new references and wanted to put a link to the reference library.  Despite all the reference tools I have on hand, it was not easy to find a way to do this easily. I finall…
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now