开发者

How to collect return from other page?

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.


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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜