Table dinamic with calculation

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Fabiooo7
Forum Newbie
Posts: 10
Joined: Sun Feb 16, 2014 3:23 pm

Table dinamic with calculation

Post by Fabiooo7 »

Hello Friends
Could someone help me?

This is My mysql database name: lc_movimento
| Id | data | descricao | valor

I'm using this code below is giving the error. someone could tell me what I'm missing

Code: Select all


<?php
//include"config.php";

$pdo = new PDO("mysql:host=localhost;dbname=mrangelc_teste", "mrangelc_01", "xxxxxx" );


//$pdo = new PDO('pgsql:<conn-string>');


$result = $pdo->query("select 
  (DATE_TRUNC('month',CURRENT_DATE) + CAST(FLOOR(s.idx / 5) || ' day' as interval))::DATE AS data, 
  'descricao' as descricao,
  CASE WHEN random() < 0.5 THEN ROUND(150 + (random()*100)::numeric,2) ELSE ROUND(150 + (random()*100)::numeric,2) * -1 END as valor
from lc_movimento(1,24) as s(idx)");

$ultimaData = null;
$acumulado = 0;
$totalEntradaDia = 0;
$totalSaidaDia = 0;

echo '<table>';

while($row = $result->fetch(PDO::FETCH_ASSOC)) {
	$date = DateTime::createFromFormat('Y-m-d', $row['data']);
	$key = $date->format('Ymd');

	if($ultimaData !== null && $ultimaData != $key) {
		printf('<tr><td colspan="2">Entrada: %s, Saida: %s, Saldo dia: %s, Saldo acumulado: %s</td></tr>',
			number_format($totalEntradaDia,2,',','.'),
			number_format($totalSaidaDia,2,',','.'),
			number_format($totalEntradaDia + $totalSaidaDia,2,',','.'),
			number_format($acumulado,2,',','.')
		);

		$totalSaidaDia = 0;
		$totalEntradaDia = 0;
	}

	$ultimaData = $key;

	printf('<tr><td>%s</td><td>%s</td><td>R$ %s</td></tr>',
		$date->format('d'),
		$row['descricao'],
		number_format($row['valor'],2,',','.')
	);

	if($row['valor'] > 0) {
		$totalEntradaDia += $row['valor'];
	} else {
		$totalSaidaDia += $row['valor'];
	}

	$acumulado += $row['valor'];
}

echo '</table>';

printf('<tr><td colspan="2">Entrada: %s, Saida: %s, Saldo dia: %s, Saldo acumulado: %s</td></tr>',
	number_format($totalEntradaDia,2,',','.'),
	number_format($totalSaidaDia,2,',','.'),
	number_format($totalEntradaDia + $totalSaidaDia,2,',','.'),
	number_format($acumulado,2,',','.')
);
Fatal error: Call to a member function fetch() on a non-object in /home/mrangelc/scripts/fluxocaixa/teste.php on line 23

I thank all
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Table dinamic with calculation

Post by Celauran »

Best guess is that $result is false, meaning your query is broken. Have you tried running the query manually? Have you checked for errors?
Fabiooo7
Forum Newbie
Posts: 10
Joined: Sun Feb 16, 2014 3:23 pm

Re: Table dinamic with calculation

Post by Fabiooo7 »

really is returning with an error but do not know what this error is
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Table dinamic with calculation

Post by Celauran »

Before you try calling fetch(), check that $result isn't false. If it is, find out why.

Code: Select all

if ($result !== false) {
	while ($row = $result->fetch()) {
		// etc
	}
} else {
	// Don't actually do this in production
	print_r($pdo->errorInfo());
}
Fabiooo7
Forum Newbie
Posts: 10
Joined: Sun Feb 16, 2014 3:23 pm

Re: Table dinamic with calculation

Post by Fabiooo7 »

returning
Array ( [0] => 42000 [1] => 1064 [2] => You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'interval))::DATE AS data, 'descricao' as descricao, CASE WHEN random() < ' at line 2 )
Fabiooo7
Forum Newbie
Posts: 10
Joined: Sun Feb 16, 2014 3:23 pm

Re: Table dinamic with calculation

Post by Fabiooo7 »

Dear Friend Celauran
Gotta figure out the error?
Fabiooo7
Forum Newbie
Posts: 10
Joined: Sun Feb 16, 2014 3:23 pm

Re: Table dinamic with calculation

Post by Fabiooo7 »

anyone know how I fix this?
thank you
Fabiooo7
Forum Newbie
Posts: 10
Joined: Sun Feb 16, 2014 3:23 pm

Re: Table dinamic with calculation

Post by Fabiooo7 »

anyone know how I fix this?
thank you
Post Reply