UKArea Posted October 3, 2014 Share Posted October 3, 2014 Hi,I am trying to store a sql query in a database, but every time I try to read it, it shows all the code as text. eg just print ' . $parts_row['part_number'] . 'Here is the code on the page: $parts_sql = "SELECT * FROM parts WHERE part_cat = " . $category_row['cat_id'] . " ORDER BY CAST(part_a AS DECIMAL(10,2))"; $parts_result = mysql_query($parts_sql); if(!$parts_result) { echo '<tr><td>The parts could not be displayed, please try again later.</tr></td></table>'; } else { while($parts_row = mysql_fetch_array($parts_result)) { $pageformat_sql = "SELECT * FROM category_pages WHERE catpage_number = " . $category_row['cat_page'] . ""; $pageformat_result = mysql_query($pageformat_sql); if(!$pageformat_result) { echo 'Error'; } else { while($pageformat_row = mysql_fetch_assoc($pageformat_result)) { $data = $pageformat_row['catpage_table']; echo '<table class="table2 centre" style="width:750px"> ' . $pageformat_row['catpage_tabletitle'] . '' . $data . ''; } } }} And this is what is stored in the database table: <tr> <td class="cell left">' . $parts_row['part_number'] . '</td> <td class="cell centre">' . $parts_row['part_a'] . ' mm</td> <td class="cell centre">' . $parts_row['part_b'] . ' mm</td> <td class="cell centre">' . $parts_row['part_c'] . ' mm</td> <td class="cell centre">' . $parts_row['part_d'] . ' mm</td> <td class="cell centre">' . $parts_row['part_e'] . ' mm</td> <td class="cell centre">' . $parts_row['part_imped100'] . '</td> <td class="cell centre"><a href="datasheets/' . $parts_row['part_datasheet'] . '.pdf"><img border="0" src="images/pdf.jpg" alt="Download"</a></td></tr> I would be very grateful to anyone who can help me with this. Quote Link to comment https://forums.phpfreaks.com/topic/291411-using-a-sql-query-thats-stored-in-a-table-on-the-database/ Share on other sites More sharing options...
NotionCommotion Posted October 3, 2014 Share Posted October 3, 2014 You do not want to store the HTML and PHP script in your database. You want to store data in your database, and then use PHP to create the HTML. Quote Link to comment https://forums.phpfreaks.com/topic/291411-using-a-sql-query-thats-stored-in-a-table-on-the-database/#findComment-1492603 Share on other sites More sharing options...
mac_gyver Posted October 3, 2014 Share Posted October 3, 2014 some other things you should and shouldn't do - don't run database queries inside of loops. use one JOINed query to retrieve the related data you want from your tables. separate your database retrieval 'business' logic from your html 'presentation' logic, see this post - http://forums.phpfreaks.com/topic/291210-passing-to-get-beginner-question/page-2?do=findComment&comment=1492189 don't use the old depreciated mysql_ functions, see this - http://php.net/manual/en/mysqlinfo.api.choosing.php edit: also, based on the column names you are using - part_a, part_b, ... you need to give your database columns meaningful names and likely need to normalize the data. Quote Link to comment https://forums.phpfreaks.com/topic/291411-using-a-sql-query-thats-stored-in-a-table-on-the-database/#findComment-1492608 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.