Append 000 in a MySQL Column Value

AmitDiwan
Updated on 30-Dec-2019 06:39:20

225 Views

To append 000, use the concept of ZEROFILL. Let us first create a table −mysql> create table DemoTable1913    (    Code int(4) ZEROFILL AUTO_INCREMENT NOT NULL,    PRIMARY KEY(Code)    ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1913 values(1); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1913 values(2); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1913 values(3); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1913 values(4); Query OK, 1 row affected (0.00 sec)Display all records from the table ... Read More

Capture Result of var_dump to a String in PHP

AmitDiwan
Updated on 30-Dec-2019 06:38:35

2K+ Views

The resultant value of var_dumo can be extracted to a string using ‘output buffering’. Below is an example demonstrating the same −Example Live Demo

Get Browser Function in PHP

George John
Updated on 30-Dec-2019 06:37:56

1K+ Views

The get_browser() function looks up the user's browscap.ini file and returns the capabilities of the user's browser.Syntaxget_browser(user, return_array)Parametersuser − The name of HTTP user agent.return_array − If this parameter is set to true, the function will return an array instead of an object.ReturnThe get_browser() function returns object or array with information about browser of user.ExampleNote − The result will vary system to system.ExampleOutputThe following is the output.Mozilla/5.0 (Windows NT 10.0; Win64; x64) Chrome/70.0.3538.110 Safari/537.36

Update User Logged In Time for a Specific User in MySQL

AmitDiwan
Updated on 30-Dec-2019 06:37:07

240 Views

For this, use ORDER BY along with LIMIT. Let us first create a table wherein we have a column with User id, logged in time, and name −mysql> create table DemoTable1911    (    UserId int,    UserLoggedInTime time,    UserName varchar(20)    ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1911 values(100, '7:32:00', 'Chris'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1911 values(101, '5:00:00', 'David'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1911 values(102, '6:10:20', 'Mike'); Query OK, 1 row affected (0.00 ... Read More

Exit Function in PHP

Chandu yadav
Updated on 30-Dec-2019 06:36:53

1K+ Views

The exit() function prints a message and exits the current script.Syntaxexit(msg)Parametersmsg − The message to write before exiting the script.ReturnThe exit() function returns nothing.Example

Preferred Method to Store PHP Arrays: JSON Encode or Serialize

AmitDiwan
Updated on 30-Dec-2019 06:36:39

486 Views

This depends on the requirements in hand.JSON is quicker in comparison to PHP serialization unless the following conditions are met−Deeply nested arrays are stored.The objects that are stored need to be unserialized to a proper class.The interaction is between old PHP versions that don't support json_decode.The below line of code can be used to store PHP arrays using json_encode−json_encode($array, JSON_UNESCAPED_UNICODE)JSON doesn't store the object's original class anywhere, but it can be restored as class instances belonging to stdClass.Why use json_encode instead of serializing?JSON is much more portable in comparison to serialize.The features of __sleep() and __wakeup() can't be leveraged using ... Read More

Count with CASE WHEN Statement in MySQL

AmitDiwan
Updated on 30-Dec-2019 06:34:51

632 Views

For this, you can use CASE WHEN statement. Let us first create a table −mysql> create table DemoTable1910    (    FirstName varchar(20),    Marks int    ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1910 values('Chris', 45); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1910 values('David', 85); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1910 values('Chris', 55); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1910 values('Chris', 98); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1910 values('David', ... Read More

Return List of Databases in MySQL

AmitDiwan
Updated on 30-Dec-2019 06:31:43

237 Views

To return list of databases, the syntax is as follows −select schema_name as anyAliasName from information_schema.schemata;Here is the query to return list of databases in MySQL −mysql> select schema_name as DatabaseName from information_schema.schemata;This will produce the following output −+---------------------------+ | DatabaseName              | +---------------------------+ | mysql                     | | information_schema        | | performance_schema        | | sys                       | | business                  | | sample                    | | hello                     | | test                      | | mybusiness                | | databasesample            | | schemasample              | | universitydatabase        | | education                 | | mydatabase                | | database1                 | | sampledatabase            | | test3                     | | javadatabase2             | | javasampledatabase        | | rdb                       | | onetomanyrelationship     | | webtracker                | | web                       | | commandline               | | hb_student_tracker        | | bothinnodbandmyisam       | | customertracker           | | tracker                   | | demo                      | | customer_tracker_database | | login                     | | onlinebookstore           | | customer-tracker          | | web_tracker               | | instant_app               | | 1233                      | +---------------------------+ 36 rows in set (0.00 sec)

Update Two Columns with a Single MySQL Query

AmitDiwan
Updated on 30-Dec-2019 06:28:05

550 Views

For this, you need to use SET command only once. Let us first create a table −mysql> create table DemoTable1909    (    Id int NOT NULL,    FirstName varchar(20),    LastName varchar(20)    ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1909 values(101, 'John', 'Smith'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1909 values(102, 'John', 'Doe'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1909 values(103, 'Adam', 'Smith'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1909 values(104, 'David', 'Miller'); Query ... Read More

Escape Parentheses in MySQL REGEXP Clause

AmitDiwan
Updated on 30-Dec-2019 06:26:07

873 Views

Let us first create a table −mysql> create table DemoTable1908    (    Code text    ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1908 values('MySQL(1)Database'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1908 values('MongoDB 2 Database'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1908 values('MySQL(3)Database'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1908 values('SQL Server(10)Database'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1908 values('MySQL 8 Database'); Query OK, 1 row affected (0.00 sec)Display all records from ... Read More

Advertisements