5

I'm trying to create a dynamic code that would ready from any table with the certain name but the difference between each table name is a number that is generated by a variable: for example :

//that's how I get my variable the value for example is = 3

$pid = $GLOBALS["localid"];

//the table name for example is tablename_3

$strTable = "tablename_" .$pid;

//here's how the query should look like

$query = "SELECT * FROM . $strTable . where .....;

I'm making a mistake somewhere but can't figure it out and would appreciate a little help please

4
  • 2
    That is not how you use SQL. Predefine your queries. Don't patch them together! Commented Nov 28, 2017 at 11:25
  • 1
    echo your query, so that it will easy to find out the issue Commented Nov 28, 2017 at 11:28
  • Change your query to this :- $query = "SELECT * FROM $strTable where ....."; Commented Nov 28, 2017 at 11:32
  • Validate the Global variable first and check the quotes, you don't need dots in the double quotes string for a variable. it takes variable as a value "SELECT * from $table_name where = $value"; Commented Nov 28, 2017 at 11:36

2 Answers 2

3

Remove the dots and also make sure you have single quotes aroung where

$query = "SELECT * FROM $strTable where '.....';
Sign up to request clarification or add additional context in comments.

Comments

1

Besides the comments about do or don't build your queries like this...

You're not closing the quotes properly.

$query = "SELECT * FROM . $strTable . where .....; //Double quote not closed.

should be:

$query = 'SELECT * FROM' . $strTable . 'where .....'; //Single quoted strings concatenated with variable.

or

$query = "SELECT * FROM $strTable where ....."; //Variable inside double quoted string.

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.