Page 1 of 1

Table dinamic with calculation

Posted: Thu Mar 20, 2014 10:55 am
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

Re: Table dinamic with calculation

Posted: Thu Mar 20, 2014 11:07 am
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?

Re: Table dinamic with calculation

Posted: Thu Mar 20, 2014 11:36 am
by Fabiooo7
really is returning with an error but do not know what this error is

Re: Table dinamic with calculation

Posted: Thu Mar 20, 2014 11:41 am
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());
}

Re: Table dinamic with calculation

Posted: Thu Mar 20, 2014 11:47 am
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 )

Re: Table dinamic with calculation

Posted: Thu Mar 20, 2014 12:38 pm
by Fabiooo7
Dear Friend Celauran
Gotta figure out the error?

Re: Table dinamic with calculation

Posted: Fri Mar 21, 2014 9:25 am
by Fabiooo7
anyone know how I fix this?
thank you

Re: Table dinamic with calculation

Posted: Wed Mar 26, 2014 9:41 am
by Fabiooo7
anyone know how I fix this?
thank you