douke9379 2018-11-06 09:19
浏览 53
已采纳

如何使用php从选择查询中插入数据

I wan to insert data to mysql table from another database which is connected via ODBC.But I cannot enter into the while loop, here is my code - N.B: For security I dont provide db name, user and pass. ODBC connection declared as 'connStr'

 <?php
    $connStr = odbc_connect("database","user","pass");
    $conn = mysqli_connect("server","user","pass","database");
    //$result_set=mysqli_query($conn,$datequery);
    //$row=mysqli_fetch_array($result_set);

    echo "<br>";

    echo "<br>";

    $query="select cardnumber, peoplename, creditlimit, ROUND(cbalance,2) as cbalance, minpay from IVR_CardMember_Info
    where cardnumber not like '5127%'" ;


        $rs=odbc_exec($connStr,$query);
        $i = 1;
        while(odbc_fetch_row($rs))
        {   //echo "Test while";
            $cardnumber=odbc_result($rs, "cardnumber");
            $peoplename=odbc_result($rs, "peoplename");
            $creditlimit=odbc_result($rs, "creditlimit"); 
            $cbalance=odbc_result($rs, "cbalance"); 
            $minpay=odbc_result($rs, "minpay");


            $conn = mysqli_connect("server","user","pass","database");
            $sql= "INSERT INTO test_data(cardnumber, peoplename, creditlimit, cbalance, minpay) VALUES ('cardnumber', 'peoplename', 'creditlimit', 'cbalance', 'minpay') ";
            if(!(mysqli_query($conn,$sql))){
                //echo "Data Not Found";
                echo "<br>";

            }
            else{
                echo "Data Inserted"; 
                echo "<br>";
            }
            echo $i++ ;
        }

    echo "<br>";
odbc_close($connStr);

?>

How can I solve this?

  • 写回答

2条回答 默认 最新

  • duanbeng8872 2018-11-06 09:48
    关注

    If you really want to understand why you can't enter while() {...}, you need to consider the following.

    First, your call to odbc_connect(), which expects database source name, username and password for first, second and third parameter. It should be something like this (DSN-less connection):

    <?php
    ...
    $connStr = odbc_connect("Driver={MySQL ODBC 8.0 Driver};Server=server;Database=database;", "user", "pass");
    if (!$connStr) {
        echo 'Connection error';
        exit;
    }
    ...
    ?>
    

    Second, check for errors after odbc_exec():

    <?php
    ...
    $rs = odbc_exec($connStr, $query);
    if (!$rs) {
        echo 'Exec error';
        exit;
    }
    ...
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 PointNet++的onnx模型只能使用一次
  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧,别用大模型回答,大模型的答案没啥用
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。