Forum Moderators: coopster

Message Too Old, No Replies

Assigning a variable to a table

is this legal? seems to work ok

         

babyShiloh

11:09 pm on Mar 14, 2007 (gmt 0)

10+ Year Member



I'm new so I just wanna check if this is cool. I have a master page with a list of products - that list comes from a table that contains a column of other table names. These other tables break down the products into subcategories (like if you click on cars, it gives a break down of honda, ford, etc.). I set this up so once you click on, say cars, in the master page, the url sends a $_GET value with the subcategory table name to the next page. I then assign a variable (say $displayTable) to that $_GET variable and run the query below to extract the subcategories, like honda, toyota, etc.

Works pretty good since all the subcategory tables (like cars, pickup trucks, etc.) have the same column names - only the items in the tables change. I just can't seem to find any books or tutorials that assign a variable to a table (or even a database for that matter). Is there any problem with doing that?

$query="SELECT product FROM $displayTable";
$result=mysql_query($query);
while ($row=mysql_fetch_assoc($result)) {
echo $row['product']; echo "<br>";}

eelixduppy

12:12 am on Mar 15, 2007 (gmt 0)



Welcome to WebmasterWorld, babyShiloh!

>> Is there any problem with doing that?

No, there isn't, however, you want to check to see if those tables exist before allowing them to go into a query otherwise you may get unexpected results. Also, to prevent from sql injection, you want to escape any variables that you are using in the query, as well. For this, you should use mysql_real_escape_string [php.net].

Good luck! :)

babyShiloh

12:55 am on Mar 18, 2007 (gmt 0)

10+ Year Member



cool. thanks!

phpsir

2:34 pm on Mar 18, 2007 (gmt 0)

10+ Year Member



Cool!

eelixduppy

9:35 pm on Mar 18, 2007 (gmt 0)



Welcome to WebmasterWorld, phpsir!

Nice to have you :)