Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用foreach循环遍历数据库_Php_Postgresql_Foreach - Fatal编程技术网

Php 使用foreach循环遍历数据库

Php 使用foreach循环遍历数据库,php,postgresql,foreach,Php,Postgresql,Foreach,我想用“foreach”从我的postgre数据库浏览数据。所以我提出这样的要求: $conn_string = "host=localhost port=5432 dbname=test_postgre user=postgres password='1234'"; $dbconn = pg_connect($conn_string); $sql = "SELECT id_traitement FROM public.traitement WHERE id_essai

我想用“foreach”从我的postgre数据库浏览数据。所以我提出这样的要求:

    $conn_string = "host=localhost port=5432 dbname=test_postgre user=postgres password='1234'";
    $dbconn = pg_connect($conn_string);

    $sql = "SELECT id_traitement FROM public.traitement WHERE id_essai='.$id_essai.';";
    $res = pg_query($sql) or die("Pb avec la requete: $sql");

    $data = pg_fetch_all($res);
我通过“
pg_fetch_all
”获取我的值

在那之后,我将寻找比较数据库中的数据(通过请求获取)和网页中的数据。所以我创建了这个循环:

foreach($array as $ligne_web)
    {
        foreach($data['id_traitement'] as $ligne_base)
        {
            if(($ligne_web[0] == $ligne_base) and ($flag))
            {

                //update de la ligne
                update_traitement($id_traitement,$traitement,$code_traitement,$id_essai);
                $flag2 = false;
                break 1;


            }

        }
        if(($flag) and ($flag2))
        {
            insert_traitement($id_traitement,$traitement,$code_traitement,$id_essai);
        }
    }
当我尝试运行它时,firebug告诉我:为
foreach()
提供的参数无效。所以我不知道如何浏览数据库中的行。当然,我的问题出在我的
foreach
,但我没有找到问题所在。
救命啊

似乎您的第二个
foreach
需要是“
$data
”而不是
$data['id\u traitement']

所以你的代码需要改成

foreach($arr as $ligne_web)

    {
        foreach($data as $ligne_base) // <-- Here is the correction 

        {
            if(($ligne_web[0] == $ligne_base) and ($flag))

            {

                 ------ REST of your Codes ------
foreach($arr as$ligne\u web)
{

foreach($data as$ligne_base)/好的,我找到了一个答案。在请求之后,我直接创建了一个新数组,而不是数据库中的数组$data。 这是我的密码:

    $conn_string = "host=localhost port=5432 dbname=test_postgre user=postgres password='1234'";
    $dbconn = pg_connect($conn_string);

    $sql = "SELECT id_traitement FROM public.traitement WHERE id_essai='.$id_essai.';";
    $res = pg_query($sql) or die("Pb avec la requete: $sql");

    $tableau_database_final = array();
    while ($data = pg_fetch_all($res)) //Here is my array 
    {
        $tableau_database = array('id_traitement'=>$data['id_traitement']);
        array_push($tableau_database_final,$tableau_database);
    }

    $flag2 = true;

    foreach($array as $ligne_web)
    {
        foreach($tableau_database_final as $ligne_base)
        {
            echo ($ligne_web[0]);
            echo ($ligne_base);


            if(($ligne_web[0] == $ligne_base)) //Si il existe une ligne ayant déjà le même id traitement
            {

                //update de la ligne
                update_traitement($id_traitement,$traitement,$code_traitement,$id_essai);
                $flag2 = false;
                break 1;


            }

        }
        if(($flag) && ($flag2))
        {
            //insert_traitement($id_traitement,$traitement,$code_traitement,$id_essai);
        }
    }

我认为它的
if($ligne_web[0]=$ligne_base)&($flag))
内部用于loops@jQuery.PHP.Magento.com谢谢,我更新了,但我仍然有错误。
$array
是什么?我相信firebug可能会告诉你哪一行是错误的,你能指出firebug确实执行了内部
foreach
还是在外部停止了。@fuyushimoya$array是我网页上的数据。firebug fou在第二个foreach上查找错误,因此我认为问题在于如何从请求调用数组(使用$data)@AnirbanN这是我第一次使用sqlfiddle,所以我不知道它是否符合您的期望。抱歉,它还不起作用。对于您之前的问题,我的$array来自我的网页中的一个可手持设备。我用ajax发送它,并用json获取它,但我已经将其用作数组,并且它起作用了,因此数据库中的数组确实存在问题@Anirban NI怀疑“pg_fetch_all”是否是本例中使用的正确表达式。