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 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 虚心请教几个问题,小生先有礼了
  • ¥30 截图中的mathematics程序转换成matlab