将“id”分配给mysql行以与关联数组一起使用

时间:2013-04-10 19:25:58

标签: php mysql

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

我该如何解决这个问题?

顺便说一句,我确实尝试使用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);

}

我显然想要显示第一个查询行的'Name','Image'和'Age'值,然后是第三个et.c

1 个答案:

答案 0 :(得分: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,因此不要使用更多内存重新定义它。