Hello,
In an attempt to find a workaround for the lack of subquery in MySql, I'm trying use PHP code to limit my result set. I'm trying to use a variable to substitute into a portion of an 'if' statement that I evaluate later in the program. It's not working and I'm not sure how to fix it.

Here is how I'm defining the variable:

$if_statement = '$myrow["Numbers"]>0';

the variable $if_statement will change based on certain criteria. Later in theh program, I then go on to create my 'if' statement as I read through the rows of the table:

if ($if_statement)
{
.....
{

Problem is, the $if_statement variable doesn't seem to translate into a viable statement. I read about the eval() function, which evaluates a string as PHP code, but can't get this to work either. Is this what I need to use to solve my problem? Or, is there a better way to do it?

Thanks!

Mar

    Ok first a question why not just do:

    if ($myrow["Numbers"]>0) {
    }

    as your if statement? I'm assuming that your if is inside some loop and needs to be executed on each row that you've returned.

    Now using eval should work but there are some issues regarding eval that are sometimes hard to work with.

    if (eval($if_statement)) {
    }

    That's how your if statement should look using eval, but you are going to have to check what PHP thinks is inside $if_statement, and maybe add some escapes () to make things work properly.

      Hi Thanks for your response - At least I know I'm on the right track with Eval().

      The value in $if_statment will change based on the value of the Numbers column in the database. Basically, if Numbers is within some range, I want to print that row, else I don't. I can't weed out the rows in the SQL query because it took me a long, complex 'IF' query just to get the value of Numbers (long story), so I've got to weed out the rows in PHP code.

      When I tried to use the statement:

      If (eval($if_statement))

      I don't get any results and when I print $if_statement to see what it evaluates to, I see nothing.

      Mar

        You should check the scope of the $if_statement variable. I'm assuming that you are looping though your results and setting this variable. If you create the variable inside your loop you won't have access to it outside of the loop.

        Also again why can't you just evaluate the expression, why do you have to put it into a variable to be evaluated at a later time?

        Maybe you should post the relevent code sections.

          Well, I came up with a workaround for this problem which involved eliminating the variable solution.

          I think the jist of my problem was that I was trying to do something gourmet when I should have just stuck with meat and potatoes.

          Thanks for all the help! I did learn quite a bit working through this situation.

          Mar

            Write a Reply...