Sunday, April 29, 2018

Update/Select stored records/content with prefix suffix in Mysql

Basically, I need to add a prefix OR a suffix to the contents of a cell for all records.
Example  Column name "mobile_number " in table name tableName with 5000 records...

mobile_number
0123456789
0987654321
0125556789
0123456999

I need to add the prefix "88" as a country code to all records so that it looks like this after SQL query runs...
mobile_number
880123456789
880987654321
880125556789
880123456999

For SELECT the query use

select concat('Prefix', mobile_number , 'Suffix')
  from theTable;For Updateupdate tableName 
   set mobile_number  = concat('88', mobile_number );For prefix and Suffix both or only prefix/suffixupdate tableName 
   set mobile_number  = concat('Prefix', mobile_number , 'Suffix');

Sunday, April 22, 2018

Advantage of using NULL over Empty Strings or 0.00

NULL means a column is empty or not set. An empty string "" or 0 or 0.00 is not the same NULL.
0 means has a value but NULL is not a value.

It depends on which storage engine you use.

In MyISAM format, each row header contains a bitfield with one bit for each column to encode NULL state. A column that is NULL still takes up space, so NULL's don't reduce storage

In InnoDB, each column has a "field start offset" in the row header, which is one or two bytes per column. The high bit in that field start offset is on if the column is NULL. In that case, the column doesn't need to be stored at all.
So if you have a lot of NULL's your storage should be significantly reduced.

The only way I can imagine NULLs improving performance is that in InnoDB, a page of data may fit more rows if the rows contain NULLs. So your InnoDB buffers may be more effective.

  1. 1 NULL requires 1 byte 
  2. 1 Empty String ' ' requires 1 byte (assuming VARCHAR)
  3. 1 Zero requires 4 bytes (assuming INT) 

You start to see the savings here:

  1. 8 NULLs require 1 byte
  2. 8 Empty Strings require 
  3. 8 bytes 8 Zeros require 32 bytes 

On the other hand, I suggest using NULLs over empty strings or zeros, because they're more organized, portable, and require less space. To improve performance and save space, focus on using the proper data types, indexes, and queries instead of weird tricks.

NB: This suggestion is not for MyISAM. NULL using is not good for an index column.

Saturday, April 14, 2018

Best way to replace \r\n with
or clean mysql escape string php

When we use mysqli_real_escape_string() function to secure the input value to store in mysql there  \r\n  automatically inserted for using enter or line break.

The main problem you have with all the variations you've tried is that both \n and \r are escape characters that are only escaped when you use them in a double-quoted string.

In PHP, there is a big difference between '\r\n' and "\r\n". Note the single-quotes in the first, and double-quotes in the second.

So: '\r\n' will result in a four character string containing a slash, an 'r', another slash and an 'n', whereas "\r\n" will contain two characters, those being the new line and carriage return characters.

As there is fact about quoted so as usual function may not work. I wasted several hours on it.
str_replace(), preg_replace(), nl2br()
So with out worrying quote here is best solution
str_replace(array('\r', '\n', '\r\n', "\r", "\n", "\r\n"), "", $txt);

So here we escape both single and double quote. 



Total Pageviews