0

I have MYSQL query like the below:

$sourcedata = mysql_fetch_array(
                mysql_query("SELECT Whereclause 
                               FROM generaltable 
                              WHERE ID = '1'"
                           )
                );

$Whereclause = $sourcedata['Whereclause'];

In "Whereclause" field from "generaltable" in database, consist of the below text:

WHERE username = '$_SESSION[username]'

Then I have other query:

$data = mysql_fetch_array(
                   mysql_query("SELECT * 
                                  FROM usertable 
                                       $Whereclause"
                              )
                        );

When I echo $data['username'], it is not showing anything.

If I copy the value from database to replace $Whereclause it is working fine, also if I replace '$_SESSION[username]' with 'admin' for example, it is working fine.

How can I solve this? Need your help please.

Thank you very much.

3
  • try to print "SELECT * FROM usertable $Whereclause" and query it in your mysql editor Commented Oct 17, 2014 at 8:24
  • @Arif_suhail_123 yes, I put that value in database field. Commented Oct 17, 2014 at 8:49
  • @Pantamtuy Do you mean from sql query in phpmyadmin? Can it identify the $Whereclause? Commented Oct 17, 2014 at 8:53

2 Answers 2

1

As you are using fetch array. Can you please try with

$data[0]['username']
Sign up to request clarification or add additional context in comments.

Comments

0

The problem you're having is that $Whereclause is a String, so it is not evaluated ('$_SESSION[username]' remains exactly as it is).

You have to tell PHP to evaluate this string with the function eval():

$data = mysql_fetch_array(
          mysql_query(
           eval("return \"SELECT * FROM usertable $Whereclause\";")
         )
        );

2 Comments

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in ...
True! I'm going to correct it, look back the answer in a minute

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.