0

I have 2 tables in a SQL database and I want to compare columns from them, then when there are more than 1 duplicate entries I want to count them and display with PHP and create a list of only the pageCatagorys that are duplicates

My two Tables look like this:

Table: pageCatagorys

ID | catagory
1  | Home
2  | Test 1
3  | Other Services 

Table: pages

ID | pageName | pageCatagory   | pageURL
1  | Test 1   | Test 1         | test-1
2  | Test 2   | Test 1         | test-2
3  | Test 3   | Home           | test-3
4  | Test 4   | Other Services | test-4

When I run the query $pagecount = mysql_query("SELECT pageCatagory, COUNT(*) FROM pages GROUP BY pageCatagory HAVING COUNT(*) > 1"); in phpMyAdmin i get the below result table:

pageCatagory     | COUNT(*)
Test 1           |    2

However its always hitting the else rule in my code below.

<?php
            $sql = mysql_query("SELECT catagory FROM pageCatagorys");

                while($row = mysql_fetch_assoc($sql)) { 
                    $pagecount = mysql_query("SELECT pageCatagory, COUNT(*) FROM pages GROUP BY pageCatagory HAVING COUNT(*) > 1");
                        if($pagecount > 1){
                        ?>
                        <li class="dropdown"><a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"><?php echo $row["catagory"] ?><span class="caret"></span></a>
                        <ul class="dropdown-menu" role="menu">
                        <?php   $sql1 = mysql_query("SELECT pageName, pageURL FROM pageURL"); 
                            while($row1 = mysql_fetch_assoc($sql1)) { ?>
                            <li><a href="<?php echo $row["pageName"] ?>"><?php echo $row["pageName"] ?></a></li>
                            <li class="divider"></li>
                        <?php } ?>
                        </ul>
                <?php  }  else { ?> 
                <li><a href="#"><?php echo $row["catagory"] ?></a></li>
   <?php } } ?>
  • 1
    already answered here: [link](http://stackoverflow.com/questions/12528644/count-duplicates-records-in-mysql-table) – John Pangilinan Dec 10 '16 at 16:33
  • You are not assigning `$pagecount`. This is strictly a php issue . . . and you should start using mysqli_. – Gordon Linoff Dec 10 '16 at 16:34
  • `$pagecount` is a resource, fetch it – chris85 Dec 10 '16 at 16:37
  • 2
    Every time you use [the `mysql_`](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) database extension in new code **[a Kitten is strangled somewhere in the world](http://2.bp.blogspot.com/-zCT6jizimfI/UjJ5UTb_BeI/AAAAAAAACgg/AS6XCd6aNdg/s1600/luna_getting_strangled.jpg)** it is deprecated and has been for years and is gone for ever in PHP7. If you are just learning PHP, spend your energies learning the `PDO` or `mysqli` database extensions. [Start here](http://php.net/manual/en/book.pdo.php) – RiggsFolly Dec 10 '16 at 16:48
  • 1
    I dunno about the kittens, @RiggsFolly, but I do know that every time somebody uses the `mysql_` interface, a cybercriminal pwns a website somewhere in the world. – O. Jones Dec 10 '16 at 17:33

0 Answers0