使用mysql_fetch_array()和foreach()显示结果时,本地Apache服务器崩溃

时间:2013-04-12 13:17:53

标签: php apache foreach

所以,基本上我要做的是在页面上显示搜索命中。我已经设置了一个发布到下面页面的表单,然后在我的MySQL数据库中搜索这些数据,结果应该按以下格式显示:

<小时/>
名称
个人资料图片路径
年龄



我在互联网上广泛搜索并发现你必须使用while循环,因为我之前只使用了foreach循环。

但是每当我搜索某些内容时,我的apache服务器都会崩溃。我认为这可能是因为while循环没有崩溃,但它只会显示相同结果集(名称,图像和年龄)一定次数,具体取决于在数据库中找到的行数。

这是搜索页面代码:

require_once "lib/mysql.php";

if(isset($ _ POST [search])&amp;&amp;!empty($ _ POST [search])){

$search = $_POST[search];

// The mysql class used for functions.

$mysql = new mysqlHandle;

// Opens access to the mysql server.

$mysql->accessOpen('root', '', 'Portfolio', 'localhost');

// Sets $rows to number of entries (rows) with the 'Name' value of $search.

$rows = $mysql->tableCheckNum('*', stats, Name, $search, Portfolio);

echo "<div id='content'>
    <div id='content_main'>
        <h2>Search Results for '$search'</h2>
        <br/><hr/><br/>";

// Makes sure that there's at least 1 search hit.

if ($rows>0){

    // Sets the current hit number. (the first one is 1, the second is 2 et.c)

    $num = 1;

    $user = array();

    // My mysql function for checking the order of entries, see the attached myqsl code.

    while ($result = $mysql->tableCheckOrder('*', stats, Name, $search, ID, DESC, Portfolio)){

        // The $user array should hold the user's (searched for) details.

        $user[] = $result;

        foreach ($user as $result){

        // Makes sure that we haven't exceeded the number of rows.

            if ($num<=$rows){

            // Adds one to the number of displayed rows, as we have dsplayed another.

                $num++;

            // Prints the user's (searched for) details.

                print_r ($user[Name]."<br/>");
                print_r ($user[Image]."<br/>");
                print_r ($user[Age]."<br/>"."<br/>"."<hr/>"."<br/>");

            }

        }               
    }

}else{

    echo "No users found";

}

echo '</div>
    </div>';

}

以下是适用的MySQL代码:

// Gets order of entries (rows) The indentations under "function tableCheckOrder" isn't showing up btw.
    function tableCheckOrder($span, $table, $column, $value, $order, $mode, $base){

    mysql_select_db($base);

    $this->query11="SELECT $span FROM $table WHERE $column = '$value' ORDER BY $order $mode";
    $this->result11=mysql_query($this->query11) or die("<br/>"."Invalid $table CHECK ORDER query: " .mysql_error());

    return mysql_fetch_array($this->result11, $result_type = MYSQL_BOTH);

}

// This function checks amount of rows in table.

function tableCheckNum($span, $table, $column, $value, $base){

    mysql_select_db($base);

    $this->query="SELECT $span FROM $table WHERE $column = '$value'";
    $this->result=mysql_query($this->query) or die("Invalid $table CHECK NUM query: " .mysql_error());

    return mysql_num_rows($this->result);

}

这是我的'stats'表结构的图片:

My 'stats' table structure

我真的希望能够在不崩溃的情况下显示搜索结果

如果我可以提供更多信息,请询问!

提前致谢!

0 个答案:

没有答案