0

I have an sql query :

$variable_tablename;
$componentkhy1 = $Ns_Azuredb->get_results("SELECT * FROM tabkh WHERE TabkhSystem = '".$varInput_CalcSystemKh."';");

foreach ($componentkhy1 as $rs_componentkhy1)
{
    $khsysy1 = $rs_componentkhy1->table_name;
}

instead of the table_name, i would like to use the variable $variable_tablename is it possible to do so?

Thanks guys

2
  • Where is table_name? Do you mean tabkh? Commented May 8, 2020 at 16:55
  • $khsysy1 = $rs_componentkhy1->table_name; on here instead of table_name(it should have been colum name so appologies ) i want to use a variable. Commented May 8, 2020 at 18:56

1 Answer 1

0

Just substitute the variable into the string

$variable_tablename = 'tabkh';
$componentkhy1 = $Ns_Azuredb->get_results("SELECT * FROM `$variable_tablename` WHERE TabkhSystem = '".$varInput_CalcSystemKh."';");

If the variable is coming from the user, make sure you check it against a whitelist to prevent SQL injection.

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

18 Comments

Hi, thanks for the reply, what i actually want to replace with a variable is on here: $khsysy1 = $rs_componentkhy1->table_name; instead of table_name, the variable. so on my case the table name is created dynamically, thats why
The properties of the object are column names, not table names.
Use a variable in place of the property name: $rs_componentkhy1->$variable_tablename
Thats how i tried but nothing happening. Also php editor shows that as error. Is there a standard format to use it?
It should work. Update the question with your actual code.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.