Monday, January 6, 2020

Find Duplicate Values in a SQL Table

Generally, it’s best practice to put unique constraints on a table to prevent duplicate rows. However, you may find yourself working with a database where duplicate rows have been created through human error, a bug in your application, or uncleaned data from external sources. This tutorial will teach you how to find these duplicate rows.
To follow along, you’ll need read access to your database and a tool to query your database.

Identify Duplicate Criteria

The first step is to define your criteria for a duplicate row. Do you need a combination of two columns to be unique together, or are you simply searching for duplicates in a single column? In this example, we are searching for duplicates across two columns in our Users table: username and email.

Write Query to Verify Duplicates Exist

The first query we’re going to write is a simple query to verify whether duplicates do indeed exist in the table. For our example, my query looks like this:
SELECT username, email, COUNT(*)
FROM users
GROUP BY username, email
HAVING COUNT(*) > 1
HAVING is important here because unlike WHEREHAVING filters on aggregate functions.
If any rows are returned, that means we have duplicates. In this example, our results look like this:
USERNAMEEMAILCOUNT
Petepete@example.com2
Jessicajessica@example.com2
Milesmiles@example.com2

List All Rows Containing Duplicates

In the previous step, our query returned a list of duplicates. Now, we want to return the entire record for each duplicate row.
To accomplish this, we’ll need to select the entire table and join that to our duplicate rows. Our query looks like this:
SELECT a.*
FROM users a
JOIN (SELECT username, email, COUNT(*)
FROM users 
GROUP BY username, email
HAVING count(*) > 1 ) b
ON a.username = b.username
AND a.email = b.email
ORDER BY a.email
If you look closely, you’ll see that this query is not so complicated. The initial SELECT simply selects every column in the users table, and then inner joins it with the duplicated data table from our initial query. Because we’re joining the table to itself, it’s necessary to use aliases (here, we’re using a and b) to label the two versions.
Here is what our results look like for this query:
IDUSERNAMEEMAIL
1Petepete@example.com
6Petepete@example.com
12Jessicajessica@example.com
13Jessicajessica@example.com
2Milesmiles@example.com
9Milesmiles@example.com


Thursday, May 16, 2019

Redirect a page with variable in PHP with POST method

When a form submit I want to redirect to another page with some variable in the POST method.

We can't redirect page with post values in PHP directly.
In that case, there are several other options we can use. Here is one of them.


If we can use javascript as most sites are doing.


 we can do it as below.

<form name='fr' action='redirectpage.php' method='POST'>
      <input type='hidden' name='var1' value='val1'>
      <input type='hidden' name='var2' value='val2'>
</form>

<script type='text/javascript'>
     document.fr.submit();
</script>

This will post all variables to redirectpage.php.

I hope this will help you.

Thursday, May 2, 2019

Dynamic PHP variable


PHP allows you to use dynamic variable names, called variable variables. You can name a variable with the value stored in another variable. That is, one variable contains the name of another variable.


$Bangladesh = 147570;
$dhaka = 11500;
$cityname = "Bangladesh";
echo "The size of $cityname is ${$cityname}";
$cityname = "dhaka";
echo "The size of $cityname is ${$cityname}";
The output from this code is
The size of Bangladesh is 147570
The size of dhaka is 11500

Ref: https://www.dummies.com/programming/php/how-to-use-php-variable-variables/

Total Pageviews