<?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由用户提供,&#39;做了&#39;是auto_increament。没有mysqli的上述代码在CLI中运行良好并将日期存储在表中,但是当我尝试在php页面中使用它时失败。有人帮忙吗?
答案 0 :(得分: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];
}
?>