dongqing8765 2016-06-03 04:45
浏览 83
已采纳

如何使我的函数返回结果作为全局变量?

First time poster here...

Just wondering if anyone can help me turn my function's results into a global variable that I can use in another php page...

$sql = "SELECT title FROM table_1 WHERE id >= '1' and id <='10'";


    function dbCall($servername, $username, $password, $dbname, $sql) {
        global $item;
            $conn = new mysqli($servername, $username, $password, $dbname);
            if ($conn->connect_error) {
                die("Connection failed: " . $conn->connect_error);
            } 

                    $result = mysqli_query($conn, $sql);

            if (mysqli_num_rows($result) > 0) {
                    // output data of each row
                    while($row = mysqli_fetch_array($result)) {

                       $item=("'". $row[0]."', ");
                       echo $item;
                    }
                } else {
                    echo "0 results";
            }
                    return $list;
                    $conn->close();
            }

Currently it returns a list of the results:

'tom', 'mary', 'joseph', 'jim', 'kathy', 'bob', 'luke', 'sam', 'heidi', 'tiffany',

But I can't call this as a global variable in my other php page...

Could anyone help?

Edit:

Actually when I call the fucntion I get this one the other page:

include ("C:\inetpub\wwwroot\db-call.php");
$varList = array(dbCall($servername, $username, $password, $dbname));
echo $varList;

And I get this result back:

'tom', 'mary', 'joseph', 'jim', 'kathy', 'bob', 'luke', 'sam', 'heidi', 'tiffany',Array

Thanks again!---

Thanks,

  • 写回答

2条回答 默认 最新

  • drgd73844 2016-06-03 06:22
    关注

    Change your function to return the values, not print the values:

    function dbCall($servername, $username, $password, $dbname, $sql) {
        $item = [];
        $conn = new mysqli($servername, $username, $password, $dbname);
        if ($conn->connect_error) {
            die("Connection failed: " . $conn->connect_error);
        } 
    
        $result = mysqli_query($conn, $sql);
    
        if (mysqli_num_rows($result) > 0) {
                        // output data of each row
            while($row = mysqli_fetch_array($result)) {
    
                $item[] = $row[0];
            }
        } else {
            $item[] = "0 results";
        }
        $conn->close();
        return $item;
    }
    

    Now when you call like this:

    include ("C:\inetpub\wwwroot\db-call.php");
    $varList = dbCall($servername, $username, $password, $dbname);
    echo $varList;
    

    Your $varList contains array, you can process with those values as you need

    Array
    (
        [0] => tom
        [1] => mary
        [2] => joseph
        [3] => jim
        [4] => kathy
        [5] => bob
        [6] => luke
        [7] => sam
        [8] => heidi
        [9] => tiffany
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵