User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the MySQL section within the Web Development category of DaniWeb, a massive community of 391,557 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,694 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our MySQL advertiser:
Views: 4209 | Replies: 6
Reply
Join Date: Apr 2007
Posts: 4
Reputation: BeerGuzler is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
BeerGuzler BeerGuzler is offline Offline
Newbie Poster

Help Query multiple tables with duplicate data

  #1  
Apr 9th, 2007
Hi , I've been working on comparison query with PHP and MySql 5.0 yet not getting anywhere with it.

I'm trying to perform a search ( query )

I have several tables within the same database that contain same fields yet hold different data

eg
Table 1
id |Product | Price | Private

Table 2
id| Product | Price | Private

etc...

What I'm trying to do is have the ability to search the tables and display it as follows

Products (tables 1 & 2 ) | Price( Table 1 ) | Price (Table 2 )

I've been playing with UNION
$query = "Select `product`, `private` as p, 'table1' as n from `table1` where `name` REGEXP '".$trimmed."' UNION ALL Select `product`, `private` as p, 'table2' as n from `table2` where `name` REGEXP '".$trimmed."' ORDER by `product`"; 

Output in php as follows
while($row = mysql_fetch_array($result))
 
 
 
                                                 {
                                                 echo "<tr>";
 
                                                 //echo "<td>" . $row['Product'] . "</td>";
                                                                // echo "<td>" . $row['Product'] . "</td>";
                                                                // echo "<td>" . $row['private'] . "</td>";
                                                                // echo "<td>" . $row['id'] . "</td>";
                                                                // echo "<td>" . $row['table1.price'] . "</td>";
                                                                // echo "<td>" . $row['table2.price'] . "</td>";
                                                                // echo "</tr>";
 
                                                 echo "<td>" . $row['name'] . "</td>";
                                                 echo "<td>" . $row['n'] . "</td>"; 
                                                 echo "<td>" . $row['p'] . "</td>"; 
                                                 echo "</tr>";
                                                 }

So far I cannot seem to get the products nor pricing in line , any ideas ?
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Nov 2006
Location: South Wales
Posts: 159
Reputation: phper is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 10
phper's Avatar
phper phper is offline Offline
Junior Poster

Re: Query multiple tables with duplicate data

  #2  
Apr 13th, 2007
So the prices are different in the two tables?
Reply With Quote  
Join Date: Apr 2007
Posts: 4
Reputation: BeerGuzler is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
BeerGuzler BeerGuzler is offline Offline
Newbie Poster

Re: Query multiple tables with duplicate data

  #3  
Apr 13th, 2007
Originally Posted by phper View Post
So the prices are different in the two tables?


Yes, product names will be very similar , prices different.

have multiple vendors , so I want the ability to perform a seach against the tables and have the Products shows via union all with the prices in different colums

Products | Price tbl1 | Price tbl2| price tble3 |

Trying to wrap my head around it still.
Reply With Quote  
Join Date: Mar 2004
Posts: 715
Reputation: Phaelax is on a distinguished road 
Rep Power: 6
Solved Threads: 28
Phaelax Phaelax is offline Offline
Master Poster

Re: Query multiple tables with duplicate data

  #4  
Apr 18th, 2007
similar and the same are two different things. If you know for sure they will be the same then you can link the two tables together.

Give this a try. It will, however assume that the same product does in fact lie within both tables. If the product isn't in both tables, then I don't think it will return that item at all. If that's the case, then you'll need some sort of outer join.

  1. $query = "SELECT table1.product,table1.price,table2.price FROM table1, table2 WHERE table1.product = table2.product";
  2.  
  3. $result = mysql_db_query($database,$query);
  4. while ($row = mysql_fetch_array($result))
  5. {
  6. echo $row[0]." - $".$row[1].", $".$row[2]."<br>";
  7. }
Reply With Quote  
Join Date: Apr 2007
Posts: 9
Reputation: arty56 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
arty56 arty56 is offline Offline
Newbie Poster

Solution Re: Query multiple tables with duplicate data

  #5  
Apr 21st, 2007
Try this for a solution.
save the following as a php page or html doesn't matter.
search.php or search.html
  1. <form name='search' action='search1.php' method='post'>
  2. <CENTER>
  3. <TABLE>
  4. <TR>
  5. <TD>Search for Product:</TD>
  6. <TD><input type="text" name="product" size="25"></TD>
  7. </TR>
  8. </TABLE>
  9. <CENTER>
  10. <input type='submit' name="submit" value="Search For Product"'>
  11. </form>

Save the following as search1.php
  1. <?
  2. // your connection to the server
  3. require("includes/dbconnect.php");
  4. ?>
  5. <?
  6. //post sent from search form.
  7. if(isset($_POST['submit']))
  8. {
  9. $product = addslashes(@$_POST['product']);
  10. }
  11. // Begin your table outside of the array
  12. echo "<table width='100%' border='0' cellpadding='4' cellspacing='0'>
  13. <tr>
  14. <td width='10%' bgcolor='#BBDDFF'><b>ID #</b></td>
  15. <td width='20%' bgcolor='#BBDDFF'><b>Product</b></td>
  16. <td width='30%' bgcolor='#BBDDFF'><b>Tbl #1 Price</b></td>
  17. <td width='20%' bgcolor='#BBDDFF'><b>Tbl #2 Price</b></td>
  18. <td width='20%' bgcolor='#BBDDFF'><b>Private</b></td>
  19. </tr>";
  20. // Define your colors for the alternating rows
  21.  
  22. $color1 = "#ffffff";
  23. $color2 = "#ccffff";
  24. $row_count = 0;
  25. // Perform an statndard SQL query:
  26. $page = $_GET['page'];
  27. $limit = 150;
  28. $query_count = "SELECT * FROM n inner join p on n.product = p.product WHERE n.product LIKE '%$product%' order by n.product asc";
  29. $result_count = mysql_query($query_count);
  30. $totalrows = mysql_num_rows($result_count);
  31. if(empty($page)){
  32. $page = 1;
  33. }
  34. $limitvalue = $page * $limit - ($limit);
  35.  
  36. $sql_events = mysql_query("SELECT * FROM n inner join p on n.product = p.product WHERE n.product LIKE '%$product%' order by n.product asc LIMIT $limitvalue, $limit") or die (mysql_error());
  37. // We are going to use the "$row" method for this query. This is just my preference.
  38.  
  39. while ($row = mysql_fetch_array($sql_events)) {
  40. $Id = stripslashes($row[0]);
  41. $product = stripslashes($row[1]);
  42. $price = stripslashes($row[2]);
  43. $price1 = stripslashes($row[3]);
  44. $private = stripslashes($row[4]);
  45.  
  46. /* Now we do this small line which is basically going to tell
  47.   PHP to alternate the colors between the two colors we defined above. */
  48.  
  49. $row_color = ($row_count % 2) ? $color1 : $color2;
  50.  
  51. // Echo your table row and table data that you want to be looped over and over here.
  52.  
  53. echo "<tr>
  54. <td bgcolor='$row_color'>$Id</td>
  55. <td bgcolor='$row_color'>$product</td>
  56. <td bgcolor='$row_color'>$$price</td>
  57. <td bgcolor='$row_color'>$$price1</td>
  58. <td bgcolor='$row_color'>$private</td>
  59. </tr>";
  60. // Add 1 to the row count
  61. $row_count++;
  62. }
  63. // Close out your table.
  64. echo "</TABLE>";
  65. echo "<center><hr color=#c0c0c0>";
  66. if($page != 1)
  67. {
  68. $pageprev = $page-1;
  69. echo("<a href=\"$PHP_SELF?page=$pageprev\">PREV</a>&nbsp;&nbsp;&nbsp;");
  70. }
  71. else
  72. {
  73. echo("PREV&nbsp;&nbsp;&nbsp;&nbsp;");
  74. }
  75.  
  76. $numofpages = $totalrows / $limit;
  77.  
  78. for($i = 1; $i <= $numofpages; $i++)
  79. {
  80. if($i == $page)
  81. {
  82. echo($i."&nbsp;");
  83. }
  84. else
  85. {
  86. echo("<a href=\"$PHP_SELF?page=$i\">$i</a>&nbsp;");
  87. }
  88. }
  89. if(($totalrows % $limit) != 0)
  90. {
  91. if($i == $page)
  92. {
  93. echo($i."&nbsp;");
  94. }
  95. else
  96. {
  97. echo("<a href=\"$PHP_SELF?page=$i\">$i</a>&nbsp;");
  98. }
  99. }
  100.  
  101. if(($totalrows - ($limit * $page)) >=0)
  102. {
  103. $pagenext = $page+1;
  104.  
  105. echo("&nbsp;&nbsp;&nbsp;<a href=\"$PHP_SELF?page=$pagenext\">NEXT</a>");
  106. }
  107. else
  108. {
  109. echo("&nbsp;&nbsp;&nbsp;NEXT&nbsp;");
  110. }
  111. echo "</center>";
  112. ?>

Hope this helps.
Arty
Last edited by arty56 : Apr 21st, 2007 at 9:01 am.
Reply With Quote  
Join Date: Apr 2007
Posts: 4
Reputation: BeerGuzler is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
BeerGuzler BeerGuzler is offline Offline
Newbie Poster

Re: Query multiple tables with duplicate data

  #6  
Apr 21st, 2007
Thanks Arty56 ,
When I execute the code I get this return in the browser


PREV "); } else { echo("PREV "); } $numofpages = $totalrows / $limit; for($i = 1; $i <= $numofpages; $i++) { if($i == $page) { echo($i." "); } else { echo("$i "); } } if(($totalrows - ($limit * $page)) >=0) { $pagenext = $page+1; echo(" <A href="http://127.0.0.1/%22$PHP_SELF?page=$pagenext\"">NEXT"); } else { echo(" NEXT "); } echo "
"; ?>

I've double checked everything...any ideas
Reply With Quote  
Join Date: Apr 2007
Posts: 9
Reputation: arty56 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
arty56 arty56 is offline Offline
Newbie Poster

Solution Re: Query multiple tables with duplicate data

  #7  
Apr 21st, 2007
Originally Posted by BeerGuzler View Post
Thanks Arty56 ,
When I execute the code I get this return in the browser


PREV "); } else { echo("PREV "); } $numofpages = $totalrows / $limit; for($i = 1; $i <= $numofpages; $i++) { if($i == $page) { echo($i." "); } else { echo("$i "); } } if(($totalrows - ($limit * $page)) >=0) { $pagenext = $page+1; echo(" <A href="http://127.0.0.1/%22$PHP_SELF?page=$pagenext\"">NEXT"); } else { echo(" NEXT "); } echo "
"; ?>

I've double checked everything...any ideas


I have checked the script and the only change I made was to remove line 4 ?> and line 5 <?php

I also see you have added a http://127.0.0.1/%22 infront of $PHP_SELF.
testing on your own computer should not require an IP address.
and diffinately not required on your hosting server.
$PHP_SELF is telling php to use the same php page, so that maybe throwing an error.
I use $PHP_SELF instead of typing in search1.php.
As far as why the php code is showing check to see if you have removed or added an extra ?> in the code just before that error.
or you have a <?php missing.
Because it is reading the code as text and not php.
If you use the code as given without adding the IP address it will work.
I use it on all my sites.

Example 1:
The example shows that the second segment of code is missing the opening php tag. Php should throw up an error instead of echoing the second segment of code.

<?php
echo " some php code";
?>

echo "some more php code";
?>

Example 2:
The example shows that the second segment of code is missing the opening and closing php tag. So it will be parsed as text and not php
<?php
echo " some php code";
?>
echo "some more php code";


--Arty
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb MySQL Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the MySQL Forum

All times are GMT -4. The time now is 9:46 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC