0

** Here is the code,and how to retrieve mysql data, pass through the function and print it. i try this but show an error in function part like GenerateName(), GeneratePrice(), GenerateCode() section. How to solve that, Help me to solve that problem **

<?php
require('mc_table.php');
$pdf=new PDF_MC_Table();
$pdf->AddPage();
$pdf->SetFont('Arial','',14);
$pdf->Cell(188,12,'Title',1,1,'C');
$pdf->Cell(47,12,'',0,1);
$pdf->SetWidths(array(47,47,47));
$pdf->Cell(47,12,'Name',1,0);
$pdf->Cell(47,12,'Price',1,0);
$pdf->Cell(47,12,'Code',1,1);
include('db.php');
$sql="Select Name,Price,Code from products";
$result=mysql_query($sql);
while($dtset=mysql_fetch_array($result))
{
$name=$dtset['Name'];
$price=$dtset['Price'];
$code=$dtset['Code'];
function GenerateName($name)
{
$nb=1;
$w='';
for($i=1;$i<=$nb;$i++)
$w.='$name';
return $w;
}
function GeneratePrice($price)
{
$nb=1;
$p='';
for($i=1;$i<=$nb;$i++)
$p.='$price';
return $p;
}
function GenerateCode($code)
{
$nb=1;
$c='';
for($i=1;$i<=$nb;$i++)
$c.='$code';
return $c;
}
for($i=0;$i<1;$i++)
$pdf->Row(array(GenerateName(),GeneratePrice(),GenerateCode()));
}
$pdf->Output();
?>
Jack
  • 1
  • 5
  • As mysql_* was deprecated in PHP 5.5 (please refer to [PHP doc](http://php.net/manual/en/function.mysql-connect.php)) you should **really** consider using [PPS : Prepared Parameterized Statements](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php). This will help [Preventing SQL injection](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php). And please use `error_reporting(E_ALL); ini_set('display_errors', 1);` on top of your pages and let us know which error are thrown – OldPadawan Apr 15 '17 at 06:19
  • Fatal error: Cannot redeclare GenerateName() (previously declared in C:\wamp\www\test2\index.php:24) in C:\wamp\www\test2\index.php on line 21 – Jack Apr 15 '17 at 06:21
  • error speaks for itself... then `require_once` [PHP doc](http://php.net/manual/en/function.require-once.php) might be useful – OldPadawan Apr 15 '17 at 06:23
  • give me solution – Jack Apr 15 '17 at 06:26
  • I gave you all you need mate :) the solution is making sure you don't try to include/declare twice ! – OldPadawan Apr 15 '17 at 06:27
  • now check ,but still error on this code – Jack Apr 15 '17 at 06:31
  • check where you declare `GenerateName()` first, in which file ? `mc_table.php` ? `db.php` ? – OldPadawan Apr 15 '17 at 06:36

1 Answers1

1

You have declared the functions inside the while loop. On the first iteration they get declared, but in the next iteration you try to declare them again. You can't do that, that's why you get the error message:

Fatal error: Cannot redeclare GenerateName() (previously declared in C:\wamp\www\test2\index.php:24) in C:\wamp\www\test2\index.php on line 21

Move the functions outside of the while loop so they get declared only once.

Progman
  • 16,827
  • 6
  • 33
  • 48