簡體   English   中英

如何從表中將數據從一個函數傳遞到另一個函數

[英]How to pass the fetch data from table from one function to another function in php

我在mysql中有一個名為tab2的表。 它包含名稱和電話字段。

我創建了一個名為gettabledata()的函數:

function gettabledata()
{
    $data = array();

    $query = mysql_query("SELECT * FROM tab2");
    $row = mysql_fetch_assoc($query);
     //Here I am unable to get that how can I pass the fetched data in row 
    //  variable to anywhere this function called.
 //  After update:
     return $row;
}
if(isset($_POST['action']))
{
       $gettab = createtable();//calling the function gettabledata

       //Here I want to get the all data from that table
     // After doing Update:
      foreach($gettab as $row){
        echo $row['name'];
        echo $row['phone'];
     }

}

更新后,我收到警告,例如:

`
Warning: Illegal string offset 'name' in E:\xampp\htdocs\practive\csvtest.php on line 29

Notice: Uninitialized string offset: 0 in E:\xampp\htdocs\practive\csvtest.php on line 29

Warning: Illegal string offset 'phone' in E:\xampp\htdocs\practive\csvtest.php on line 30

Notice: Uninitialized string offset: 0 in E:\xampp\htdocs\practive\csvtest.php on line 30

Warning: Illegal string offset 'name' in E:\xampp\htdocs\practive\csvtest.php on line 29
0
Warning: Illegal string offset 'phone' in E:\xampp\htdocs\practive\csvtest.php on line 30 `

另外在做print_r($gettab) ,我得到了:

Array ( [name] => [phone] => 0 ) 

它只是表中的第一條記錄。

幫我解決這個問題。感謝您的期待。

您只需要在末尾添加return語句即可

gettabledata()

使用return語句修改函數,如下所示。

    function gettabledata()
    {
        $query = mysql_query("SELECT * FROM tab2");
        $row = mysql_fetch_assoc($query);

        return $row;
    }

    if(isset($_POST['action']))
    {
         $row_arr = gettabledata();//calling the function gettabledata

         foreach($row_arr as $row){
            echo $row['id'];
         }

     }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM