tags:

views:

74

answers:

5

Is there something wrong with using the following:

$test .= '$row[\''.$row[0].'\'].';
//When echo'ed shows
echo $test;
//$row['CustomerID'].$row['CompanyName']

//I have used it like this
eval($test)

PHP complains:

Parse error: parse error in C:\w.php(73) : eval()'d code on line 1

I can't see where the parse error is. Please help.

Update + More Detail

function get_rows(){
//This function needs to find out what columns it will query for
//so it calls get_columns()
while($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_NUMERIC)){
 $pdf->Cell(0,10, eval($column_names) ,0,1);
}

function get_columns(){
//returns a string of the columns that need to
//be retrieved
return $column_names;
}

I have a PDF creator in function get_rows() which needs to be passed a string which is the value of the evaluated code I have shown above.

I have condensed what is happening as there is too much code.

+4  A: 

You can use variables for the keys as well like this:

echo $row[$row[0]];

This will get you the value with the key that is in $row[0].


Edit Now that you added more information: I would just store the column names in an array and do a foreach loop to retrieve the corresponding values from $row like this:

while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_NUMERIC)) {
    $value = '';
    foreach ($column_names as $column_name) {
        $value .= $row[$column_name];
    }
    $pdf->Cell(0, 10, $value, 0, 1);
}
Gumbo
The thing is, the string $test is generated from another function which is used to help another function prepare the displaying of data from my database. I don't think I can use the above in my situation. Or have I misunderstood how to use the above?
Abs
Can you post more code? This can be 100% solved without eval().
Pekka
Depending on the "real circumstances" it may even be feasible (if not better) to create the statement according to some get\_columns() function/method and thus only fetch the data that is required.
VolkerK
Thanks Gumbo - I should of thought of this myself! Apologies for not supplying enough info.
Abs
A: 

Gumbo is totally right, there is absolutely no need to use eval() for this.

For the purposes of finding the actual parse error, there seems to be no semicolon behind the statement?

Pekka
That solves the eval problem. Thanks! I am guessing using eval in this situation is bad practice, if so, what should I be doing. I don't understand Gumbo's answer, its too high level! Maybe a link will help?
Abs
+1  A: 

Your code always appends a . even if it is not needed. Your results should be impossible based on the code you have provided. Given your code I would expect to see this:

echo $test;
//$row['CustomerID'].$row['CompanyName'].

Note the extra dot.

Also, as Pekka pointed out, you are missing the semi-colon.

Mark Byers
Please give me the benefit of the doubt, I have simplified the code. :)
Abs
Well this certainly would explain exactly the error message you quote. And there is little benefit-of-the-doubt available when you're using evil `eval`.
bobince
A: 

Quoting the manual:

eval() returns NULL unless return is called in the evaluated code"

You don't even show what $column_names contains or how it's generated so there isn't enough info to say what's wrong.

I have a PDF creator in function get_rows() which needs to be passed a string which is the value of the evaluated code I have shown above.

Why exactly do you need "eval" to generate a string?

Álvaro G. Vicario
+1  A: 

I certainly agree that you don't need eval() here, but to give you an example of what I mean by "tested, self-contained example":

<?php
function insert_rows($pdf) {
  $column_names = get_evalcode();
  echo 'Debug: ', __METHOD__, ': column_names=\'', $column_names, "'\n";
  // while($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_NUMERIC)){
    $row = array('a'=>'fA', 'b'=>'fB', 'c'=>'fC');
    $pdf->Cell(0,10, eval($column_names) ,0,1);
  //}
}

function get_evalcode(){
  $column_names = 'return $row["a"].$row["b"];';
  return $column_names;
}

class DummyPDF {
  public function Cell($p1, $p2, $string, $p4, $p5) {
    echo 'Debug: ', __METHOD__, ': ', $string, "\n";
  }
}
insert_rows(new DummyPDF);

prints

Debug: insert_rows: column_names='return $row["a"].$row["b"];'
Debug: DummyPDF::Cell: fAfB

Now you can remove the dummy implementations step-by-step and compare the differences.

VolkerK