dssk35460 2014-09-15 10:55
浏览 16
已采纳

将sql查询结果插入新查询

So the task is:

    1. Do a query like Select * from table.
    1. Take some cell value
    1. Insert this value to a new query.

What do I have so far:
$Conn = odbc_connect("..."); $Result = odbc_exec("Select ..."); while($r = odbc_fetch_array($Result)) // showing result in a table

Here it looks like I should use the r array and insert data like

$var = r['some_field'];
$query = 'Select * from table where some_field = {$var}";

But how can I fill this array with values and how to make it available out of while loop? Here I'm using odbc, but it doesn't matter, I need the algorithm. Thanks.

The whole code looks like:

<?php
$data = array();
$state = 'false';
if($_REQUEST['user_action']=='')
{
    $Conn = odbc_connect("...");
    $data = array();     

    if($_REQUEST['name']!='')
    {                                                 
        $Result = odbc_exec($Conn, "select ...");
        //Showing result table
        while($r = odbc_fetch_array(Result))
        {                 
            array_push($data, $r['cardgroup']); 
            $state = 'true';     
        }
            //   print_r($data);  WORKS;             
    }    
}  

if ($_REQUEST['user_action'] == 'action1')
           {     
                //I need to use $data HERE. Doesn't work
                // $state = 'false' here...
           }
?>
  • 写回答

4条回答 默认 最新

  • doukeng7426 2014-09-15 11:07
    关注

    Define array outside while loop

    $data = array();//defining
    while($r = odbc_fetch_array($Result))
    

    use array_push() inside while loop

    array_push($data, $r['some_field']);
    

    then try to print array of complete data outside loop

    print_r($data);
    

    Updates


    Place $data = array(); at the top of first IF statement. Try this code:

    $data = array();//at top   
    if($_REQUEST['user_action']=='')
    {
        $Conn = odbc_connect("...");  
    
        if($_REQUEST['name']!='')
        {                                                 
            $Result = odbc_exec($Conn, "select ...");
            //Showing result table
            while($r = odbc_fetch_array(Result))
            {                 
                array_push($data, $r['cardgroup']); 
            }
                //   print_r($data);  WORKS;             
        }    
    }  
    
    if ($_REQUEST['user_action'] == 'action1')
    {     
          //print_r($data) works here also
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制