Jump to content

[SOLVED] Cannot redeclare mysql_fetch_array()


nick02895

Recommended Posts

I've been trying to re-write some code I found at http://bytes.com/forum/thread519.html that creates pdf pages that I can print labels with.

Issue is it wasn't written for mysql and php (now that I've been into it for several hours).

I think I'm quite close, could use some help here.

2 files

labels.php and print.php

error I'm getting is

Cannot redeclare mysql_fetch_array() on line 29

Which is the end of function 1

 

----------------------------------print.php-----------
<?php
require_once('labels.php');
require('pdf/fpdf.php');
$cnx=db_connect();
$SelectStmt="select * from user";
PrintAddressLabels($SelectStmt);
?>
------------------------------end print.php------

--------------------------------------------------------------labels.php-------
<?php
//------------------connect mysql database-------------------
function db_connect()
{
   $result = mysql_connect("localhost", "username", "password");
   if (!$result)
      return false;
   if (!mysql_select_db("db"))
      return false;

   return $result;
}
//------------------------------------function  1--------------------
function mysql_fetch_array($res)
{
$row = array();
$result = array();
if ($result = mysql_fetch_row($res))
{
$nf = mysql_num_fields($res)+1;
for($count=1; $count < $nf; $count++)
{
$field_name = mysql_field_name($res, $count);
$field_value = mysql_result($res, $count);
$row[$field_name] = $field_value;
}
return $row;
}
}
//----------------------------function 2---------------
function Avery5160($x, // X co-ord of label (0-2)
$y, // Y co-ord of label (0-9)
&$pdf,
$Data) // String w/ line breaks to print
{
$LeftMargin = 4.2;
$TopMargin = 12.7;
$LabelWidth = 66.6;
$LabelHeight = 25.45;
// Create Co-Ords of Upper left of the Label
$AbsX = $LeftMargin + (($LabelWidth + 4.22) * $x);
$AbsY = $TopMargin + ($LabelHeight * $y);
// Fudge the Start 3mm inside the label to avoid alignment errors
$pdf->SetXY($AbsX+3,$AbsY+3);
$pdf->MultiCell($LabelWidth-8,4.5,$Data);
return;
}
//------------------------------------------function 3---------------
function PrintAddressLabels($SelectStmt)
{
global $cnx; // database conneciton
$pdf=new FPDF();
$pdf->Open();
$pdf->AddPage();
$pdf->SetFont('Arial','B',10);
$pdf->SetMargins(0,0);
$pdf->SetAutoPageBreak(false);
$cur = mysql_query($SelectStmt);
if (!$cur) {
echo "Database Error";
return;
}
$x = 0;
$y = 0;
while ($row = mysql_fetch_array($cur) )
{
$LabelText = sprintf("%s\n%s\n%s, %s, %s",
$row['firstname'],
$row['lastname'],
$row['address'],$row['city'],$row['zip']);
Avery5160($x,$y,$pdf,$LabelText);
$y++; // next row
if ($y == 10 ) { // end of page wrap to next column
$x++;
$y = 0;
if ($x == 3 ) { // end of page
$x = 0;
$y = 0;
$pdf->AddPage();
}
}
} else {
// Error quit printing
break;
}
}
$pdf->Output();
}
?>
-----------------------------------------end labels.php--------------------

Any and all comments welcome.

Nick

 

Remove the following

function mysql_fetch_array($res)
{
$row = array();
$result = array();
if ($result = mysql_fetch_row($res))
{
$nf = mysql_num_fields($res)+1;
for($count=1; $count < $nf; $count++)
{
$field_name = mysql_field_name($res, $count);
$field_value = mysql_result($res, $count);
$row[$field_name] = $field_value;
}

and your code should work, all the above code does is simulate the actual built in mysql_fetch_array function.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.