0

我一直在为我的网站制作一个搜索系统,我希望它能够显示一些带有从 mysql 数据库中提取的数据的结果。这引起了一个小问题,所以我在网上寻找了一些帮助,发现您实际上可以使用mysql_fetch_arraymysql_fetch_assoc来显示结果。但是,当我运行搜索表单和操作页面时,我的本地主机服务器冻结并且我的计算机开始过热(笔记本电脑)。

我将如何解决这个问题?

顺便说一句,我确实尝试过使用while ($user = $mysql->tableCheckArray(_parameters_))(正如我在一些教程中发现的那样),但是如上所述发生了同样的事情。

这是我的搜索代码:

if (isset($_POST[search]) && !empty($_POST[search])){

$search = $_POST[search];

$mysql = new mysqlHandle;

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

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

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

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

if ($rows>0){

    // Sets the row's 'id' / entry number.

    $num = 1;

    while ($num<$rows){

        $user = $mysql->tableCheckArray('*', stats, Name, $search, Portfolio);

        echo $user[Name]."<br/>";
        echo $user[Image]."<br/>";
        echo $user[Age]."<br/>"."<br/>"."<br/>"."<hr/>"."<br/>";

        $num++;

    }

    /*
    while ($num<=$rows){

        $user = $mysql->tableCheckAssoc('*', stats, Name, $search, Portfolio);

        echo $user[Name][$num]."<br/>";
        echo $user[Image][$num]."<br/>";
        echo $user[Age][$num]."<br/>"."<br/>"."<hr/>"."<br/>";

        $num++


    }
    */

}else{

    echo "No users found";

}

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

}

这是我的 MySQL 代码:

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

    $this->span=$span;
    $this->table=$table;
    $this->column=$column;
    $this->value=$value;
    $this->database=$base;

    mysql_select_db($this->database);

    $this->query10="SELECT $this->span FROM $this->table WHERE $this->column = '$this->value'";
    $this->result10=mysql_query($this->query10) or die("<br/>"."Invalid $table CHECK query: " .mysql_error());

    return mysql_fetch_assoc($this->result10);  

}

// Returns array.

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

    $this->span=$span;
    $this->table=$table;
    $this->column=$column;
    $this->value=$value;
    $this->database=$base;

    mysql_select_db($this->database);

    $this->query4="SELECT $this->span FROM $this->table WHERE $this->column = '$this->value'";
    $this->result4=mysql_query($this->query4) or die("<br/>"."Invalid $table CHECK query: " .mysql_error());

    return mysql_fetch_array($this->result4);       

}

// Returns number of rows.

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

    //accessOpen();

    $this->span=$span;
    $this->table=$table;
    $this->column=$column;
    $this->value=$value;
    $this->database=$base;

    mysql_select_db($this->database);

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

    return mysql_num_rows($this->result);

}

我显然希望显示第一个查询行的“名称”、“图像”和“年龄”值,然后显示第三个等。

4

1 回答 1

1

tableCheckArray() 每次都返回第一个匹配的记录。每次调用它时,都必须重新运行查询并传递相同的结果集。

而是让 tableCheck() 传回结果。

function tableCheck($span, $table, $column, $value, $base){
    mysql_select_db($base);

    $sql = "SELECT $span FROM $table WHERE $column = '$value'";

    return mysql_query($sql) or die("Invalid $table CHECK query: " .mysql_error());
}

然后你会运行这样的东西:

$recs = tableCheck($span, $table, $column, $value, $base);

while($row = mysql_fetch_array($recs)){
    // print $row
}

tableCheck 函数非常低效,因为它们要求您继续运行查询并且它们只返回第一条记录。此外,没有任何意义$this->span = $span。$span 已经定义,所以不要使用更多的内存重新定义它。

于 2013-04-10T19:40:52.907 回答