0

For a particular process I am storing a list of mysql queries in Database using php variables for condition data retrieval.

I'm storing:

Select count(1) from Users where user='$user1'

I have written a script where I first initialize: $user1="XYZ";

Then I select the above query from database and try to execute it again using mysql_query.

But the value of $user1 is not getting initialized as required.

Please fidn below the psuedo code:

$user1="xyz";
//selecting the query
$select_table="SELECT * FROM `test`";
$result_table=mysql_query($select_table);        
$select_query=mysql_result($result_table,$j,"Select_Query");
$panelinttable_result=mysql_query($select_query);
Kevin
  • 41,694
  • 12
  • 53
  • 70
  • Sidenote, please also have a look at this question: [Why shouldn't I use mysql_* functions in PHP?](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php?rq=1) – bcmcfc Aug 11 '14 at 07:32

1 Answers1

0

This will not work as when you retrieve data, $user1 will not be a variable but a part of string "Select count(1) from Users where user='$user1'".

Using str_replace() should work :

$select_query = mysql_result($result_table,$j,"Select_Query");
$select_query = str_replace('$user1', $user1, $select_query);
$panelinttable_result=mysql_query($select_query);

Also, why are you storing those queries in database ? It would be better to make functions for this.

function userExists($user) {
   $query = mysql_query("Select count(1) from Users where user = '".$user."'");
   return mysql_num_rows($query);
}
fdehanne
  • 1,658
  • 1
  • 16
  • 32