1

This is what I'm trying to achieve. If All is selected from the drop down menu then I select all clients from my database. For each client it will then run the script creating the html page and then the pdf. This is where I have got to but I cant get it to generate a html page and pdf for each client.

<?php
$client_id=$_POST["client_id"];
$date_start=$_POST["date_start"];
$date_end=$_POST["date_end"];

if ($client_id == 'ALL') 
{
  $con = mysql_connect("localhost","user","password");
  if (!$con)
  {
    die('Could not connect: ' . mysql_error());
  }

  mysql_select_db("mydatabase", $con);  

  $query = "select client_id from ca_client_account";

  $result = mysql_query($query) or die(mysql_error());

  while($row = mysql_fetch_array($result))
  {
    $command="php $result.php $result $date_start $date_end > $result.html";
    exec($command, $output, $status);
    echo $command;
    if ($status!=0) {print_r($output); die("wget failed with status $status"); }

    $command="wkhtmltopdf-i386 --margin-left 5mm --margin-right 5mm $result.html $result.pdf";
    exec($command, $output, $status);
    if ($status!=0) die("htmltopdf failed");
  }

}
else
{
  $command="php $client.php $client_id $date_start $date_end > $client.html";
  exec($command, $output, $status);
  if ($status!=0) {print_r($output); die("wget failed with status $status"); }

  $command="wkhtmltopdf-i386 --margin-left 5mm --margin-right 5mm $client.html $client.pdf";
  exec($command, $output, $status);
  if ($status!=0) die("htmltopdf failed");
}
?>

For a single client everything is perfect. When I try and generate all client statements I cant get it to work.

What am i doing wrong?

Many thanks

3
  • What output do you get. Any errors? Commented Feb 23, 2012 at 2:38
  • the $result variable does not output each client_id as intended. It outputs resource id 3. Commented Feb 23, 2012 at 2:40
  • curious, why do you exec a php file instead of just includeing the file and running the functions like any other? Seems like less hassle to me. Commented Feb 23, 2012 at 2:48

2 Answers 2

2

$result will not deliver what you expect here. You need to specify the exact field you intend to echo out. In this case $row['client_id'] would work.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you, its almost there but I get Undefined offset:1 now. I used $row[1] instead of $row['client_id'] - im sure this isnt the cause tho
@TheManiac you are right. I did not mean to change them they are a static file myfile.php - I changed them without thinking when copying in here to make the post generic
Great -- one more thing: if you want to use $row['client_id'] instead of $row[1] (the former being much more descriptive and a better practice in general) replace your call to mysql_fetch_array with mysql_fetch_assoc
2

You have a fundamental misunderstanding of how database calls work. In your $command, you use $result, which is a database requery RESULT HANDLE. It is not a value from the query, it not useable by anything OTHER than mysql_*() functions. A standard query sequence looks something like this:

$sql = "SELECT ...";
$result = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_array($result);

echo $row['some_field'];

$result contains a statement handle representing your query results. You use this statement handle to FETCH a row of data. Within that $row of data you've fetched, will be the individual data fields you specified in your query. It is THOSE bits of data which you would pass on to your external script.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.