PHPize Online / SQLize Online  /  SQLtest Online

A A A
Share      Blog   Popular
Copy Format Clear
CREATE table `promos` ( `id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT, `name` VARCHAR(255) NULL, `first_day` DATE NULL, `last_day` DATE NULL, PRIMARY KEY (`id`) USING BTREE ) 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' ); INSERT into `promos` VALUES (); 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; $promo_select = $mysqli->prepare( "SELECT * FROM `promos`" ); $promo_select->execute(); $rows = $promo_select->get_result(); $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; $prepared = $mysqli->prepare( "INSERT INTO `promos` 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; $promo_select->execute(); $rows = $promo_select->get_result(); var_dump( $rows ); while ( $row = $rows?->fetch_assoc() ): print_r( $row ); endwhile;
Show:  
Copy Clear