SQL | Brain Goo https://www.popmartian.com/tipsntricks Carpe Crap 'em Fri, 06 Feb 2009 17:00:59 +0000 en-US hourly 1 https://wordpress.org/?v=6.9.10 SQL: Select duplicate rows, select indistinct rows https://www.popmartian.com/tipsntricks/2008/10/27/sql-select-duplicate-rows-select-indistinct-rows/ https://www.popmartian.com/tipsntricks/2008/10/27/sql-select-duplicate-rows-select-indistinct-rows/#comments Mon, 27 Oct 2008 16:09:29 +0000 http://www.popmartian.com/tipsntricks/?p=67 Using SQL, MySQL or whatever your favorite database may be, we all know using SELECT DISTINCT will return unique values in case a value appears more than once in a table. How do you fine duplicates? What if I want … Continue reading →

The post SQL: Select duplicate rows, select indistinct rows first appeared on Brain Goo.

]]>
Using SQL, MySQL or whatever your favorite database may be, we all know using SELECT DISTINCT will return unique values in case a value appears more than once in a table.

How do you fine duplicates? What if I want to know where the duplicates are?

The trick is to use count() and compare it to an integer.

SELECT count(*), locations.* FROM locations GROUP BY number HAVING COUNT(*) > 1

That will select a count of unique instances of a value from a table called “locations”, group them by a field called “number” where the count is greater than 1. In other words, it will return all the fields where there are more than one instance of the same value in the column “number”.

Did you find this post useful or have questions or comments? Please let me know!

The post SQL: Select duplicate rows, select indistinct rows first appeared on Brain Goo.

]]>
https://www.popmartian.com/tipsntricks/2008/10/27/sql-select-duplicate-rows-select-indistinct-rows/feed/ 1
Quick Character Escaping in PHP https://www.popmartian.com/tipsntricks/2007/10/10/quick-character-escaping-in-php/ https://www.popmartian.com/tipsntricks/2007/10/10/quick-character-escaping-in-php/#respond Wed, 10 Oct 2007 17:48:14 +0000 http://www.popmartian.com/tipsntricks/2007/10/10/quick-character-escaping-in-php/ When writing PHP web apps, I tend to run in to a portability issue when dealing with SQL connectivity. Since I can’t count on having the PEAR DB module available, I rolled my own set of functions to interact with … Continue reading →

The post Quick Character Escaping in PHP first appeared on Brain Goo.

]]>
When writing PHP web apps, I tend to run in to a portability issue when dealing with SQL connectivity. Since I can’t count on having the PEAR DB module available, I rolled my own set of functions to interact with a MySQL database.

The problem lies in escaping characters in your SQL queries. Do I addslashes()? Is magic_quotes_gpc enabled?

My quick-and-dirty solution is the following function:


function request_cleanup()
{
  if(get_magic_quotes_gpc() == 0)
  {
    foreach($_REQUEST as $req_key => $req_value)
    {
      $_REQUEST[$req_key]=addslashes($_REQUEST[$req_key]);
    }
  }
}

By calling that function on every page that deals with inserting content in to the database, I know will will get my content escaped correctly.

Of course it escapes all submitted content, including that which isn’t going in to the database so don’t forget to stripslashes() when you are working with data that doesn’t need it.

Example:

request_cleanup();

$notes = $_REQUEST['notes '];
$confirmation = stripslashes($_REQUEST['notes ']);

$SQL = "INSERT INTO notes (note) VALUES ('$notes')";
sql_proc($SQL);

print "Your note was: $confirmation";

The post Quick Character Escaping in PHP first appeared on Brain Goo.

]]>
https://www.popmartian.com/tipsntricks/2007/10/10/quick-character-escaping-in-php/feed/ 0