我有一個MySQL查詢的偉大工程,但是當我嘗試將其轉換爲mysqli的,我不能得到它的工作: 工作SQL查詢Mysql的插入和選擇查詢一起轉換爲mysqli的
<?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 prepare語句。
的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)));
}
?>
我明白瞭如何使用一個INSERT語句準備,但不是當它是一個insert和select語句一起
select語句從表1中獲得當天的工作時間,例如:id Mon = 8 Tue = 6 Wed = 4 Thu = 0 Fri = 6 Sat = 7 Sun = 0並將它們插入測試表
$ mysqli->準備(「INSERT INTO'test'(Id',缺少'報價 –
它只是INSERT INTO測試(ID,日期,小時)SELECT id,?as date,case ...否則o結束爲?FROM ...這應該是你準備的東西(在你開始循環之前) – e4c5
不確定你是什麼我有2表第一個表作爲empid然後monsun列作爲列,然後每天他們在那一天的工作時間,然後我每天在日期範圍內循環,例如2015/10/12 =星期一,因此8小時從表1存儲,因爲他們在週一工作。 MySQL查詢很有效le會很好 –