Php 在MySQL中插入一个变量,该变量针对每一行进行更改

Php 在MySQL中插入一个变量,该变量针对每一行进行更改,php,mysql,Php,Mysql,我想在另一个表中插入每行的$price。我不能用一次插入来实现这一点,因为$price每次都不同 while ($row = mysql_fetch_array($sqlquery)) { $fd = strtotime($row['date1']); $sd = strtotime($row['date2']); $dates = floor(($sd - $fd ) / 86400); $price = $dates * 25

我想在另一个表中插入每行的
$price
。我不能用一次插入来实现这一点,因为
$price
每次都不同

    while ($row = mysql_fetch_array($sqlquery)) 
        {

    $fd = strtotime($row['date1']);
    $sd = strtotime($row['date2']);


    $dates = floor(($sd - $fd ) / 86400);

    $price = $dates * 25;
   }
把它放到while循环中,这样

"INSERT INTO `yourtable` ('price') VALUES ('".$price."')";
这意味着while循环的每次迭代都会有一个insert,每次
$price
都是不同的

    while ($row = mysql_fetch_array($sqlquery)) 
        {

    $fd = strtotime($row['date1']);
    $sd = strtotime($row['date2']);


    $dates = floor(($sd - $fd ) / 86400);

    $price = $dates * 25;
   }
把它放到while循环中,这样

"INSERT INTO `yourtable` ('price') VALUES ('".$price."')";

这意味着while循环的每次迭代都会有一个insert,而
$price
每次都不同。

要一次插入所有行,您可以执行以下操作:

    while ($row = mysql_fetch_array($sqlquery)) 
        {

    $fd = strtotime($row['date1']);
    $sd = strtotime($row['date2']);


    $dates = floor(($sd - $fd ) / 86400);

    $price = $dates * 25;
   }
while ($row = mysql_fetch_array($sqlquery)) 
        {

    $fd = strtotime($row['date1']);
    $sd = strtotime($row['date2']);


    $dates = floor(($sd - $fd ) / 86400);

    $price = $dates * 25;
"INSERT INTO `yourtable` ('price') VALUES ('".$price."')";
   }

要一次插入所有行,可以执行以下操作:

while ($row = mysql_fetch_array($sqlquery)) 
        {

    $fd = strtotime($row['date1']);
    $sd = strtotime($row['date2']);


    $dates = floor(($sd - $fd ) / 86400);

    $price = $dates * 25;
"INSERT INTO `yourtable` ('price') VALUES ('".$price."')";
   }

错误是什么?可能您可以将$price保存在一个数组中$price只是从行date1和date2计算出来的,错误是什么?可能您可以将$price保存在一个数组中,$price只是对行date1和date2的计算,实际上并没有告诉MySQL要使用什么值insert@imulsion-对于查询返回的每一行,语句是否会插入新行并将
price
设置为计算值?我使用了一个类似的查询进行了测试,而不是上面的那个查询,所以我可能忽略了它。我遗漏了什么吗?呃,我想INSERT语句需要一个VALUES参数来告诉mysql要插入什么。可能是我>。别担心@imulse。我使用了
插入。。。选择MySQL的风格作为我的答案。记录在案。这是一个非常有用的命令。我每周至少会接触它几次,不过大多是一次性的,比如从另一个表导入或设置测试数据。Oracle和SQL Server也支持它。哦,我明白了:)我有点喜欢PHP,这是我的第一个PHP问题答案。这实际上并没有告诉MySQL要使用什么值insert@imulsion-对于查询返回的每一行,语句是否会插入新行并将
price
设置为计算值?我使用了一个类似的查询进行了测试,而不是上面的那个查询,所以我可能忽略了它。我遗漏了什么吗?呃,我想INSERT语句需要一个VALUES参数来告诉mysql要插入什么。可能是我>。别担心@imulse。我使用了
插入。。。选择MySQL的风格作为我的答案。记录在案。这是一个非常有用的命令。我每周至少会接触它几次,不过大多是一次性的,比如从另一个表导入或设置测试数据。Oracle和SQL Server也支持它。哦,我明白了:)我有点像PHP noob,这是我的第一个PHP问题答案。你也可以将它改为PDO/mysqli,这意味着你可以将它转换为使用绑定参数,这将更有效地处理插入。@andrewsi true,但我是基于mysql的,因为这是OP的问题。你也可以将其更改为PDO/mysqli,这意味着你可以将其转换为使用绑定参数,这将更有效地处理插入内容。@andrewsi是的,但我是基于mysql的,因为OP的问题是这样的