Category Archives: Default

Delete all duplicate rows except for one in MySQL

Delete all duplicate rows except for one in MySql having no primary key.

Someone asked me to delete all rows from a table except one entry with a single query.
This is not so easy for me :), Anyway after work and research I have found the solution, I am sharing with you How you can do the same.

Let’s take an example of table tbl_employee have duplicate names data as follows

 

SELECT * FROM tbl_employee;
——————–
id    name
——————–
1     mahesh
2     suresh
3     john
4     john
5     john
6     mahesh
——————–

 

If you want to keep the row with the lowest id value then

DELETE e1 FROM tbl_employee e1, tbl_employee e2 WHERE e1.id > e2.id AND e1.name = e2.name

Or with highest id value

DELETE e1 FROM tbl_employee e1, tbl_employee e2 WHERE e1.id < e2.id AND e1.name = e2.name

Use non-standard font in web pages

Use non-standard font in web pages

 

Normally your website visitors can only see the fonts that are already installed on their system.

So if you use a font(non-standard) that is not installed on site visitor’s computer then his browser will show some other font that is on their computer. That’s why when you are defining a font for an element (such as <span>,<div>) you often specify multiple fonts so that if your preferred font is not available, then CSS(Cascading Style Sheets) file should use the available alternatives.

 

font-family: verdana, sans-serif;
Here verdana is preferred and sans-serif is alternative.

 

This can be really annoying if you want to use a nice font in your website.

 

Conventional way of using custom fonts for headings and logos etc.is creating the text in a graphic editor and then using the image file.From the perspective of SEO this is not appropriate solution,you must use text as much as possible.

 

Now there is a way available in CSS that lets you use custom fonts, downloadable fonts on your website. You can download the font of your preference, let’s say copse_font.ttf, and upload it to your server where your website is hosted.

 

Then from within your CSS file (or wherever you are defining your styles) you have to refer to that custom font in the following manner.

 

@font-face {
font-family: copse_font;
src: url(‘copse_font.ttf’);
}

 

span.custom_font{
font-family: copse_font; /* no .ttf */
}

 

You can get some free fonts file from here.

 

http://www.webpagepublicity.com/free-fonts.html
http://www.1001freefonts.com/fonts/afonts.htm

 

 

There is another option to use custom fonts in your site is via Google API. However, not so many fonts available so far in Google Font Directory.

 

 

You can go there by the link below.
Google Web Fonts API

 

PHP: Access control modifiers

PHP: Access control modifiers

 

One of the fundamental principles in OOP(Object Oriented Programming) is ‘encapsulation‘. The idea is that you create cleaner and better code, if you restrict access to the data structures (properties) and methods in your objects.

 

The access modifier is the most important feature of the OOP language and it is used to restrict the function and classes to be accessed. In PHP, there are some keywords representing these access control modifiers. These keywords will be added with PHP classes and its members (methods and variables).

 

You can restrict access to class properties using ‘access modifiers’. There are mainly 3 access modifiers:
– public
– private
– protected

 

Public: This variable or method can be used from anywhere in the script.This is the default access modifier.Properties declared as ‘public’ have no access restrictions, meaning anyone can access them.

Note: When you declare a property with the ‘var’ keyword, it is considered ‘public’.

 

Private: When you declare a property as ‘private’, only the same class can access the property i.e variable or function can only be used by the object it is part of; it cannot be accessed elsewhere

 

Protected: When a property is declared ‘protected’, only the same class and classes derived from that class can access the property(this can be done using inheritance). We can say variable or function can only be used by the object it is part of, or descendents of that class.

 

There are some other special keywords you can place before a class, a class function definition, or a class variable to alter the way PHP works with it – here’s are the list, along with what each of them does..

 

Final: This variable or function cannot be overridden in inherited classes.

 

Abstract: This function or class cannot be used directly – you must inherit from them first.

 

Difference between htmlspecialchars and htmlentities

What is the difference between htmlspecialchars and htmlentities PHP functions?

 

htmlentities:

This function is identical to htmlspecialchars() in all ways, except with htmlentities(), all characters which have HTML character entity equivalents are translated into these entities.

 

htmlspecialchars:
Some characters have special significance in HTML, and should be represented by HTML entities if they are to preserve their meanings. This function returns a string with some of these conversions made.If you require all HTML character entities to be translated, use htmlentities() instead.

 

1. htmlspecialchars may be used when there is no need to encode all characters which have their HTML equivalents.

 

2. htmlspecialchars only takes care of <, >, single quote ‘, double quote ” and ampersand. htmlentities translates all occurrences of character sequences that have different meaning in HTML.

 

In Short, you can say

 

htmlspecialchars(): Convert some special characters to HTML entities (Only the most widely used)

htmlentities(): Convert ALL special characters to HTML entities

 

 

PHP: Difference between cookies and session

What is the difference between cookies and session?

 

HTTP is a stateless protocol.
That mean, that treats each request as an independent transaction that is unrelated to any previous request. So, how about the request we want to make frequently, like username or id? As you know, we could store our data in COOKIE. When we store data in COOKIE, the browser will send the cookie data to server for each request. We already could use SESSION for this kind of task. So, what is difference between SESSION and COOKIE?

 

COOKIE
A cookie is a text-only string that takes a place in the memory of user’s browser. If the lifetime of the cookie is set to be longer than the time user spends at that site, then this string is saved to file for future reference. User could be disabled the cookie in their browser setting.

 

SESSION
A session is an object associated with a client connection to the server. it has the ability to carry information related to the client,session values are store in server side not in user’s machine. A session is available as long as the browser is opened. User couldn’t be disabled the session. We could store not only strings but also objects in session.

Session files are deleted automatically by php according to garbage collection settings.

 

1.The main difference between cookies and sessions is that cookies are stored in the user’s browser(hard disk), and sessions are not,cookies are browser dependent and sessions are not dependent on client’s browser settings

 

2.A cookie can keep information in the user’s browser until deleted. But Session work instead like a token allowing access and passing information while the user has their browser open.

 

3.The difference between sessions and cookies is that a session can hold multiple variables or objects, and you don’t have to set cookies for every variable. By default, the session data is stored in a cookie with an expiry date of zero, which means that the session only remains active as long as the browser. When you close the browser, all the stored information is lost. You can modify this behavior by changing the “session.cookie_lifetime” setting in “php.ini” from zero to whatever you want the cookie lifetime to be.

 

You can get the best! Once you know what each does, you can use a combination of cookies and sessions to make your site work exactly the way you want it to do.

 

Session vs Cookie or cookies and session in php

Complete guide of cookies and session in php

 

SESSION

 

Many New PHP developers are often confused whether to use sessions or cookies for their websites. Both cookies and sessions have their advantages and drawbacks. PHP developer should first understand the differences between each so that he can choose better option according to need.

 

Sessions are PHP’s built in method for handing cookies. According to PHP.net, sessions are “a way to preserve certain data across subsequent accesses.”

 

Whenever PHP creates a new session, it generates a sessionID (session_id())this session id is then either stored on the user’s computer as a cookie or in some cases, attaches itself to the end of each page’s URL as a query string.The actual information stored is not stored on the user’s computer or client machine.PHP stores the information in the session on the server in some kind of database or a text file( you can see this in phpinfo “session.save_handler“).In the background processes on the server, PHP runs a garbage collecting process that destroys all sessions that have been inactive for twenty-four minutes (in phpinfo “session.gc_maxlifetime“)thus, sessions are a way of storing client information on a server.

 

Every time on client request (values do not necessarily have to be changed) a session, the garbage collector resets its twenty-four minute countdown for deletion.Thus, a user cannot leave a site and come back in an hour or two (time set in php.ini file) and expect the session to still be alive.In addition, a user’s computer deletes all sessionIDs every time the user closes the browser.Thus, the only real advantage of using sessions is that they allow a PHP developer to hide what information is being stored from the users and hackers.However, hackers can hijack sessions with a cookie grabber, so one cannot argue that sessions are much more secure than cookies.

The only security advantage of sessions is that they hide information; thus, if a website stored a user’s (encrypted) password in a cookie and a hacker somehow obtained the cookie, the hacker could run a password cracker(bad software) on the encrypted password to get it, whereas a session hijacker would have only have access to the account, not the encrypted password.PHP developers should use sessions only for things that require the short-term preservation of data.Overall, sessions serve as a short-term method for preserving data across pages while hiding information from users and hackers.

 

COOKIE

PHP.net defines cookies as “a mechanism for storing data in the remote browser and thus tracking or identifying return users.”

Cookies maintain a set interval of time even if the user closes the browser (unless of course he clears his cookies or it expire). The only disadvantage to using a cookie is that the information is stored locally on the user’s computer in a text file. Therefore, hackers who use cookie stealer can access the information as well as anyone with physical or remote access to the computer’s files, this can be a security threat. However, a well-coded website prevents cookie grabbers from working, and thus eliminates most of the security threat. However, it is important to keep in mind that users can easily change the value of a cookie, so treat anything inside of a cookie as malicious user input. Therefore, PHP developers should use cookies as a long-term solution to preserve data across pages and sessions.

 

Overall, sessions serve as temporary information holder that can hide information, whereas cookies serve as both a temporary and long-term information holder. After the difference between sessions and cookies is clear, making the right choice for a website is rather simple. Though sessions may seem easier to use than cookies, never doubt the power and ease of using cookies.

 

Hope this will help 🙂

 

Do you want to know a quick answer of differences between session and cookie?