dongsi7759 2011-08-30 16:49 采纳率: 100%
浏览 45
已采纳

如何从其他页面收集退货?

I've done a query to collect some information from my DB in a .php document, and returned a value via return $var; I now want to use this value in another .php document in the same root dir. I've tried to simply state the variable in the other code document, but then I get an error message saying that my variable is undefined.

The function works similar to this one: http://se2.php.net/manual/en/function.mysql-fetch-assoc.php

And this code is present at text.func.php, I now want to use the return array in search.php

What should I do to get my value from the return, to the other page, where I want to execute more code with it?

I appreciate any kind of help. Thank you in advance.

Code:

In text.func.php:

function search($query) {

$query = mysql_real_escape_string($query);
$sql = "SELECT * FROM `text` WHERE categories LIKE '%$query%'";


$result = mysql_query($sql);

if (!$result) {
    echo "Could not successfully run query ($sql) from DB: " . mysql_error();
    exit;
}

$rows = array();

while ($row = mysql_fetch_assoc($result)) {
    echo $rows[] = $row;
}

mysql_free_result($result);

return $rows;

$rows is the variable that I want to do a foreach with and echo out on another page.

  • 写回答

1条回答 默认 最新

  • douzai6337 2011-08-30 17:28
    关注

    I think what you're trying to do here is this:

    • Have all the database logic in a function in file 1 (text.func.php?)
    • Have another file (file 2, search.php?) which uses the first file to get data from the DB

    In that case, all you have to do is this: File 1:

    <?php
    function someDataGettingFunction() {
        // get data from the DB
        return $data;
    }
    ?>
    

    File 2:

    <?php
    require_once('file1.php');
    $data = someFunctionForTheDataBase();
    // etc.
    ?>
    

    In the above case, the file which gets accessed by the user (file 2, search.php) requires the file which has the database logic in it and calls the function in this second file which gets the data. Is this what you're trying to do?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了