Database Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Codewalkers ForumsOther TechnologiesDatabase Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Codewalkers Forums Sponsor:
  #1  
Old February 18th, 2004, 01:24 AM
dude051 dude051 is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Texas
Posts: 8 dude051 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via AIM to dude051
selecting a table in a query from a variable passed from PHP... possible?

Im creating a dynamic library of tables. The setup is simple... and I have seen it on many sites. I display a table with titles. Apon clicking that title, I need to go out and display all the data for that one title from a different table (for orginazation and speed). I have 4 tables... one containing the Titles.. and the other 3 containing the info for the titles separated into types. My code is:

php Code:
Original - php Code
  1.  
  2. <?
  3. $view=$_GET[view];
  4. $id=$_GET[id];
  5.  
  6. if(!$view && !$id)
  7. {
  8. mysql_connect('localhost','*******','*******');
  9. mysql_select_db('releases');
  10. $query = mysql_query('SELECT * FROM anime');
  11.    
  12. echo "<table width="527" border="1" cellpadding="2" cellspacing="0" bordercolor="#000000">n";
  13. echo "  <tr bordercolor="#666666" bgcolor="#666666">n";
  14. echo "    <td width="239"><div align="center" class="style5">Anime Title</div></td>n";
  15. echo "    <td width="90"><div align="center" class="style5">Anime Type</div></td>n";
  16. echo "    <td width="90"><div align="center" class="style5">Completed</div></td>n";
  17. echo "    <td width="90"><div align="center" class="style5">To Be Done </div></td>n";
  18. echo "    <td width="90"><div align="center" class="style5">Total</div></td>n";
  19. echo "  </tr>n";
  20.  
  21. $num = 1;
  22.  
  23. while($anime = mysql_fetch_array($query))
  24. {
  25.  
  26.     if($num == 1)
  27.     {
  28.     echo "  <tr bordercolor="#000000">n";
  29.     echo "    <td bordercolor="#FFFFFF"><a href="releases.php?view=anime_".$anime['type']."&id=".$anime['title']."&page=1">".$anime['title']." </a></td>n";
  30.     echo "    <td bordercolor="#FFFFFF"><div align="center">".$anime['type']."</div></td>n";
  31.     echo "    <td bordercolor="#FFFFFF"><div align="center">".$anime['complete']."</div></td>n";
  32.     echo "    <td bordercolor="#FFFFFF"><div align="center">".$anime['todo']."</div></td>n";
  33.     echo "    <td bordercolor="#FFFFFF"><div align="center">".$anime['total']."</div></td>n";
  34.     echo "  </tr>n";
  35.  
  36.     $num++;
  37.     }
  38.     else
  39.     {
  40.     echo "  <tr bordercolor="#CCCCCC" bgcolor="#CCCCCC">n";
  41.     echo "    <td><a href="releases.php?view=anime_".$anime['type']."&title=".$anime['title']."&page=1">".$anime['title']." </a></td>n";
  42.     echo "    <td><div align="center">".$anime['type']."</div></td>n";
  43.     echo "    <td><div align="center">".$anime['complete']."</div></td>n";
  44.     echo "    <td><div align="center">".$anime['todo']."</div></td>n";
  45.     echo "    <td><div align="center">".$anime['total']."</div></td>n";
  46.     echo "  </tr>n";
  47.  
  48.     $num = 1;
  49.     }
  50. }
  51.  
  52. echo "  <tr bordercolor="#FFFFFF" bgcolor="#FFFFFF">n";
  53. echo "    <td>&nbsp;</td>n";
  54. echo "    <td>&nbsp;</td>n";
  55. echo "    <td>&nbsp;</td>n";
  56. echo "    <td>&nbsp;</td>n";
  57. echo "  </tr>n";
  58. echo "</table>n";
  59. }
  60. else
  61. {
  62. mysql_connect('localhost','*********','*********')  ;
  63. mysql_select_db('releases');
  64. $query = mysql_query('SELECT * FROM $view where title=$title');
  65.  
  66. while($anime = mysql_fetch_array($query))
  67. {
  68. echo "<table width="539" height="170" border="1" cellpadding="3" bordercolor="#FFFFFF">n";
  69. echo "  <!--DWLayoutTable-->n";
  70. echo "  <tr>n";
  71. echo"    <td height="178" valign="top" bordercolor="#FFFFFF"><div align="center">".
  72.       "<p><br>".
  73.         "<br>".
  74.         "<img src="".$anime['image']." width="160" height="120" border="1"></p>".
  75.       "</div></td>".
  76.     "<td valign="top" bordercolor="#FFFFFF"><div align="left">".
  77.                 "<table width="344" height="170" border="0" cellpadding="2" cellspacing="0">".
  78.                   "<tr>".
  79.                     ".<td width="48" height="16">&nbsp;</td>".
  80.                     "<td width="288">&nbsp;</td>".
  81.                   "</tr>".
  82.                   "<tr bgcolor="#CCCCCC">".
  83.                     "<td bordercolor="#FFFFFF"><span class="style7">Title:</span></td>".
  84.                     "<td bordercolor="#FFFFFF">".$anime['ep_title']."</td>".
  85.                   "</tr>".
  86.                   "<tr>".
  87.                     "<td bordercolor="#FFFFFF" bgcolor="#FFFFFF"><span class="style7">Episode:</span></td>".
  88.                     "<td>".$anime['ep_num']."</td>".
  89.                   "</tr>".
  90.                   "<tr bgcolor="#CCCCCC">".
  91.                     "<td bordercolor="#FFFFFF"><span class="style7">CRC:</span></td>".
  92.                     "<td bordercolor="#FFFFFF">".$anime['crc']."</td>".
  93.                   "</tr>".
  94.                   "<tr>".
  95.                     "<td bordercolor="#FFFFFF" bgcolor="#FFFFFF"><span class="style7">Filesize:</span></td>".
  96.                     "<td bordercolor="#FFFFFF" bgcolor="#FFFFFF"><div align="justify">".$anime['filesize']."</div></td>".
  97.                   "</tr>".
  98.                   "<tr bgcolor="#CCCCCC">".
  99.                     "<td bordercolor="#FFFFFF"><span class="style7">Filename:</span></td>".
  100.                     "<td bordercolor="#FFFFFF">".$anime['filename']."</td>".
  101.                   "</tr>".
  102.                   "<tr>".
  103.                     "<td bordercolor="#FFFFFF" bgcolor="#FFFFFF"><span class="style7">Encoder:</span></td>".
  104.                     "<td bordercolor="#FFFFFF" bgcolor="#FFFFFF">".$anime['encoder']."</td>".
  105.                   "</tr>".
  106.                   "<tr bgcolor="#CCCCCC">".
  107.                     "<td bordercolor="#FFFFFF"><span class="style7">Codec:</span></td>".
  108.                     "<td bordercolor="#FFFFFF">".$anime['codec']."</td>".
  109.                   "</tr>".
  110.                   "<tr>".
  111.                     "<td height="12" bordercolor="#FFFFFF" bgcolor="#FFFFFF"><span class="style7">Notes:</span></td>".
  112.                     "<td bordercolor="#FFFFFF" bgcolor="#FFFFFF">".$anime['notes']."</td>".
  113.                   "</tr>".
  114.                   "<tr>".
  115.                     "<td height="12" bordercolor="#FFFFFF" bgcolor="#FFFFFF">&nbsp;</td>".
  116.                     "<td bordercolor="#FFFFFF" bgcolor="#FFFFFF">&nbsp;</td>".
  117.                   "</tr>".
  118.                 "</table>n";
  119. }
  120. }   
  121. ?>

Prob alot of unnecessary code there... sorry. but I placed it in its entirety to also see if the code ive written so far couldnt be improved. Im of course a n00b at this... ive gotten the first table to display... but once they click and the "link releases?$view=whatever&title=whatever" is sent back to be proscessed in the second half of code.... the varibles dont seem to come in because I get the error "Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /public_html/releases.php on line 130" which tells me it can't find the table. So can variables be passed into a query to select a table like "$query = mysql_query('SELECT * FROM $view where title=$title');" ?? Any better coding suggestions to my code is appreciated, and of course a solution to the problem is appreciated even more. laterz

Reply With Quote
  #2  
Old February 18th, 2004, 01:38 AM
CodeKadiya CodeKadiya is offline
Codewalkers Regular (2000 - 2499 posts)
 
Join Date: Apr 2007
Location: Colombo,Sri Lanka
Posts: 2,313 CodeKadiya User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 4
Send a message via Yahoo to CodeKadiya
RE: selecting a table in a query from a variable passed from PHP... possible?

I modified your code a bit.... can you check this and let me know the result?
php Code:
Original - php Code
  1.  
  2. <?
  3. $view=$_GET['view'];
  4. $id=$_GET['id'];
  5. $title=$_GET['title'];
  6.  
  7. $conn = mysql_connect('localhost','*******','*******');
  8. mysql_select_db('releases',$conn);
  9.  
  10. if(!$view && !$id)
  11. {
  12.     $query = mysql_query("SELECT * FROM anime") or die("Error : ".mysql_error());
  13.    
  14.     echo "<table width="527" border="1" cellpadding="2" cellspacing="0" bordercolor="#000000">n";
  15.     echo " <tr bordercolor="#666666" bgcolor="#666666">n";
  16.     echo " <td width="239"><div align="center" class="style5">Anime Title</div></td>n";
  17.     echo " <td width="90"><div align="center" class="style5">Anime Type</div></td>n";
  18.     echo " <td width="90"><div align="center" class="style5">Completed</div></td>n";
  19.     echo " <td width="90"><div align="center" class="style5">To Be Done </div></td>n";
  20.     echo " <td width="90"><div align="center" class="style5">Total</div></td>n";
  21.     echo " </tr>n";
  22.    
  23.     $num = 1;
  24.    
  25.     while($anime = mysql_fetch_array($query))
  26.     {
  27.         if($num == 1)
  28.         {
  29.             echo " <tr bordercolor="#000000">n";
  30.             echo " <td bordercolor="#FFFFFF"><a href="releases.php?view=anime_".$anime['type']."&id=".$anime['title']."&page=1">".$anime['title']." </a></td>n";
  31.             echo " <td bordercolor="#FFFFFF"><div align="center">".$anime['type']."</div></td>n";
  32.             echo " <td bordercolor="#FFFFFF"><div align="center">".$anime['complete']."</div></td>n";
  33.             echo " <td bordercolor="#FFFFFF"><div align="center">".$anime['todo']."</div></td>n";
  34.             echo " <td bordercolor="#FFFFFF"><div align="center">".$anime['total']."</div></td>n";
  35.             echo " </tr>n";
  36.            
  37.             $num++;
  38.         }
  39.         else
  40.         {
  41.             echo " <tr bordercolor="#CCCCCC" bgcolor="#CCCCCC">n";
  42.             echo " <td><a href="releases.php?view=anime_".$anime['type']."&title=".$anime['title']."&page=1">".$anime['title']." </a></td>n";
  43.