Jump to content

Using a sql query thats stored in a table on the database.


UKArea

Recommended Posts

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.

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.

Archived

This topic is now archived and is closed to further replies.

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