Why is my Query not returning a result Background

I have a large table with lots of values and want to check if any of the names are repeating and only display those results. I have built a view and cannot get the SQL query to return the result I want.

Code

Here is the code as it stands, I've done loads of variations of it but cant get the result and think that there must be a core problem with my logic.

So I have built a view and selected a couple of column and now I want a column that will print out the name of the title the number of times it appears in the database. Or just count the number and print that.

&lt;?php
  $value = $row-&gt;title;
  $query= "SELECT node.title, node.type 
           FROM {node} node 
           WHERE  (node.type IN ('institution') 
           AND node.title  =".$value.")";
  $result= db_query($query);
  $wat = $result-&gt;fetchAll(PDO::FETCH_COLUMN, 0);
  foreach($wat as $res){echo $res;}
?&gt;


So as you can see I get the value of title at the row I'm on and compare it to every value for title in the database, then return only results that match. After that I loop through them and get the value printed. 

I have managed to get all the titles out and print them but as soon as i bring $value into it it breaks. I have checked the value of $value and it returns the expected result. I just cant figure out what is wrong with my SQL query. 

Question

What is wrong with my Query? is there something that stops me using the $row-&gt;title in drupal that I don't know about?
Without access to your error logs etc. I'm going to assume the problem is that you are not putting quotes around your title string, so it is trying to use the title as a database column and it fails.

It should be like this:

AND node.title  = '" . $value . "')";


That is not a great solution either though because it opens you up to possible SQL injection attacks so instead, use Drupal's built in sanitisation like this:

$query = "SELECT title, type 
            FROM {node}
           WHERE type = 'institution'
             AND title = :title";
$result = db_query($query, array(':title' =&gt; $value));


When you do it like that Drupal will sanitize the $value and it will also know that it is a string and wrap it in quotes.
For more information see the documentation for Drupal's database API - Static queries.

In the above example I also changed the type "IN" operator to "=" because you only had one value, removed the redundant brackets around the where clause and removed the table alias to make the query as simple as possible.

On the topic of EntityFieldQuery, there are multiple different ways to run a query in Drupal and the best solution depends on the particular use case.
If you want optimal performance and don't need to restrict results based on node access then db_query() is likely the best method.
Without knowing your requirements I couldn't advise on the best approach for your current query.