1

This is my Code

table_query
id query
1  SELECT * FROM TABLE WHERE date = "$loc_date"



 <?php
$q1 = 'SELECT query FROM table_query WHERE 1 LIMIT 1';
$result = execute q1;
$query = $result['query'];
    $loc_date = '2017-12-06';
    $loc_query = $query; //This query fetch from table. Not directly write on file
    execute query here
    ?>

But I don't get any result after executing query.

3
  • Add all the code . How we can know that you din't have any error in your next code part? The code which is actually necessary is hided by you. Please add that Commented Dec 6, 2017 at 6:59
  • String literals should be quoted like date = "$loc_date" in your case Commented Dec 6, 2017 at 6:59
  • 2
    you can simply change the query in double quotes instead of single quotes and it will work e.g $loc_query="SELECT * FROM TABLE WHERE date = $loc_date"; Commented Dec 6, 2017 at 7:02

4 Answers 4

2

You have to write variable in double quotation single quote.

$loc_date = '2017-12-06';
$loc_query = "SELECT * FROM TABLE WHERE date = '".$loc_date."'"; 
Sign up to request clarification or add additional context in comments.

Comments

1

Need to add the single or double quote in $loc_date string

for example

$loc_query = 'SELECT * FROM TABLE WHERE date = "'.$loc_date.'"';

OR

$loc_query = "SELECT * FROM TABLE WHERE date = '".$loc_date."'";

Comments

0

Put "" into the where condition.

$loc_query = 'SELECT * FROM TABLE WHERE date = "'.$loc_date.'"';

Then execute the query. It should give your result.

6 Comments

We have one table where all query are stored. and get all that query and execute one by one.
But In some query we have to pass dynamic value, for ex date, name etc.
Did You get now?
No, because the question you posted and the comment you explained are not the same.
$loc_date declare at the top of php file. and same variable use in query, But this query come form table.
|
0

You can try this one :)

//creates database connection
$connection=mysql_connect("localhost","root","");
//selects database connection
$database_select=mysql_select_db("your_database_name",$connection);

//put the entered date to loc_date variable
$loc_date='2017-12-06';

//select * date with 2017-12-06 on table_query but would display only 1   since theres a LIMIT
$query1=mysql_query("select * from table_query WHERE date='$loc_date' LIMIT 1");
while($loc_query=mysql_fetch_array($query1)){
echo $loc_query['date']; //prints the date - 2017-12-06 if present
}

I think you want to display the date which is '2017-12-06' on your table_query. But since you want to limit the record to be displayed to 1, so only one(1) date which is 2017-12-06 would be displayed on that code. If theres more than 1 record(date:2017-12-06) saved on that table, you can remove the "LIMIT 1" on mysql query so that it will display all.

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.