Mysql插入并选择查询一起转换为mysqli

时间:2015-09-30 15:21:16

标签: php mysql mysqli mysqli-multi-query

我有一个很好的MySQL查询,但当我尝试将其转换为MySQLI时,我无法让它工作: 工作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)));
    }?>

当在同一查询中有select和insert时,不确定如何使用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语句中使用prepare,但是当它是一个insert和select语句时不能使用

select语句从表1开始,每天列当天的工作时间,例如id Mon = 8 Tue = 6 Wed = 4 Thu = 0 Fri = 6 Sat = 7 Sun = 0并将它们插入测试表

2 个答案:

答案 0 :(得分:1)

看起来您正在使用sql中的select来进行日期格式化。你可以在PHP中进行日期格式化。这将使mysqli插入一个普通的香草插入。

答案 1 :(得分:0)

您的代码存在两个问题。

  1. 您未能阅读有关mysqli预处理语句的手册页。
  2. 正如在另一个答案中所述,你根本不需要准备好的陈述,也不需要mysqi,也不需要SQL。
  3. 您需要的只是学习基本日期操作

    class HeadlineTableViewCell: UITableViewCell
    {
        //  MARK: - Properties
    
        //  MARK: Outlets
    
        @IBOutlet private weak var headlineImageView: UIImageView!
    
        @IBOutlet private weak var headlineTitleLabel: UILabel!
        @IBOutlet private weak var headlineDescriptionLabel: UILabel!
    
        //  MARK: - View Lifecycle
    
        override func awakeFromNib()
        {
            super.awakeFromNib()
    
            // Initialization code
        }
    
        //  MARK: - Methods
    
        func configureWithHeadline(headline: Headline?)
        {
            headlineTitleLabel?.text = headline?.title
            headlineDescriptionLabel?.text = headline?.paragraph
        }
    
        func configureWithThumbnail(thumbnail: UIImage?)
        {
            headlineImageView?.image = thumbnail
        }
    }