dwex Posted January 21, 2011 Share Posted January 21, 2011 $tab is a variable for my table's name <?php while($row=mysqli_fetch_assoc($result)){ ?> <?php echo $row['{$tab}_name']?> Quote Link to comment https://forums.phpfreaks.com/topic/225178-what-is-the-correct-syntax-to-line-of-code/ Share on other sites More sharing options...
trq Posted January 21, 2011 Share Posted January 21, 2011 You shouldn't need to reference your table's name within the indexes of the array returned by mysqli_fetch_assoc. We need to see more code, including your query. Quote Link to comment https://forums.phpfreaks.com/topic/225178-what-is-the-correct-syntax-to-line-of-code/#findComment-1162958 Share on other sites More sharing options...
dwex Posted January 21, 2011 Author Share Posted January 21, 2011 the page before this sends a value to this page and the value is my table's name. <a href="displayguns.php?paintguns=guns"> guns is my table's name. <?php $tab = $_GET['paintguns']; $HOST = 'localhost'; $USERNAME = 'root'; $PASSWORD = ''; $DB = 'pbstore'; $link = mysqli_connect($HOST,$USERNAME,$PASSWORD,$DB) or die(mysqli_connect_error()); $sql = "SELECT {$tab}_id , {$tab}_name , {$tab}_price , {$tab}_image FROM {$tab} "; $result = mysqli_query($link, $sql) or die(mysqli_error($link)); ?> Quote Link to comment https://forums.phpfreaks.com/topic/225178-what-is-the-correct-syntax-to-line-of-code/#findComment-1162961 Share on other sites More sharing options...
trq Posted January 21, 2011 Share Posted January 21, 2011 Why are you naming all your fields prefixed with the table name? it makes no sense to do so. Quote Link to comment https://forums.phpfreaks.com/topic/225178-what-is-the-correct-syntax-to-line-of-code/#findComment-1162967 Share on other sites More sharing options...
dwex Posted January 21, 2011 Author Share Posted January 21, 2011 lol true, ah well... it's already been done. <?php echo $row['{$tab}_name']?> so is there no way I can do that? Quote Link to comment https://forums.phpfreaks.com/topic/225178-what-is-the-correct-syntax-to-line-of-code/#findComment-1162970 Share on other sites More sharing options...
trq Posted January 21, 2011 Share Posted January 21, 2011 Variables are not interpolated within single quotes. <?php echo $row["{$tab}_name"]?> Quote Link to comment https://forums.phpfreaks.com/topic/225178-what-is-the-correct-syntax-to-line-of-code/#findComment-1162971 Share on other sites More sharing options...
dwex Posted January 21, 2011 Author Share Posted January 21, 2011 is there a right way to do it then or the only way would be to change all the table names in my database? Quote Link to comment https://forums.phpfreaks.com/topic/225178-what-is-the-correct-syntax-to-line-of-code/#findComment-1162974 Share on other sites More sharing options...
trq Posted January 21, 2011 Share Posted January 21, 2011 or the only way would be to change all the table names in my database? You mean field names? It seems stupid to pre-pend your fields with there table name. eg; If you have a table called 'Users', why do you need a field called 'Users_Name'? 'Name' is sufficient. Quote Link to comment https://forums.phpfreaks.com/topic/225178-what-is-the-correct-syntax-to-line-of-code/#findComment-1162975 Share on other sites More sharing options...
PFMaBiSmAd Posted January 21, 2011 Share Posted January 21, 2011 He's also got an INSERT query where the table name and the column prefixes are from a form field (apparently selected from a drop down menu.) Before you spend any more time on this, you need to fix your database design. Your design is not efficient and is resulting in more complicated code than you need. You need to make your design general purpose. You don't create another table just because you add a new category or type and your column names should only indicate the purpose of the data (id, name, price, image), not what the data in them references. Quote Link to comment https://forums.phpfreaks.com/topic/225178-what-is-the-correct-syntax-to-line-of-code/#findComment-1162976 Share on other sites More sharing options...
dwex Posted January 21, 2011 Author Share Posted January 21, 2011 yeah , I forgotten why I did that. Anyway , thanks for the help! Quote Link to comment https://forums.phpfreaks.com/topic/225178-what-is-the-correct-syntax-to-line-of-code/#findComment-1162979 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.