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 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化