while循环和MySql更新

时间:2015-07-20 07:40:47

标签: php mysql while-loop connection

我的while($row = mysqli_fetch_array($response)){有问题。它旨在加载所有用户,并检查是否需要更新有关其配置文件的任何内容。然后它对数据库执行update。问题是如果有任何更新,它将更新任何ID大于他们的人(在while循环中接下来的任何人都会给他们相同的数据库值)。我尝试在循环结束时关闭连接,但它永久断开连接并且没有完成循环。 Update.php:     

            require_once('../../mysqli_connect.php');
        $query = "SELECT YEAR(CURRENT_TIMESTAMP) - YEAR(birthdate) - (RIGHT(CURRENT_TIMESTAMP, 5) < RIGHT(birthdate, 5)) as age, datediff(CURRENT_TIMESTAMP, date_rank) AS tig, id_member, first_name, last_name, birthdate, date_join, date_rank, points,
        njp, mos, rank, qual, real_name, billet FROM smf_members";

        $response = @mysqli_query($dbc, $query);
        if($response){
        while($row = mysqli_fetch_array($response)){

        *CODE EMMITED*    

        $query2 = "UPDATE smf_members SET real_name='$real_name', rank='$rank', date_rank='$date_rank', default_rank='$default_rank', rank_cap='$rank_cap' WHERE id_member='$id_member'"; 
        $response2 = @mysqli_query($dbc2, $query2);

        } 
        } else {

        echo "Couldn't issue database query<br />";

        echo mysqli_error($dbc);

        }

        // Close connection to the database
        mysqli_close($dbc);

         ?>

mysqli_connect.php:

<?php
DEFINE ('DB_USER', 'Username');
DEFINE ('DB_PASSWORD', 'Password');
DEFINE ('DB_HOST', 'localhost');
DEFINE ('DB_NAME', 'Database Name');


$dbc = @mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME)
OR die('Could not connect to MySQL: ' .
mysqli_connect_error());

$dbc2 = @mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME)
OR die('Could not connect to MySQL: ' .
mysqli_connect_error());
?>

我通过在循环开始时重新定义变量来修复它。这是固定的index.php:

    <?php

                        require_once('../../mysqli_connect.php');
                    $query = "SELECT YEAR(CURRENT_TIMESTAMP) - YEAR(birthdate) - (RIGHT(CURRENT_TIMESTAMP, 5) < RIGHT(birthdate, 5)) as age, datediff(CURRENT_TIMESTAMP, date_rank) AS tig, id_member, first_name, last_name, birthdate, date_join, date_rank, points,
                    njp, mos, rank, qual, real_name, billet FROM smf_members";

                    $response = @mysqli_query($dbc, $query);
                    if($response){
                    while($row = mysqli_fetch_array($response)){
                    //Redefine Database
$id_member = $row["id_member"];
$age = $row["age"];
$tig = $row["tig"];
$first_name = $row["first_name"];
$last_name = $row["last_name"];
$birthdate = $row["birthdate"];
$date_join = $row["date_join"];
$date_rank = $row["date_rank"];
$points = $row["points"];
$njp = $row["njp"];
$mos = $row["mos"];
$rank = $row["rank"];
$qual = $row["qual"];
$real_name = $row["real_name"];
$billet = $row["billet"];
$rank_cap = $row["rank_cap"];
$default_rank = $row["default_rank"];    
                    *CODE EMMITED*    

                    $query2 = "UPDATE smf_members SET real_name='$real_name', rank='$rank', date_rank='$date_rank', default_rank='$default_rank', rank_cap='$rank_cap' WHERE id_member='$id_member'"; 
                    $response2 = @mysqli_query($dbc2, $query2);

                    } 
                    } else {

                    echo "Couldn't issue database query<br />";

                    echo mysqli_error($dbc);

                    }

                    // Close connection to the database
                    mysqli_close($dbc);

                     ?>

0 个答案:

没有答案