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
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;	  
   }

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.