Solved

PDO PHP Trying to create a Class

Posted on 2014-02-13
9
474 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 43

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 110

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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 43

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
 
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 43

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 43

Expert Comment

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

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

622 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