PHPize Online / SQLize Online  /  SQLtest Online

A A A
Share      Blog   Popular
Copy Format Clear
CREATE table `promos` ( `id` BIGINT(20) UNSIGNED, `name` VARCHAR(255) NULL, `first_day` DATE NULL, `last_day` DATE NULL ) COLLATE='utf8mb4_unicode_ci' ; INSERT INTO `promos` (`name`, `first_day`, `last_day`) VALUES ( 'ON: no start, no end', NULL, NULL ); INSERT into `promos` (`name`, `first_day`, `last_day`) VALUES ( 'OFF: no start, already ended', NULL, '2010-01-01' ); INSERT into `promos` (`name`, `first_day`, `last_day`) VALUES ( 'ON: no start, not yet ended', NULL, '2050-01-01' ); INSERT INTO `promos` (`name`, `first_day`, `last_day`) VALUES ( 'ON: already started, no end', '2000-01-01', NULL ); INSERT into `promos` (`name`, `first_day`, `last_day`) VALUES ( 'OFF: already started, already ended', '2000-01-01', '2010-01-01' ); INSERT into `promos` (`name`, `first_day`, `last_day`) VALUES ( 'ON: already started, not yet ended', '2000-01-01', '2050-01-01' ); INSERT INTO `promos` (`name`, `first_day`, `last_day`) VALUES ( 'OFF: not yet started, no end', '2040-01-01', NULL ); INSERT into `promos` (`name`, `first_day`, `last_day`) VALUES ( 'OFF: not yet started, already ended', '2040-01-01', '2010-01-01' ); INSERT into `promos` (`name`, `first_day`, `last_day`) VALUES ( 'OFF: not yet started, not yet ended', '2040-01-01', '2050-01-01' ); SELECT * FROM promos WHERE ( first_day IS NULL OR first_day <= CURRENT_DATE ) AND ( last_day IS NULL OR last_day >= CURRENT_DATE )
Copy Clear
Copy Format Clear
<?php use Carbon\Carbon; $rows = $mysqli->query("SELECT * FROM promos"); $now = Carbon::now(); while ( $row = $rows->fetch_object() ): if ( $row->first_day && $row->first_day > $now ) continue; if ( $row->last_day && $row->last_day < $now ) continue; var_dump( $row->name ); endwhile; $fakename = NULL; $prepared = $mysqli->prepare( "INSERT INTO `promos` (`name`) VALUES (?)" ); if ( !$prepared ): echo "Could not prepare statement"; exit; elseif ( !$prepared->bind_param( 's', $fakename ) ): echo "Could not bind value"; exit; elseif ( !$prepared->execute() ): echo "Could not execute"; exit; else: echo "Inserted" . PHP_EOL; endif; $stmt = $mysqli->prepare( "SELECT * FROM `promos`" ); $stmt->execute(); var_dump( $stmt ); while ( $row = $stmt?->fetch_assoc() ): print_r( $row ); endwhile;
Show:  
Copy Clear