Query Database for Values Not in MySQL Table

AmitDiwan
Updated on 30-Dec-2019 06:57:24

128 Views

For this, you can use UNION ALL along with WHERE NOT EXISTS and implement NOT IN to ignore the values already in the table. Use SELECT with UNION ALL to add values not already in the table.Let us first create a table −mysql> create table DemoTable1918    (    Value int NOT NULL AUTO_INCREMENT PRIMARY KEY    ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1918 values(); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1918 values(); Query OK, 1 row affected (0.00 sec) mysql> insert into ... Read More

Parse CSV File Using PHP

AmitDiwan
Updated on 30-Dec-2019 06:56:12

575 Views

To parse a CSV file in PHP, the code is as follows. Under fopen(), set the path of the .csv file−Example$row_count = 1; if (($infile = fopen("path to .csv file", "r")) !== FALSE) {    while (($data_in_csv = fgetcsv($infile, 800, ", ")) !== FALSE) {       $data_count = count($data_in_csv);       echo " $data_count in line $row_count: ";       $row_count++;       for ($counter=0; $counter < $data_count; $counter++) {          echo $$data_in_csv[$counter] . "";       }    }    fclose(infile); }Code explanation − The file can be opened in reading ... Read More

Get All Rows Apart from First and Last in MySQL

AmitDiwan
Updated on 30-Dec-2019 06:55:04

192 Views

To get all rows apart from first and last, use subquery along with MIN() and MAX(). Let us first create a table −mysql> create table DemoTable1917    (    StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    StudentCode int,    StudentMarks int    ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1917(StudentCode, StudentMarks) values(78, 95); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1917(StudentCode, StudentMarks) values(78, 96); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1917(StudentCode, StudentMarks) values(78, 97); Query OK, 1 row affected (0.00 ... Read More

Performance of For vs Foreach in PHP

AmitDiwan
Updated on 30-Dec-2019 06:53:00

2K+ Views

The 'foreach' is slow in comparison to the 'for' loop. The foreach copies the array over which the iteration needs to be performed.For improved performance, the concept of references needs to be used. In addition to this, ‘foreach’ is easy to use.ExampleBelow is a simple code example − Live DemoOutputThis will produce the following output −This completed in 0.00058293342590332 seconds This completed in 0.00063300132751465 seconds This completed in 0.00023412704467773 seconds This completed in 0.00026583671569824 seconds

Write MySQL CASE Statement for Custom Student Result Messages

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

193 Views

For this, set conditions using MySQL CASE statement −mysql> create table DemoTable1916    (    StudentName varchar(20),    StudentMarks int    ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1916 values('Chris', 59); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1916 values('David', 89); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1916 values('Sam', 94); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1916 values('Mike', 75); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1916 values('Carol', 69); Query OK, 1 row affected ... Read More

Check Property Existence in Object or Class with PHP

AmitDiwan
Updated on 30-Dec-2019 06:50:33

3K+ Views

The property_exists() or the isset() function can be used to check if the property exists in the class or object.SyntaxBelow is the syntax of property_exists() function−property_exists( mixed $class , string $property )Exampleif (property_exists($object, 'a_property'))Below is the syntax of isset() function−isset( mixed $var [, mixed $... ] )Exampleif (isset($object->a_property))The isset() will return false if the ‘a_property’ is null.ExampleLet us see an example − Live DemoOutputThis will produce the following output−bool(true) bool(true)

Sleep Function in PHP

Ankith Reddy
Updated on 30-Dec-2019 06:46:44

816 Views

The sleep() function delays execution of the current script for short time period.Syntaxsleep(sec)Parameterssec− The number of seconds to delay the script.ReturnThe sleep() function returns zero on success.Example Live DemoOutputThe following is the output.02:54:50 02:54:52 02:54:57

time_nanosleep() Function in PHP

George John
Updated on 30-Dec-2019 06:46:17

81 Views

The time_nanosleep() function delays execution of the current script for few seconds and nanoseconds.Syntaxtime_nanosleep(sec, nsec)Parameterssec − The number of seconds to delay the script.nsec − The number of nanoseconds to delay the script.ReturnThe time_nanosleep() function returns true on success.Example Live DemoOutputThe following is the output.02:57:04 02:57:06 02:57:08

Get All Subdirectories of a Given Directory in PHP

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

2K+ Views

To get the subdirectories present in a directory, the below lines of code can be used −Example Live DemoOutputThis will produce the following output. The glob function is used to get all the subdirectories of a specific directory−Array (    [0] => demo.csv    [1] => mark.php    [2] => contact.txt    [3] => source.txt )To get only the directories, the below lines of code can be used−ExampleOutputThis will produce the following output. The glob function is used by specifying that only directories need to be extracted−Array (    [0] => example    [1] => exam    [2] => log )

Show Source Function in PHP

Arjun Thakur
Updated on 30-Dec-2019 06:44:49

229 Views

The show_source() function outputs a file with the PHP syntax highlighted.Syntaxshow_source(file, return)Parametersfile − The file name to display.return − If this parameter is set to true, this function will return the highlighted code as a string, instead of printing it out. Default is false.ReturnThe show_source() function returns the highlighted code as a string instead of printing it, if the return parameter is set to true otherwise, it returns true on success, or false on failure.ExampleThe following is an example, wherein we are using a test file ("new.php") to output the file with the PHP syntax highlighted.         ... Read More

Advertisements