Jump to content

Variables in sql query?


Lee

Recommended Posts

Can I use a variable inside an sql query to determine which table to select from? The 2 functions below do exactly the same thing, they're just selecting data from different tables. I'm not sure how I can do it. Maybe put a parameter in the function & use sprinf?

 

// Output the page data
function showpages()
  {
  	db_connect();			
$query = ("SELECT * FROM pages");  // can I change pages to a variable somehow?			
$result = mysql_query($query);		
    $result = result_to_assoc($result);
    return $result;	  
   }

// Echo the pricelist data into the pricelist form
function show_pricelist()
  {
db_connect();			
$query = ("SELECT * FROM pricelist");  // Again, if pricelist can be a variable, then I need only 1 function			
$result = mysql_query($query);		
    $result = result_to_assoc($result);
    return $result;	  
   }

Link to comment
https://forums.phpfreaks.com/topic/224825-variables-in-sql-query/
Share on other sites

function show_pricelist($table)
  {
db_connect();			
$query = ("SELECT * FROM {$table}");  // Again, if pricelist can be a variable, then I need only 1 function			
$result = mysql_query($query);		
    $result = result_to_assoc($result);
    return $result;	  
   }

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.