Jump to content

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


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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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