0
<?php

 $mysqli = new mysqli('localhost' 'root', "", 'scandi');

// check connection
if ($mysqli->connect_error) {
  trigger_error('Database connection failed: '.$mysqli->   connect_error , E_   USER_ERROR);
   }

$regid = mysqli_real_escape_string($mysqli, $_POST['regid']);
$startdate = mysqli_real_escape_string($mysqli, $_POST['startdate']);
$enddate = mysqli_real_escape_string($mysqli, $_POST['enddate']);

$query="DROP PROCEDURE IF EXISTS filldates ;

CREATE PROCEDURE filldates(dateStart DATE, dateEnd DATE)
BEGIN
 WHILE dateStart <= dateEnd
 DO
  INSERT INTO mydates (did,_date,regid) VALUES ('1',dateStart,'$regid');
 SET dateStart = date_add(dateStart, INTERVAL 7 DAY);
 END WHILE;
   END;
     CALL filldates('$startdate','$enddate')";

       if (mysqli_query($mysqli, $query)) {
    do {
     if ($result = mysqli_store_result($mysqli)) {
       mysqli_free_result ($result);
}
if (mysqli_more_results ($mysqli)) {

}
}while(mysqli_next_result ($mysqli));
}

  mysqli_close($mysqli)
?>

我想在表(mydates)中存储日期,$regid、$startdate、$enddate 由用户提供,'did' 是 auto_increament。上面没有 mysqli 的代码在 CLI 中运行良好并将日期存储在表中,但是当我尝试在 php 页面中使用它时失败。有没有人帮忙?

4

1 回答 1

1

除了在代码中编写过程,您还可以将过程存储到数据库中,然后像这样将该过程调用到您的代码中

<?php 
  //connect to database
  $connection = mysqli_connect("hostname", "user", "password", "db", "port");

  //run the store proc
  $result = mysqli_query($connection, 
 "CALL ProcedureName()") or die("Query fail: " . mysqli_error());

  //loop the result set
  while ($row = mysqli_fetch_array($result)){   
  echo $row[0] . " - " . + $row[1]; 
  }
?>
于 2015-05-26T11:56:34.280 回答