0

I am trying to execute the query below.

$condition = "WHERE emp_id = '$emp_id'"; 
$myquery = "SELECT * FROM emp_table".$condition;

I expect my query to be like this, but dynamically:

$myquery = "SELECT * FROM emp_table WHERE emp_id = '$emp_id'";

Is there anyway to make SQL statements dynamically through variables in php..?

7
  • what's the error you getting? Commented Sep 19, 2015 at 15:51
  • 2
    first, myquery is missing a $; second, it will read ... emp_tableWHERE ...; third: what database library are you using? mysqli? PDO? What's your code? Commented Sep 19, 2015 at 15:52
  • php.net/manual/en/language.operators.string.php Commented Sep 19, 2015 at 15:52
  • php.net/manual/en/pdo.prepare.php < this might help Commented Sep 19, 2015 at 15:52
  • thanks for the replies but nothing turned out.. :( Commented Sep 19, 2015 at 16:08

3 Answers 3

1

It should be

$condition = "WHERE emp_id = '$emp_id'"; 
$myquery = "SELECT * FROM emp_table ".$condition;

you forgot to put $ on myquery

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

Comments

0

You should never build queries dynamically like that. Correct way to do it is to use prepared statements.

In your case it'll be like that

$statement = $pdo->prepare("SELECT * FROM emp_table WHERE emp_id = :emp_id");
$statement->execute(array(
    ':emp_id' => $emp_id
));

$rows = $statements->fetchAll(PDO::FETCH_ASSOC);

3 Comments

Hi elon, I dont need to include variable in WHICH clause.. My requirement is, depending on the input, the WHERE clause itself is to be included r excluded.. ex) if emp-id is blank, then it should not include the condition emp_id = $emp_id
You should include that in your question.
Sorry for that, but my example stated that.. may be you dnt have a look
0

Have you tried this? -

$condition = "WHERE emp_id = '" . $emp_id . "'"; 
$myquery = "SELECT * FROM emp_table " . $condition;

PHP does not expand variables within single-quote strings.

echo "$name"; //works

echo '$name'; //does not work

Take a look at this: Single quotes or double quotes for variable concatenation?

5 Comments

Sterex, I tried but still its the same.. helpless :(
Where are you getting the error? And what is the error?
its not including the variables in SQL statement, i get error in $myquery,
Hey @Multi-Coder, although I hate linking to W3Schools, here's something that will help you get started with PHP/MySQL (looks like you are VERY new to this): w3schools.com/PHP/php_mysql_select.asp Can you try using their syntax for preparing and executing your queries?
hi.. thanks for ur response.. :) .. i am not new and i knew what all were there.. my problem is different.. example : i need a query = "select * from table" .. but i am spliting is as two variables.. $var1= "Select * ", $var2 = "from query". and finally, i execute my query as $myquery = $var1.$var2; .. But its not working

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.