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条)

报告相同问题?

悬赏问题

  • ¥250 雷电模拟器内存穿透、寻基址和特征码的教学
  • ¥200 比特币ord程序wallet_constructor.rs文件支持一次性铸造1000个代币,并将它们分配到40个UTXO上(每个UTXO上分配25个代币),并设置找零地址
  • ¥15 关于Java的学习问题
  • ¥15 如何使用chatgpt完成文本分类任务?
  • ¥15 已知速度v关于位置s的等式,怎么转化为已知位置求速度v的等式
  • ¥15 我有个餐饮系统,用wampserver把环境配置好了,但是后端的网页却进去,是为什么,能不能帮远程一下?
  • ¥15 R运行没有名称为"species"的插槽对于此对象类"SDMmodelCV"
  • ¥20 基于决策树的数字信号处理,2ask 2psk 2fsk的代码,检查下报错的原因
  • ¥15 wincc已组态的变量过多
  • ¥60 如图:直线与椭圆X轴平行,求直线与椭圆任意一点的相切坐标计算公式