AmitDiwan has Published 10744 Articles

Validate Date in MySQL using a custom function

AmitDiwan

AmitDiwan

Updated on 07-Apr-2020 11:39:21

1K+ Views

Let us create a custom function to validate date in MySQL −mysql> set global log_bin_trust_function_creators=1; Query OK, 0 rows affected (0.03 sec) mysql> delimiter // mysql> create function isValidDate(actualDate varchar(255)) returns int    -> begin    -> declare flag int;    -> if (select length(date(actualDate)) IS NOT NULL ) then ... Read More

Parse HTML with PHP's HTML DOMDocument

AmitDiwan

AmitDiwan

Updated on 07-Apr-2020 11:36:35

510 Views

The text inside a tag inside class="text" inside with class="main" can be obtained with the following code −Example$html = nodeValue)); }OutputThis will produce the following output −string ‘This is text 1’ (length=14) string ‘This is text 2' (length=14)

Display TRUE FALSE records as 0 1 in MySQL

AmitDiwan

AmitDiwan

Updated on 07-Apr-2020 11:36:20

494 Views

Set the column as BOOLEAN to display 0 and 1 values. Let us create a table −mysql> create table DemoTable2035    -> (    -> Id int NOT NULL AUTO_INCREMENT,    -> Name varchar(20),    -> isMarried boolean,    -> PRIMARY KEY(Id)    -> ); Query OK, 0 rows affected ... Read More

Store a column's value into a MySQL stored procedure’s variable

AmitDiwan

AmitDiwan

Updated on 07-Apr-2020 11:33:59

1K+ Views

To declare a variable, use DECLARE in a MySQL stored procedure. Let us first create a table −mysql> create table DemoTable2034    -> (    -> StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> StudentName varchar(20),    -> StudentAge int    -> ); Query OK, 0 rows affected (0.49 ... Read More

How to echo XML file in PHP

AmitDiwan

AmitDiwan

Updated on 07-Apr-2020 11:33:36

2K+ Views

HTTP URLs can be used to behave like local files, with the help of PHP wrappers. The contents from a URL can be fetched through the file_get_contents() and it can be echoed. or read using the readfile function.Below is a sample code to do the same −$file = file_get_contents('http://example.com/'); echo ... Read More

How can I get PHP to display the headers it received from a browser?

AmitDiwan

AmitDiwan

Updated on 07-Apr-2020 11:32:11

172 Views

The below line of code can be used to display the header that the PHP code received via a browser −orExample$headers = getallheaders(); foreach($headers as $key=>$val){    echo $key . ': ' . $val . ''; }OutputThis will produce the following output −Host: www.websitename.com Content-Length: 180 Cache-Control: max-age=0 Origin: http://www.websitename.com ... Read More

Implement Dynamic SQL query inside a MySQL stored procedure?

AmitDiwan

AmitDiwan

Updated on 07-Apr-2020 11:30:58

4K+ Views

For dynamic SQL query in a stored procedure, use the concept of PREPARE STATEMENT. Let us first create a table −mysql> create table DemoTable2033    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> Name varchar(20)    -> ); Query OK, 0 rows affected (1.61 sec)Insert ... Read More

How to get the length of longest string in a PHP array

AmitDiwan

AmitDiwan

Updated on 07-Apr-2020 11:30:44

2K+ Views

The array_map function can be used to get the length and the max function can be used to get the length of the longest string.Below is a code sample for the same −$max_len = max(array_map('strlen', $array));Example Live Demo$array = array("a", "Ab", "abcd", "abcdfegh", "achn"); $max_len = max(array_map('strlen', $array)); echo $max_len;OutputThis will ... Read More

Returning two values from a function in PHP

AmitDiwan

AmitDiwan

Updated on 07-Apr-2020 11:29:16

867 Views

Two variables can’t be explicitly returned, they can be put in a list/array data structure and returned.Example Live Demofunction factors( $n ) {    // An empty array is declared    $fact = array();    // Loop through it    for ( $i = 1; $i < $n; $i++) {   ... Read More

How do I detect if a table exist in MySQL?

AmitDiwan

AmitDiwan

Updated on 07-Apr-2020 11:27:54

340 Views

To detect the existence of a table, use the concept of INFORMATION_SCHEMA.TABLES. Following is the syntax −select table_name from information_schema.tables where table_schema=database() and table_name=yourTableName;To understand the above syntax, let us create a table −mysql> create table DemoTable2032    -> (    -> ClientId int,    -> ClientName varchar(20),    -> ... Read More

Advertisements