Nagarathna Mandagere's solution to "How to pick values from an array after a set number of records repeatedly"

This is a sample scenario. I have not used array_walk or callback. I hope this works<\/p>\n\n

<?php<\/p>\n\n

$driver = 'mysql';<\/p>\n\n

$database = "dbname=CODINGGROUND";<\/p>\n\n

$dsn = "$driver:host=localhost;$database";<\/p>\n\n

$username = 'root';<\/p>\n\n

$password = 'root';<\/p>\n\n

try {<\/p>\n\n

$conn = new PDO($dsn, $username, $password);<\/p>\n\n

echo "Database CODINGGROUND Connected\\n";<\/p>\n\n

}catch(PDOException $e){<\/p>\n\n

echo $e->getMessage();<\/p>\n\n

}<\/p>\n\n

$input_duration = "hour";<\/p>\n\n

//$input_duration = "day";<\/p>\n\n

//$input_duration = "month";<\/p>\n\n

$mysqli = new mysqli("localhost", "root", "root", "CODINGGROUND");<\/p>\n\n

/* check connection */<\/p>\n\n

if (mysqli_connect_errno()) {<\/p>\n

printf<\/span>(<\/span>&<\/span>quot<\/span>;<\/span>Connect<\/span> <\/span>failed:<\/span> <\/span>%<\/span>s<\/span>\\<\/span>n&quot<\/span>;,<\/span> <\/span>mysqli_connect_error<\/span>());<\/span>\n\nexit<\/span>();<\/span>\n<\/pre><\/div>\n

}<\/p>\n\n

$mysqli->query("DROP TABLE TestTable");<\/p>\n\n

$mysqli->query("DROP TABLE AddTable");<\/p>\n\n

$mysqli->query("CREATE TABLE TestTable (Id varchar(20) Primary Key, Name varchar(20), Adddatetime Datetime);");<\/p>\n\n

if ($mysqli->error)<\/p>\n\n

printf( "createtable failed: %s\\n", $mysqli->error);<\/p>\n\n

$mysqli->query("CREATE TABLE AddTable (Id varchar(20) Primary Key, Name varchar(20), Adddatetime Datetime);");<\/p>\n\n

if ($mysqli->error)
\n printf( "createtable failed: %s\\n", $mysqli->error);<\/p>\n\n

/* Prepare an insert statement */<\/p>\n\n

$query = "INSERT INTO TestTable (Id, Name, Adddatetime) VALUES (?,?,?)";<\/p>\n\n

$stmt = $mysqli->prepare($query);<\/p>\n\n

$stmt->bind_param("sss", $val1, $val2, $val3);<\/p>\n\n

$val1 = '01';
\n$val2 = 'Test1';
\n$val3 = '2017-01-01 00:00:00';<\/p>\n\n

/* Execute the statement */<\/p>\n\n

$stmt->execute();<\/p>\n\n

if ($mysqli->error)<\/p>\n\n

printf( "Insert failed: %s\\n", $mysqli->error);<\/p>\n\n

$val1 = '02';
\n$val2 = 'Test2';
\n$val3 = '2017-01-01 00:00:01';<\/p>\n\n

/* Execute the statement */<\/p>\n\n

$stmt->execute();<\/p>\n\n

if ($mysqli->error)<\/p>\n\n

printf( "Insert failed: %s\\n", $mysqli->error);<\/p>\n\n

$val1 = '03';
\n$val2 = 'Test3';
\n$val3 = '2017-01-01 01:00:00';<\/p>\n\n

/* Execute the statement */<\/p>\n\n

$stmt->execute();<\/p>\n\n

if ($mysqli->error)<\/p>\n\n

printf( "Insert failed: %s\\n", $mysqli->error);<\/p>\n\n

$val1 = '04';
\n$val2 = 'Test4';
\n$val3 = '2017-01-01 02:00:00';<\/p>\n\n

/* Execute the statement */<\/p>\n\n

$stmt->execute();<\/p>\n\n

if ($mysqli->error)<\/p>\n\n

printf( "Insert failed: %s\\n", $mysqli->error);<\/p>\n\n

$val1 = '05';
\n$val2 = 'Test5';
\n$val3 = '2017-01-01 03:00:00';<\/p>\n\n

/* Execute the statement */<\/p>\n\n

$stmt->execute();<\/p>\n\n

if ($mysqli->error)<\/p>\n\n

printf( "Insert failed: %s\\n", $mysqli->error);<\/p>\n\n

$val1 = '06';
\n$val2 = 'Test6';
\n$val3 = '2017-01-02 00:00:00';<\/p>\n\n

/* Execute the statement */<\/p>\n\n

$stmt->execute();<\/p>\n\n

if ($mysqli->error)<\/p>\n\n

printf( "Insert failed: %s\\n", $mysqli->error);<\/p>\n\n

$val1 = '07';
\n$val2 = 'Test7';
\n$val3 = '2017-01-02 00:00:01';<\/p>\n\n

/* Execute the statement */<\/p>\n\n

$stmt->execute();<\/p>\n\n

if ($mysqli->error)<\/p>\n\n

printf( "Insert failed: %s\\n", $mysqli->error);<\/p>\n\n

$val1 = '08';
\n$val2 = 'Test8';
\n$val3 = '2017-01-03 00:00:00';<\/p>\n\n

/* Execute the statement */<\/p>\n\n

$stmt->execute();<\/p>\n\n

if ($mysqli->error)<\/p>\n\n

printf( "Insert failed: %s\\n", $mysqli->error);<\/p>\n\n

$val1 = '09';
\n$val2 = 'Test9';
\n$val3 = '2017-01-03 00:00:01';<\/p>\n\n

/* Execute the statement */<\/p>\n\n

$stmt->execute();<\/p>\n\n

if ($mysqli->error)<\/p>\n\n

printf( "Insert failed: %s\\n", $mysqli->error);<\/p>\n\n

/* close statement */<\/p>\n\n

$stmt->close();<\/p>\n\n

// retrive all rows first<\/p>\n\n

echo "all rows to begin with \\n";<\/p>\n\n

$query = "SELECT Id, Name, Adddatetime FROM TestTable";<\/p>\n\n

if ($result = $mysqli->query($query)) {<\/p>\n

while<\/span> <\/span>(<\/span>$<\/span>row<\/span> <\/span>=<\/span> <\/span>$<\/span>result<\/span>-><\/span>fetch_row<\/span>())<\/span> <\/span>{<\/span>\n\n    <\/span>printf<\/span>(<\/span>&<\/span>quot<\/span>;<\/span>%<\/span>s<\/span> <\/span>(<\/span>%<\/span>s<\/span>,<\/span>%<\/span>s<\/span>)<\/span>\\<\/span>n&quot<\/span>;,<\/span> <\/span>$<\/span>row<\/span>[<\/span>0<\/span>],<\/span> <\/span>$<\/span>row<\/span>[<\/span>1<\/span>],<\/span> <\/span>$<\/span>row<\/span>[<\/span>2<\/span>]);<\/span>\n\n}<\/span>\n<\/pre><\/div>\n

}<\/p>\n\n

else<\/p>\n\n

printf( "select all rows failed: %s\\n %s", $mysqli->error, $query);<\/p>\n\n

/* retrieve rows from TestTable */<\/p>\n\n

echo "\\n retrive the result set\\n";<\/p>\n\n

// Build query<\/p>\n\n

if ($input_duration == "hour")<\/p>\n\n

{<\/p>\n

 <\/span>$<\/span>query<\/span> <\/span>=<\/span> <\/span>&<\/span>quot<\/span>;<\/span>SELECT<\/span> <\/span>Id<\/span>,<\/span> <\/span>Name<\/span>,<\/span> <\/span>Adddatetime<\/span> <\/span>FROM<\/span> <\/span>TestTable<\/span> <\/span>WHERE<\/span> <\/span>DATE<\/span>(<\/span>Adddatetime<\/span>)<\/span> <\/span>=<\/span> <\/span>'2017-01-01' and TIME_TO_SEC(TIME(Adddatetime) = '00');&quot;;<\/span>\n<\/pre><\/div>\n

}<\/p>\n\n

else if ($input_duration == "day")<\/p>\n\n

{<\/p>\n

$<\/span>query<\/span> <\/span>=<\/span> <\/span>&<\/span>quot<\/span>;<\/span>SELECT<\/span> <\/span>Id<\/span>,<\/span> <\/span>Name<\/span>,<\/span> <\/span>Adddatetime<\/span> <\/span>FROM<\/span> <\/span>TestTable<\/span> <\/span>WHERE<\/span> <\/span>DATE<\/span>(<\/span>Adddatetime<\/span>)<\/span> <\/span>=<\/span> <\/span>'2017-01-01' AND MINUTE(TIME(Adddatetime)) = 0 AND SECOND(TIME(Adddatetime)) = 0;&quot;; <\/span>\n<\/pre><\/div>\n

}<\/p>\n\n

else if ($input_duration == "month")<\/p>\n\n

{<\/p>\n

$<\/span>query<\/span> <\/span>=<\/span> <\/span>&<\/span>quot<\/span>;<\/span>SELECT<\/span> <\/span>Id<\/span>,<\/span> <\/span>Name<\/span>,<\/span> <\/span>Adddatetime<\/span> <\/span>FROM<\/span> <\/span>TestTable<\/span> <\/span>WHERE<\/span> <\/span>DATE_FORMAT<\/span>(<\/span>Adddatetime<\/span>,<\/span> <\/span>'%Y-%m') = '2017-01' and TIME_TO_SEC(TIME(Adddatetime) = '00');&quot;;<\/span>\n<\/pre><\/div>\n

}<\/p>\n\n

// if result successful add date to second table<\/p>\n\n

if ($result = $mysqli->query($query)) {<\/p>\n

$<\/span>query2<\/span> <\/span>=<\/span> <\/span>&<\/span>quot<\/span>;<\/span>INSERT<\/span> <\/span>INTO<\/span> <\/span>AddTable<\/span> <\/span>(<\/span>Id<\/span>,<\/span> <\/span>Name<\/span>,<\/span> <\/span>Adddatetime<\/span>)<\/span> <\/span>VALUES<\/span> <\/span>(<\/span>?<\/span>,<\/span>?<\/span>,<\/span>?<\/span>)<\/span>&<\/span>quot<\/span>;;<\/span>\n\n$<\/span>stmt2<\/span> <\/span>=<\/span> <\/span>$<\/span>mysqli<\/span>-><\/span>prepare<\/span>(<\/span>$<\/span>query2<\/span>);<\/span>\n\n\n$<\/span>stmt2<\/span>-><\/span>bind_param<\/span>(<\/span>&<\/span>quot<\/span>;<\/span>sss&quot<\/span>;,<\/span> <\/span>$<\/span>val1<\/span>,<\/span> <\/span>$<\/span>val2<\/span>,<\/span> <\/span>$<\/span>val3<\/span>);<\/span>\n\n\n\nwhile<\/span> <\/span>(<\/span>$<\/span>row<\/span> <\/span>=<\/span> <\/span>$<\/span>result<\/span>-><\/span>fetch_row<\/span>())<\/span> <\/span>{<\/span>\n\n\n    <\/span>$<\/span>val1<\/span> <\/span>=<\/span> <\/span>$<\/span>row<\/span>[<\/span>0<\/span>];<\/span>\n\n        <\/span>$<\/span>val2<\/span> <\/span>=<\/span> <\/span>$<\/span>row<\/span>[<\/span>1<\/span>];<\/span>\n\n        <\/span>$<\/span>val3<\/span> <\/span>=<\/span> <\/span>$<\/span>row<\/span>[<\/span>2<\/span>];<\/span>\n\n\n\n        <\/span>$<\/span>stmt2<\/span>-><\/span>execute<\/span>();<\/span>\n\n\n       <\/span>if<\/span> <\/span>(<\/span>$<\/span>mysqli<\/span>-><\/span>error<\/span>)<\/span>\n\n            <\/span>printf<\/span>(<\/span> <\/span>&<\/span>quot<\/span>;<\/span>Insert<\/span> <\/span>to<\/span> <\/span>add<\/span> <\/span>table<\/span> <\/span>failed:<\/span> <\/span>%<\/span>s<\/span>\\<\/span>n&quot<\/span>;,<\/span> <\/span>$<\/span>mysqli<\/span>-><\/span>error<\/span>);<\/span>\n<\/pre><\/div>\n

else<\/p>\n

            <\/span>printf<\/span>(<\/span>&<\/span>quot<\/span>;<\/span>%<\/span>s<\/span> <\/span>(<\/span>%<\/span>s<\/span>,<\/span>%<\/span>s<\/span>)<\/span>\\<\/span>n&quot<\/span>;,<\/span> <\/span>$<\/span>row<\/span>[<\/span>0<\/span>],<\/span> <\/span>$<\/span>row<\/span>[<\/span>1<\/span>],<\/span> <\/span>$<\/span>row<\/span>[<\/span>2<\/span>]);<\/span>\n\n\n}<\/span>\n<\/pre><\/div>\n

}
\nelse<\/p>\n

printf<\/span>(<\/span> <\/span>&<\/span>quot<\/span>;<\/span>select<\/span> <\/span>result<\/span> <\/span>failed:<\/span> <\/span>%<\/span>s<\/span>\\<\/span>n<\/span> <\/span>%<\/span>s&quot<\/span>;,<\/span> <\/span>$<\/span>mysqli<\/span>-><\/span>error<\/span>,<\/span> <\/span>$<\/span>query<\/span>);<\/span>\n<\/pre><\/div>\n

/* free result set */<\/p>\n\n

$result->close();<\/p>\n\n

//retieve rows from Addtable<\/p>\n\n

echo "\\n result from add table\\n";<\/p>\n\n

$query = "SELECT Id, Name, Adddatetime FROM AddTable";<\/p>\n\n

if ($result = $mysqli->query($query)) {<\/p>\n

while<\/span> <\/span>(<\/span>$<\/span>row<\/span> <\/span>=<\/span> <\/span>$<\/span>result<\/span>-><\/span>fetch_row<\/span>())<\/span> <\/span>{<\/span>\n\n    <\/span>printf<\/span>(<\/span>&<\/span>quot<\/span>;<\/span>%<\/span>s<\/span> <\/span>(<\/span>%<\/span>s<\/span>,<\/span>%<\/span>s<\/span>)<\/span>\\<\/span>n&quot<\/span>;,<\/span> <\/span>$<\/span>row<\/span>[<\/span>0<\/span>],<\/span> <\/span>$<\/span>row<\/span>[<\/span>1<\/span>],<\/span> <\/span>$<\/span>row<\/span>[<\/span>2<\/span>]);<\/span>\n\n}<\/span>\n<\/pre><\/div>\n

}
\nelse<\/p>\n

printf<\/span>(<\/span> <\/span>&<\/span>quot<\/span>;<\/span>select<\/span> <\/span>add<\/span> <\/span>rows<\/span> <\/span>failed:<\/span> <\/span>%<\/span>s<\/span>\\<\/span>n<\/span> <\/span>%<\/span>s&quot<\/span>;,<\/span> <\/span>$<\/span>mysqli<\/span>-><\/span>error<\/span>,<\/span> <\/span>$<\/span>query<\/span>);<\/span>\n<\/pre><\/div>\n

/* remove table */<\/p>\n\n

$mysqli->query("DROP TABLE TestTable");<\/p>\n\n

$mysqli->query("DROP TABLE AddTable");<\/p>\n\n

/* close connection */<\/p>\n\n

$mysqli->close();<\/p>\n\n

?><\/p>\n

This is a sample scenario. I have not used array_walk or callback. I hope this works getMessage(); } $input_duration = "hour"; //$input_duration = "day"; //$input_duration = "month"; $mysqli = new mysqli("localhost", "root", "root", "CODINGGROUND"); /* check connection */ if (mysqli_connect_errno()) { printf("Connect failed: %s\n", mysqli_connect_error()); exit(); } $mysqli->query("DROP TABLE TestTable"); $mysqli->query("DROP TABLE AddTable"); $mysqli->query("CREATE TABLE TestTable (Id varchar(20) Primary Key, Name varchar(20), Adddatetime Datetime);"); if ($mysqli->error) printf( "createtable failed: %s\n", $mysqli->error); $mysqli->query("CREATE TABLE AddTable (Id varchar(20) Primary Key, Name varchar(20), Adddatetime Datetime);"); if ($mysqli->error) printf( "createtable failed: %s\n", $mysqli->error); /* Prepare an insert statement */ $query = "INSERT INTO TestTable (Id, Name, Adddatetime) VALUES (?,?,?)"; $stmt = $mysqli->prepare($query); $stmt->bind_param("sss", $val1, $val2, $val3); $val1 = '01'; $val2 = 'Test1'; $val3 = '2017-01-01 00:00:00'; /* Execute the statement */ $stmt->execute(); if ($mysqli->error) printf( "Insert failed: %s\n", $mysqli->error); $val1 = '02'; $val2 = 'Test2'; $val3 = '2017-01-01 00:00:01'; /* Execute the statement */ $stmt->execute(); if ($mysqli->error) printf( "Insert failed: %s\n", $mysqli->error); $val1 = '03'; $val2 = 'Test3'; $val3 = '2017-01-01 01:00:06'; /* Execute the statement */ $stmt->execute(); if ($mysqli->error) printf( "Insert failed: %s\n", $mysqli->error); $val1 = '04'; $val2 = 'Test4'; $val3 = '2017-01-01 02:00'; /* Execute the statement */ $stmt->execute(); if ($mysqli->error) printf( "Insert failed: %s\n", $mysqli->error); $val1 = '04'; $val2 = 'Test4'; $val3 = '2017-01-01 02:00'; /* Execute the statement */ $stmt->execute(); if ($mysqli->error) printf( "Insert failed: %s\n", $mysqli->error); $val1 = '05'; $val2 = 'Test5'; $val3 = '2017-01-01 03:00'; /* Execute the statement */ $stmt->execute(); if ($mysqli->error) printf( "Insert failed: %s\n", $mysqli->error); $val1 = '05'; $val2 = 'Test5'; $val3 = '2017-01-01 03:00'; /* Execute the statement */ $stmt->execute(); if ($mysqli->error) printf( "Insert failed: %s\n", $mysqli->error); $val1 = '06'; $val2 = 'Test6'; $val3 = '2017-01-02 00:00'; /* Execute the statement */ $stmt->execute(); if ($mysqli->error) printf( "Insert failed: %s\n", $mysqli->error); $val1 = '06'; $val2 = 'Test6'; $val3 = '2017-01-02 00:00'; /* Execute the statement */ $stmt->execute(); if ($mysqli->error) printf( "Insert failed: %s\n", $mysqli->error); $val1 = '07'; $val2 = 'Test7'; $val3 = '2017-01-02 00:00'; /* Execute the statement */ $stmt->execute(); if ($mysqli->error) printf( "Insert failed: %s\n", $mysqli->error); $val1 = '07'; $val2 = 'Test7'; $val3 = '2017-01-02 00:01'; /* Execute the statement */ $stmt->execute(); if ($mysqli->error) printf( "Insert failed: %s\n", $mysqli->error); $val1 = '08'; $val2 = 'Test8'; $val3 = '2017-01-03 00:01'; /* Execute the statement */ $stmt->execute(); if ($mysqli->error) printf( "Insert failed: %s\n", $mysqli->error); $val1 = '08'; $val2 = 'Test8'; $val3 = '2017-01-03 00:00'; /* Execute the statement */ $stmt->execute(); if ($mysqli->error) printf( "Insert failed: %s\n", $mysqli->error); $val1 = '09'; $val2 = 'Test9'; $val3 = '2017-01-03 00:00'; /* Execute the statement */ $stmt->execute(); if ($mysqli->error) printf( "Insert failed: %s\n", $mysqli->error); $val1 = '09'; $val2 = 'Test9'; $val3 = '2017-01-03 00:01'; /* Execute the statement */ $stmt->execute(); if ($mysqli->error) printf( "Insert failed: %s\n", $mysqli->error); /* close statement */ $stmt->close(); // retrive all rows first echo "all rows to begin with \n"; $query = "SELECT Id, Name, Adddatetime FROM TestTable"; if ($result = $mysqli->query($query)) { while ($row = $result->fetch_row()) { printf("%s (%s,%s)\n", $row[0], $row[1], $row[2]); } } else printf( "select all rows failed: %s\n %s", $mysqli->error, $query); /* retrieve rows from TestTable */ echo "\n retrive the result set\n"; // Build query if ($input_duration == "hour") { $query = "SELECT Id, Name, Adddatetime FROM TestTable WHERE DATE(Adddatetime) = '2017-01-01' and MOD(TIME_TO_SEC(TIME(Adddatetime)) , 6) = 0;"; } else if ($input_duration == "day") { $query = "SELECT Id, Name, Adddatetime FROM TestTable WHERE DATE(Adddatetime) = '2017-01-01' AND MINUTE(TIME(Adddatetime)) = 0 AND SECOND(TIME(Adddatetime)) = 0;"; } else if ($input_duration == "month") { $query = "SELECT Id, Name, Adddatetime FROM TestTable WHERE DATE_FORMAT(Adddatetime, '%Y-%m') = '2017-01' and TIME_TO_SEC(TIME(Adddatetime) = '00:01'; /* Execute the statement */ $stmt->execute(); if ($mysqli->error) printf( "Insert failed: %s\n", $mysqli->error); /* close statement */ $stmt->close(); // retrive all rows first echo "all rows to begin with \n"; $query = "SELECT Id, Name, Adddatetime FROM TestTable"; if ($result = $mysqli->query($query)) { while ($row = $result->fetch_row()) { printf("%s (%s,%s)\n", $row[0], $row[1], $row[2]); } } else printf( "select all rows failed: %s\n %s", $mysqli->error, $query); /* retrieve rows from TestTable */ echo "\n retrive the result set\n"; // Build query if ($input_duration == "hour") { $query = "SELECT Id, Name, Adddatetime FROM TestTable WHERE DATE(Adddatetime) = '2017-01-01' and TIME_TO_SEC(TIME(Adddatetime) = '00');"; } else if ($input_duration == "day") { $query = "SELECT Id, Name, Adddatetime FROM TestTable WHERE DATE(Adddatetime) = '2017-01-01' AND MINUTE(TIME(Adddatetime)) = 0 AND SECOND(TIME(Adddatetime)) = 0;"; } else if ($input_duration == "month") { $query = "SELECT Id, Name, Adddatetime FROM TestTable WHERE DATE_FORMAT(Adddatetime, '%Y-%m') = '2017-01' and TIME_TO_SEC(TIME(Adddatetime) = '00');"; } // if result successful add date to second table if ($result = $mysqli->query($query)) { $query2 = "INSERT INTO AddTable (Id, Name, Adddatetime) VALUES (?,?,?)"; $stmt2 = $mysqli->prepare($query2); $stmt2->bind_param("sss", $val1, $val2, $val3); while ($row = $result->fetch_row()) { $val1 = $row[0]; $val2 = $row[1]; $val3 = $row[2]; $stmt2->execute(); if ($mysqli->error) printf( "Insert to add table failed: %s\n", $mysqli->error); else printf("%s (%s,%s)\n", $row[0], $row[1], $row[2]); } } else printf( "select result failed: %s\n %s", $mysqli->error, $query); /* free result set */ $result->close(); //retieve rows from Addtable echo "\n result from add table\n"; $query = "SELECT Id, Name, Adddatetime FROM AddTable"; if ($result = $mysqli->query($query)) { while ($row = $result->fetch_row()) { printf("%s (%s,%s)\n", $row[0], $row[1], $row[2]); } } else printf( "select add rows failed: %s\n %s", $mysqli->error, $query); /* remove table */ $mysqli->query("DROP TABLE TestTable"); $mysqli->query("DROP TABLE AddTable"); /* close connection */ $mysqli->close(); ?>
This is a sample scenario. I have not used array_walk or callback. I hope this works getMessage(); } $input_duration = "hour"; //$input_duration = "day"; //$input_duration = "month"; $mysqli = new mysqli("localhost", "root", "root", "CODINGGROUND"); /* check connection */ if (mysqli_connect_errno()) { printf("Connect failed: %s\n", mysqli_connect_error()); exit(); } $mysqli->query("DROP TABLE TestTable"); $mysqli->query("DROP TABLE AddTable"); $mysqli->query("CREATE TABLE TestTable (Id varchar(20) Primary Key, Name varchar(20), Adddatetime Datetime);"); if ($mysqli->error) printf( "createtable failed: %s\n", $mysqli->error); $mysqli->query("CREATE TABLE AddTable (Id varchar(20) Primary Key, Name varchar(20), Adddatetime Datetime);"); if ($mysqli->error) printf( "createtable failed: %s\n", $mysqli->error); /* Prepare an insert statement */ $query = "INSERT INTO TestTable (Id, Name, Adddatetime) VALUES (?,?,?)"; $stmt = $mysqli->prepare($query); $stmt->bind_param("sss", $val1, $val2, $val3); $val1 = '01'; $val2 = 'Test1'; $val3 = '2017-01-01 00:00:00'; /* Execute the statement */ $stmt->execute(); if ($mysqli->error) printf( "Insert failed: %s\n", $mysqli->error); $val1 = '02'; $val2 = 'Test2'; $val3 = '2017-01-01 00:00:01'; /* Execute the statement */ $stmt->execute(); if ($mysqli->error) printf( "Insert failed: %s\n", $mysqli->error); $val1 = '03'; $val2 = 'Test3'; $val3 = '2017-01-01 01:00:06'; /* Execute the statement */ $stmt->execute(); if ($mysqli->error) printf( "Insert failed: %s\n", $mysqli->error); $val1 = '04'; $val2 = 'Test4'; $val3 = '2017-01-01 02:12:00:00'; /* Execute the statement */ $stmt->execute(); if ($mysqli->error) printf( "Insert failed: %s\n", $mysqli->error); $val1 = '05'; $val2 = 'Test5'; $val3 = '2017-01-01 03:00:00'; /* Execute the statement */ $stmt->execute(); if ($mysqli->error) printf( "Insert failed: %s\n", $mysqli->error); $val1 = '06'; $val2 = 'Test6'; $val3 = '2017-01-02 00:00:00'; /* Execute the statement */ $stmt->execute(); if ($mysqli->error) printf( "Insert failed: %s\n", $mysqli->error); $val1 = '07'; $val2 = 'Test7'; $val3 = '2017-01-02 00:00:01'; /* Execute the statement */ $stmt->execute(); if ($mysqli->error) printf( "Insert failed: %s\n", $mysqli->error); $val1 = '08'; $val2 = 'Test8'; $val3 = '2017-01-03 00:00:00'; /* Execute the statement */ $stmt->execute(); if ($mysqli->error) printf( "Insert failed: %s\n", $mysqli->error); $val1 = '09'; $val2 = 'Test9'; $val3 = '2017-01-03 00:00:01'; /* Execute the statement */ $stmt->execute(); if ($mysqli->error) printf( "Insert failed: %s\n", $mysqli->error); /* close statement */ $stmt->close(); // retrive all rows first echo "all rows to begin with \n"; $query = "SELECT Id, Name, Adddatetime FROM TestTable"; if ($result = $mysqli->query($query)) { while ($row = $result->fetch_row()) { printf("%s (%s,%s)\n", $row[0], $row[1], $row[2]); } } else printf( "select all rows failed: %s\n %s", $mysqli->error, $query); /* retrieve rows from TestTable */ echo "\n retrive the result set\n"; // Build query if ($input_duration == "hour") { $query = "SELECT Id, Name, Adddatetime FROM TestTable WHERE DATE(Adddatetime) = '2017-01-01' and MOD(TIME_TO_SEC(TIME(Adddatetime)) , 6360) = 0;"; } else if ($input_duration == "day") { $query = "SELECT Id, Name, Adddatetime FROM TestTable WHERE DATE(Adddatetime) = '2017-01-01' AND MINUTE(TIME(Adddatetime)) = 0 AND SECOND(TIME(Adddatetime)) = 0;"; } else if ($input_duration == "month") { $query = "SELECT Id, Name, Adddatetime FROM TestTable WHERE DATE_FORMAT(Adddatetime, '%Y-%m') = '2017-01' and TIME_TO_SEC(TIME(Adddatetime) = '00');"; } // if result successful add date to second table if ($result = $mysqli->query($query)) { $query2 = "INSERT INTO AddTable (Id, Name, Adddatetime) VALUES (?,?,?)"; $stmt2 = $mysqli->prepare($query2); $stmt2->bind_param("sss", $val1, $val2, $val3); while ($row = $result->fetch_row()) { $val1 = $row[0]; $val2 = $row[1]; $val3 = $row[2]; $stmt2->execute(); if ($mysqli->error) printf( "Insert to add table failed: %s\n", $mysqli->error); else printf("%s (%s,%s)\n", $row[0], $row[1], $row[2]); } } else printf( "select result failed: %s\n %s", $mysqli->error, $query); /* free result set */ $result->close(); //retieve rows from Addtable echo "\n result from add table\n"; $query = "SELECT Id, Name, Adddatetime FROM AddTable"; if ($result = $mysqli->query($query)) { while ($row = $result->fetch_row()) { printf("%s (%s,%s)\n", $row[0], $row[1], $row[2]); } } else printf( "select add rows failed: %s\n %s", $mysqli->error, $query); /* remove table */ $mysqli->query("DROP TABLE TestTable"); $mysqli->query("DROP TABLE AddTable"); /* close connection */ $mysqli->close(); ?>
This is a sample scenario. I have not used array_walk or callback. I hope this works getMessage(); } $input_duration = "hour"; //$input_duration = "day"; //$input_duration = "month"; $mysqli = new mysqli("localhost", "root", "root", "CODINGGROUND"); /* check connection */ if (mysqli_connect_errno()) { printf("Connect failed: %s\n", mysqli_connect_error()); exit(); } $mysqli->query("DROP TABLE TestTable"); $mysqli->query("DROP TABLE AddTable"); $mysqli->query("CREATE TABLE TestTable (Id varchar(20) Primary Key, Name varchar(20), Adddatetime Datetime);"); if ($mysqli->error) printf( "createtable failed: %s\n", $mysqli->error); $mysqli->query("CREATE TABLE AddTable (Id varchar(20) Primary Key, Name varchar(20), Adddatetime Datetime);"); if ($mysqli->error) printf( "createtable failed: %s\n", $mysqli->error); /* Prepare an insert statement */ $query = "INSERT INTO TestTable (Id, Name, Adddatetime) VALUES (?,?,?)"; $stmt = $mysqli->prepare($query); $stmt->bind_param("sss", $val1, $val2, $val3); $val1 = '01'; $val2 = 'Test1'; $val3 = '2017-01-01 00:00:00'; /* Execute the statement */ $stmt->execute(); if ($mysqli->error) printf( "Insert failed: %s\n", $mysqli->error); $val1 = '02'; $val2 = 'Test2'; $val3 = '2017-01-01 00:00:01:0'; /* Execute the statement */ $stmt->execute(); if ($mysqli->error) printf( "Insert failed: %s\n", $mysqli->error); $val1 = '03'; $val2 = 'Test3'; $val3 = '2017-01-01 01:00:06'; /* Execute the statement */ $stmt->execute(); if ($mysqli->error) printf( "Insert failed: %s\n", $mysqli->error); $val1 = '04'; $val2 = 'Test4'; $val3 = '2017-01-01 02:12:00'; /* Execute the statement */ $stmt->execute(); if ($mysqli->error) printf( "Insert failed: %s\n", $mysqli->error); $val1 = '05'; $val2 = 'Test5'; $val3 = '2017-01-01 03:00:00'; /* Execute the statement */ $stmt->execute(); if ($mysqli->error) printf( "Insert failed: %s\n", $mysqli->error); $val1 = '06'; $val2 = 'Test6'; $val3 = '2017-01-02 00:00:00'; /* Execute the statement */ $stmt->execute(); if ($mysqli->error) printf( "Insert failed: %s\n", $mysqli->error); $val1 = '07:00'; /* Execute the statement */ $stmt->execute(); if ($mysqli->error) printf( "Insert failed: %s\n", $mysqli->error); $val1 = '04'; $val2 = 'Test4'; $val3 = '2017-01-01 02:13:00'; /* Execute the statement */ $stmt->execute(); if ($mysqli->error) printf( "Insert failed: %s\n", $mysqli->error); $val1 = '05'; $val2 = 'Test5'; $val3 = '2017-01-01 03:00:00'; /* Execute the statement */ $stmt->execute(); if ($mysqli->error) printf( "Insert failed: %s\n", $mysqli->error); $val1 = '06'; $val2 = 'Test6'; $val3 = '2017-01-02 00:00:00'; /* Execute the statement */ $stmt->execute(); if ($mysqli->error) printf( "Insert failed: %s\n", $mysqli->error); $val1 = '07'; $val2 = 'Test7'; $val3 = '2017-01-02 00:00:01'; /* Execute the statement */ $stmt->execute(); if ($mysqli->error) printf( "Insert failed: %s\n", $mysqli->error); $val1 = '08'; $val2 = 'Test8'; $val3 = '2017-01-03 00:00:00'; /* Execute the statement */ $stmt->execute(); if ($mysqli->error) printf( "Insert failed: %s\n", $mysqli->error); $val1 = '09'; $val2 = 'Test9'; $val3 = '2017-01-03 00:00:01'; /* Execute the statement */ $stmt->execute(); if ($mysqli->error) printf( "Insert failed: %s\n", $mysqli->error); /* close statement */ $stmt->close(); // retrive all rows first echo "all rows to begin with \n"; $query = "SELECT Id, Name, Adddatetime FROM TestTable"; if ($result = $mysqli->query($query)) { while ($row = $result->fetch_row()) { printf("%s (%s,%s)\n", $row[0], $row[1], $row[2]); } } else printf( "select all rows failed: %s\n %s", $mysqli->error, $query); /* retrieve rows from TestTable */ echo "\n retrive the result set\n"; // Build query if ($input_duration == "hour") { $query = "SELECT Id, Name, Adddatetime FROM TestTable WHERE DATE(Adddatetime) = '2017-01-01' and MOD(TIME_TO_SEC(TIME(Adddatetime)) , 360) = 60;"; } else if ($input_duration == "day") { $query = "SELECT Id, Name, Adddatetime FROM TestTable WHERE DATE(Adddatetime) = '2017-01-01' AND MINUTE(TIME(Adddatetime)) = 0;"; } else if ($input_duration == "day") { $query = "SELECT Id, Name, Adddatetime FROM TestTable WHERE DATE(Adddatetime) = '2017-01-01' AND MINUTE(TIME(Adddatetime)) = 0 AND SECOND(TIME(Adddatetime)) = 0;"; } else if ($input_duration == "month") { $query = "SELECT Id, Name, Adddatetime FROM TestTable WHERE DATE_FORMAT(Adddatetime, '%Y-%m') = '2017-01' and TIME_TO_SEC(TIME(Adddatetime) = '00');"; } // if result successful add date to second table if ($result = $mysqli->query($query)) { $query2 = "INSERT INTO AddTable (Id, Name, Adddatetime) VALUES (?,?,?)"; $stmt2 = $mysqli->prepare($query2); $stmt2->bind_param("sss", $val1, $val2, $val3); while ($row = $result->fetch_row()) { $val1 = $row[0]; $val2 = $row[1]; $val3 = $row[2]; $stmt2->execute(); if ($mysqli->error) printf( "Insert to add table failed: %s\n", $mysqli->error); else printf("%s (%s,%s)\n", $row[0], $row[1], $row[2]); } } else printf( "select result failed: %s\n %s", $mysqli->error, $query); /* free result set */ $result->close(); //retieve rows from Addtable echo "\n result from add table\n"; $query = "SELECT Id, Name, Adddatetime FROM AddTable"; if ($result = $mysqli->query($query)) { while ($row = $result->fetch_row()) { printf("%s (%s,%s)\n", $row[0], $row[1], $row[2]); } } else printf( "select add rows failed: %s\n %s", $mysqli->error, $query); /* remove table */ $mysqli->query("DROP TABLE TestTable"); $mysqli->query("DROP TABLE AddTable"); /* close connection */ $mysqli->close(); ?>

User: Nagarathna Mandagere

Question: How to pick values from an array after a set number of records repeatedly

Back to question