1

我有以下代码,对不起长度,但它节省了解释我需要的大部分内容...... pages() 函数中的 $result 没有被返回。当我调用它时,我得到的只是未定义的变量。为什么?我需要将 $start 和 $display 变量传递给 mysql 查询。

<?php
if(isset($_POST['get_records_submit'])) {
    $pages; $start; $display; $result;
    function pages($stmt) {
        //set how many records per page
        $display = 10;
        //determine how many pages there are
        if(isset($_GET['p']) && is_numeric($_GET['p'])) { //already determined
            $pages = $_GET['p'];
        }
        else {
            $records = mysqli_stmt_num_rows($stmt);
        }
        //calculate the number of pages
        if($records > $display) { //if there are more records than will fit on one page
            $pages = ceil($records/$display); //ceil() rounds up to the nearest integer
        }
        else { //records will fit on one page
            $pages = 1;
        }
        //determine which row to start returning results from
        if(isset($_GET['s']) && is_numeric($_GET['S'])) { //already determined
            $start = $_GET['s'];
        }
        else {
            $start = 0;
        }
        $result = array(0=>$display, 1=>$pages , 2=>$start);
        return $result;
    }
    $searchby = $_POST['search_by'];
    $searchfor = $_POST['search_for'];
    $contact = 1;
    $i = 0;
    //set the initial query
    $query = "SELECT  client_id, title_desc, firstname, surname, house, street, town, city, county, postcode as count FROM address LEFT JOIN client USING (client_id) LEFT JOIN client_title USING (title_id) LEFT JOIN address_street USING (address_id) LEFT JOIN address_town USING (address_id) LEFT JOIN address_city USING (address_id) LEFT JOIN address_county USING (address_id) WHERE is_contact = ?";
    //depending on search terms, amend the query
    if($searchby == 'all') {
        $query .= " ORDER BY surname ASC, firstname ASC";
        $stmt = mysqli_prepare($dbc, $query);
        mysqli_stmt_bind_param($stmt, 'i', $contact);
        mysqli_stmt_execute($stmt);
        mysqli_stmt_store_result($stmt);
        pages($stmt);
        var_dump ($result);
        foreach ($result as $var) { echo $var.' ';}
        mysqli_stmt_close($stmt);
        $query .= " ORDER BY surname ASC, firstname ASC LIMIT ?, ?";
        $stmt = mysqli_prepare($dbc, $query);
        mysqli_stmt_bind_param($stmt, 'iii', $contact, $start, $display);
        mysqli_stmt_execute($stmt);
        mysqli_stmt_bind_result($stmt, $client_id, $stitle, $sfname, $ssname, $shouse,$sstreet, $stown, $scity, $scounty, $spostcode);      

        if($searchfor != '') {
            echo "<p>You searched under <span class=\"bold\">\"All\"</span>, therefore your search term <span class=\"bold\">\"$searchfor\"</span> has not been included.</p>";
        }
    }
}
4

2 回答 2

5

在这一行:

pages($stmt);

该函数pages()返回数组,但此结果未设置为变量。尝试使用:

$result = pages($stmt);

然后通过变量访问数组$result

$result您在顶部声明的那个变量?未设置它的原因是该函数pages()无法访问它,因为它不在函数的范围内。

编辑

正如 Lyth 指出的那样,如果您不希望该函数返回任何内容,则可以按如下方式修改您的函数:

function pages($stmt) {
    global $result;
    // ...
    // The rest of your function
    // ...
    $result = array(0=>$display, 1=>$pages , 2=>$start);
    // Remove the 'return $result' line
}
于 2013-09-19T18:06:57.123 回答
0

我不知道你为什么写 $pages; $开始;$显示;$结果;? 如果您想获取数组的值,请尝试使用此...

$result = array($display, $pages , $start);
return $result;

您可以检查数组

print_r ($result);
于 2013-09-19T18:17:00.140 回答