Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Variable inside mysql_query

Status
Not open for further replies.

alsaffar

Programmer
Oct 25, 2001
165
0
0
KW
Hi there,

How I can use a varibale inside mysql_query?

I'm passing a varibale $TableName to a function, and inside the function I make my query depend on the passed variable.

$TableName = 'Users';
TestFunction($TableName);


Function TestFunction($TableName)
{
$Query = " SELECT ID FROM $TableName ";
$Result = mysql_query($Query) or die("Query Failed");
}

How I can get this function works fine?
 
Your problem is not using the variable in an SQL query. Your problem is that your variable is not available inside your function.

You need to tell your function that the variable $TableName exists outside the function. The "global" keywork is used for this purpose.

Read for more information ______________________________________________________________________
TANSTAAFL!
 
Hi alsaffar! Have you tried:
$TableName = 'Users';
TestFunction($TableName);


Function TestFunction($TableName)
{
$Query = " SELECT ID FROM " . $TableName;
$Result = mysql_query($Query) or die("Query Failed");
}

The dot (.) is a concatenation operator, so when the query is processed it'll be read:
SELECT ID FROM Users

Hope this works for you!
Cheers
Laura
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top