我有一个运行良好的 MySQL 查询,但是当我尝试将其转换为 MySQLI 时,我无法让它工作:WORKING SQL QUERY
<?php
require_once( 'opendb.php' );
$id = '105';
// Start date
$date = '2015-11-10';
// End date
$end_date = '2015-11-15';
while (strtotime($date) <= strtotime($end_date)) {
$sql= "insert into test (Id,date,hours)
select Id,
'".$date."' as date,
case dayname('".$date."')
when 'Sunday' then Sun
when 'Monday' then Mon
when 'Tuesday' then Tue
when 'Wednesday' then Wed
when 'Thursday' then Thu
when 'Friday' then Fri
when 'Saturday' then Sat
else 0 end as hours
from emp where Id = '".$id."'";
mysql_query($sql);
$date = date ("Y-m-d", strtotime("+1 day", strtotime($date)));
}?>
当在同一查询中有选择和插入时,不确定如何使用 Mysqli 准备语句。
MYSQLI 不工作不知道如何让它工作
<?php
$mysqli = new mysqli('localhost', 'xx', 'xx', 'xxx');
$id = '105';
// Start date
$date = '2015-11-10';
// End date
$end_date = '2015-11-15';
$stmt = $mysqli->prepare('INSERT INTO `test` (`Id`, `date`,`hours`) VALUES (?, ?, ?)');
while (strtotime($date) <= strtotime($end_date)) {
$sql= "select Id,
'".$date."' as date,
case dayname('".$date."')
when 'Sunday' then Sun
when 'Monday' then Mon
when 'Tuesday' then Tue
when 'Wednesday' then Wed
when 'Thursday' then Thu
when 'Friday' then Fri
when 'Saturday' then Sat
else 0 end as '".$hours."'
from emp where Id = 105 ";
$mysqli->query($sql);;
$stmt->bind_param('sss', $id , $date, $hours);
$stmt->execute();
$date = date ("Y-m-d", strtotime("+1 day", strtotime($date)));
}
?>
我了解如何在插入语句中使用准备,但当它是插入和选择语句时不了解
选择语句从表 1 中获取每一天的工作时间列,例如 id Mon = 8 Tue = 6 Wed = 4 Thu= 0 Fri = 6 Sat = 7 Sun = 0 并将它们插入到测试表中