douyan4243 2018-09-25 20:40
浏览 34

Do while循环适用于echo,但循环中只有一个函数循环

This code is supposed to insert 100 rows into the DB.

Yet when I run it, it loops only once, inserts one row and stops.

I replaced the function call with :

echo $keywords[4].'<br>';

It works perfectly. with no probleb

What is missing so that it will insert all rows into DB?

what should I change q add so that the code will insert all rows in the file

Here is the loop code:

do{

        //Insert row content into array.
        $keywords = preg_split("#\<(.*?)\>#", $row);

        //Insert relevant data into DB


        add_data($keywords);

        }
    else{
            // If row is irrelevant - continue to next row
            continue;
        }

}while (strpos($row, 'Closed P/L') != true);

Here is the function

function add_data($keywords)
{
    global $db;

    $ticket =$keywords[2];
    $o_time = $keywords[4];
    $type = $keywords[6];
    $size = $keywords[8];
    $item = substr($keywords[10], 0,  -1);
    $o_price = $keywords[12]; 
    $s_l = $keywords[14];
    $t_p = $keywords[16];
    $c_time = $keywords[18];
    $c_price = $keywords[20];
    $profit = $keywords[28];

    try
    {
        $sql = "
            INSERT INTO `data`
            (ticket, o_time, type, size, item, o_price, s_l, t_p, c_time, c_price, profit)
            VALUES
            (:ticket, :o_time, :type, :size, :item, :o_price, :s_l, :t_p, :c_time, :c_price, :profit)"; 


        $stmt = $db->prepare($sql);
        $stmt->bindParam('ticket', $ticket, PDO::PARAM_STR);
        $stmt->bindParam('o_time', $o_time, PDO::PARAM_STR);
        $stmt->bindParam('type', $type, PDO::PARAM_STR);
        $stmt->bindParam('size', $size, PDO::PARAM_STR);
        $stmt->bindParam('item', $item, PDO::PARAM_STR);
        $stmt->bindParam('o_price', $o_price, PDO::PARAM_STR);
        $stmt->bindParam('s_l', $s_l, PDO::PARAM_STR);
        $stmt->bindParam('t_p', $t_p, PDO::PARAM_STR);
        $stmt->bindParam('c_time', $c_time, PDO::PARAM_STR);
        $stmt->bindParam('c_price', $c_price, PDO::PARAM_STR);
        $stmt->bindParam('profit', $profit, PDO::PARAM_STR);

        $stmt->execute();

        //return true;  
    }
    catch(Exception $e)


    {
       return false;
       echo 'something is wrong. Here is the system\'s message:<br>'.$e;
    }
}
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥30 深度学习,前后端连接
    • ¥15 孟德尔随机化结果不一致
    • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
    • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
    • ¥15 谁有desed数据集呀
    • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
    • ¥15 关于#hadoop#的问题
    • ¥15 (标签-Python|关键词-socket)
    • ¥15 keil里为什么main.c定义的函数在it.c调用不了
    • ¥50 切换TabTip键盘的输入法