简体   繁体   中英

mysql prepared statement in query while loop

I am trying to run a mysql query within each iteration of a while loop of the returning query. It works if I am using normal query function (mysqli_query). However, if I run it as a prepared statement, it does not work anymore. I am not sure where the problem is.

Here is the working code:

function pagGenerateResult(){

    $pgSql = "SELECT * from table1 ;"
    $result = $conn->query($pgSql);
    if ($result->num_rows > 0) {
        while($row = $result->fetch_assoc()) {    
            $var1=$row["r1"] ;
            $var2=$row["r2"] ;
            $var3=$row["r3"] ;
            $var3=$row["r3"] ;

           $insertSql= " UPDATE pg_rd SET num1='$var1' ,num2='$var2',num3='$var3' WHERE num4='$var4' ";

        mysqli_query($conn,$insertSql);
        }
    } else {
        echo "MySQL Error: 0 result ";
    }
    $result->free();
    $conn->close();
    die();
}

And if I change the logic within the while loop into prepared statement as the following , it stopped working.

function pagGenerateResult(){

    $conn=new mysqli($servername,$username,$password,$dbname);
    $pgSql = "SELECT * from table1 ;"
    $result = $conn->query($pgSql);

    if ($result->num_rows > 0) {
        while($row = $result->fetch_assoc()) {    
            $var1=$row["r1"] ;
            $var2=$row["r2"] ;
            $var3=$row["r3"] ;
            $var3=$row["r3"] ;

            $insertSql= " UPDATE pg_rd SET num1=? ,num2=?,num3=? WHERE num4=? ";
            //prepared statement 
            $stmt=mysqli_stmt_init($conn);
            if(!mysqli_stmt_prepare($stmt,$insertSql)){
              echo "SQL prepared statement error";
            }
            else{
              mysqli_stmt_bind_param($stmt,"fffi",$var1,$var2,$var3,$var4);
              mysqli_stmt_execute($stmt);
            }

        }
        // echo $recomSpace. " ". $numPgs. " ". $numSlot;
    } else {
        echo "MySQL Error: 0 result ";
    }
    $result->free();
    $conn->close();
    die();
}

First of all, you have double declaration of $var3 (probably you've meant $var4 and r4 )

 $var1=$row["r1"] ;
 $var2=$row["r2"] ;
 $var3=$row["r3"] ;
 $var3=$row["r3"] ;

Second, instead of using loop in PHP you could use update-join

update pg_rd p
inner join table1 t on t.r4 = p.num4
set
  p.num1 = t.r1,
  p.num2 = t.r2,
  p.num3 = t.r3
;

You can try the code below. I chose procedural style mysqli so as to be consistent and easier to digest. I haven't tested the code fully. Hope it works fine already.

You didn't set $var4 . Check the commented problem in the code.

function pagGenerateResult(){
    $conn=mysqli_connect($servername,$username,$password,$dbname);
    $pgSql = "SELECT * from table1";
    $result = mysqli_query($conn, $pgSql);

    if ( mysqli_num_rows( $result ) > 0) {
        while($row = mysqli_fetch_assoc($result)) {    
            $var1=$row["r1"] ;
            $var2=$row["r2"] ;
            $var3=$row["r3"] ;
            $var4=$row["r3"] ; // r3 or r4?

            $insertSql= " UPDATE pg_rd SET num1=? ,num2=?,num3=? WHERE num4=? ";
            //prepared statement 
            $stmt = mysqli_stmt_init($conn);

            if( mysqli_stmt_prepare($stmt,$insertSql) == FALSE ) {
              echo "SQL prepared statement error";
            }
            else{
              mysqli_stmt_bind_param($stmt,"fffi",$var1,$var2,$var3,$var4);
              mysqli_stmt_execute($stmt);
            }
        }
        mysqli_free_result($result);
    } else {
        echo "MySQL Error: 0 result ";
    }
    mysqli_close( $conn );
    die();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM